Board index » delphi » Getting selected items from SysListview32 in Explorer windows

Getting selected items from SysListview32 in Explorer windows


2003-07-16 08:02:06 PM
delphi210
Hi! =)
I'm making an application that needs to find the selected items in the
listview inside an Explorer window. I found some code that uses SendMessage
and it works fine, but
it returns ALL items not just the selected. In time I will
need the icon associated with the items, but I am just wondering.. Is it
possible to assign or make a "copy" of an "alien"
control? like: mylistview := alienform.listview
thanks in advance,
Peter
 
 

Re:Getting selected items from SysListview32 in Explorer windows

Hi Jeremy, thanks for you answer =) Do you have any source somewhere,
because I am new to API coding in Delphi and feel STUCK :(
thanks
- Peter
"Jeremy Collins" <XXXX@XXXXX.COM>skrev i en meddelelse
Quote
Peter Timmerman writes:
>Hi! =)
>
>I'm making an application that needs to find the selected items in the
>listview inside an Explorer window. I found some code that uses
SendMessage
>and it works fine, but
>it returns ALL items not just the selected.

Yep, then you just iterate the collection and
list which items state indicates "selected".

--
jc

 

Re:Getting selected items from SysListview32 in Explorer windows

Hi again,
thanks - this sounds VERY promising!
=)
- Peter
"Jeremy Collins" <XXXX@XXXXX.COM>skrev i en meddelelse
Quote
Peter Timmerman writes:
>Hi Jeremy, thanks for you answer =) Do you have any source somewhere,
>because I am new to API coding in Delphi and feel STUCK :(

I'll knock up some code if you need further help, but once
you have a handle to your ListView [1], you use the control
messages documented in the API help. So to get the first
selected item:

SendMessage(hwndListView,
LVM_GETNEXTITEM,
0,
MAKELPARAM(LVNI_ALL or LVNI_SELECTED, 0));

This will return the item index of the first selected item.
To get the next selected item, send the same message with
the returned index as the 3rd parameter (the start index),
and repeat until no more items are returned.

To get the total number of items use
SendMessage(hwndListView, LVM_GETITEMCOUNT, 0, 0);

Now the catch. To get anything really useful, you need to
persuade Windows to pass some data via SendMessage from Explorer
to your application, something which it isn't very keeen on
doing.

If the listview was owned by your application, you could do this:

var
AItem : tagLVITEM;
hwndListView : HWND;
begin
hwndListView := ListView1.Handle;
FillChar(AItem , SizeOf(tagLVITEM), 0);
AItem.iItem := 1; //2nd item, e.g.
AItem.mask := LVIF_TEXT or LVIF_IMAGE;

ListView_GetItem(hwndListView, AItem);
//AItem will now contain info about the list item...
end;

The ListView_GetItem macro simply wraps a call to SendMessge
with LVM_GETITEM, to make (C++) code more readable. Delphi's
TListView already wraps this stuff up in properties (aren't you
glad).

Anyway, to get aound this, you have to create a shared chunk
of memory. Rather than explain this myself, read this usenet
post:

tinyurl.com/hjo9

Also spend some time reading your SDK help files under "List
View Controls", the "LVM_XXX" messages and "ListView_XXX" macros.
You'll need to be familiar with the LVITEM structure, and also
with how Windows (not Delphi) handles image lists.

HTH

--
jc


[1] Be careful of code which calls FindWindowEx with hardcoded
window class names - these could differ between Windows versions.