Click here to Skip to main content
15,889,883 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi below is my code to select all items on Another "Third Party Application" SysLisView items. It is not listview control within my application.

C#
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessageLVItem(HandleRef hWnd, int msg, int wParam, ref LVITEM lvi);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct LVITEM
        {
            public int mask;
            public int iItem;
            public int iSubItem;
            public int state;
            public int stateMask;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string pszText;
            public int cchTextMax;
            public int iImage;
            public IntPtr lParam;
            public int iIndent;
            public int iGroupId;
            public int cColumns;
            public IntPtr puColumns;
        }
          IntPtr SysListView = getlistviewhandle();
            LVITEM lvi = new LVITEM();
            const int LVM_FIRST = 0x1000;
            const int LVM_SETITEMSTATE = LVM_FIRST + 43;
            const int LVIS_SELECTED = 0x0002;
            const int LVIS_FOCUSED = 0x0002;
            const int LVIF_STATE = 0x0008;
            //lvi.iItem = 0;
            lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED; //only bit 2 (LVIS_SELECTED) is valid

            lvi.state = LVIS_FOCUSED | LVIS_SELECTED;
            lvi.mask = LVIF_STATE;
            IntPtr dc = SendMessageLVItem(new HandleRef(SysListView, SysListView), LVM_SETITEMSTATE, -1, ref lvi);




This above code should work as per msdn documentation that I read. But it's not selecting any items in another process listview. Handle to listview is correct. I confirmed also that no wm_command seen in spy++. Only messages I saw in spy++ was setitemstate when I select manually all items using ctrl+a.

When i set lvi items structure to like below it deselect all selected rows.
C#
lvi.stateMask = 2; //For lvi selected.

            lvi.state = 0; // for deselect all rows in listviewitems.
            lvi.mask = LVIF_STATE;


but when I used it like this as per msdn documentation, it won't work.

C#
lvi.stateMask = 2; //For lvi selected.

            lvi.state = 2; // For lvi selected. <<-- Made change here.
            lvi.mask = LVIF_STATE;


Am I missing something in lvi structure? Please let me know if you know a solution.
Posted
Updated 12-Apr-13 18:14pm
v11
Comments
Sergey Alexandrovich Kryukov 13-Apr-13 0:28am    
Why would you use P/Invoke at all? It's not good; all you want is already implemented in .NET FCL, well, almost. What do you want to achieve?
—SA
Nilesh bhope 13-Apr-13 2:59am    
It is third party syslistview control of external application, Not my own syslistview control. I intend to copy syslistview contents in clipboard using wm_command. It's working fine for copying items contents. But this external application only copying selected list view rows.
I was trying to call set item state to select all rows & then call copy command by sending wm_command. And lvm get item won't work on this syslistview control, cause it has ownerdrawfixed style.

I confirm that in spy++, set item state was calling when manually selecting all rows in listview.

But as per above code I tried, it won't select all listview rows.

Also same for windows task manager list view control. It won't select all rows.

I hope I clarify enough, why I wanted to get working above code.
Nilesh bhope 15-Apr-13 11:44am    
The code mention above sometime work & sometime won't work. I think flags has to be reset or something I am missing. Same happened with windows task manager list view control. It work some time & sometime it won't.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900