
Get Desktop Icons - How ?
I'm writing a program in VB6 that gets the position of the desktop icons in
order to restore them later. To make it more fancy, I added a listbox with
the icon names and a picturebox that shows the icon of the active item in
the listbox. My first shot was using SHGetFileInfo to get the icon handle
and DrawIconEx to draw it to the picbox. Problem with this method is that it
does not work for shortcuts or the "special items" like the recycle bin on
the desktop.
Now I'm looking for a better way. I could use the IShellFolder interface and
reference PIDLs, and I'm sure that will work because I have example code
that does it, but it is rather long code.
So, I was wondering if I coundn't find a way to use the icons that the
desktop listview uses. I tried to get access to the imagelist of the
listview, but I do not seem to get the handle of the imagelist right. Or,
the listview is empty...
Below is the code that I'm trying to use. Place a commandbutton (command1)
and a picturebox (picture1) on your form to run it.
Any help is appreciated !
David.
----------------------------------------------------------------------------
---------------
Option Explicit
'Find Desktop Listview Handle
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA"
(ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpClassName
As String, ByVal lpWindowName As String) As Long
'Get Desktop Icons
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long
Private Declare Function ImageList_GetImageCount Lib "comctl32.dll" (ByVal
himl As Long) As Long
Private Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal himl As
Long, ByVal i As Long, ByVal hdcDst As Long, ByVal x As Long, ByVal y As
Long, ByVal fStyle As Long) As Long
Private Const LVM_FIRST = &H1000
Private Const LVM_GETIMAGELIST = (LVM_FIRST + 2)
Private Const LVSIL_NORMAL = 0
Private Const ILD_TRANSPARENT = &H1
Private Sub Command1_Click()
'Get handle of listview on the desktop that holds the icons
Dim hDesktopListView As Long
hDesktopListView = flGetDesktopListViewHandle
If hDesktopListView = 0 Then Exit Sub
'Get handle to desktop listview image list
Dim hImageList As Long
hImageList = SendMessage(hDesktopListView, LVM_GETIMAGELIST,
LVSIL_NORMAL, 0&)
Debug.Print hImageList '<-- Is this the correct handle?
Debug.Print ImageList_GetImageCount(hImageList) '<-- Returns 0...?!
'Draw an icon (Does not work)
Picture1.AutoRedraw = True
Debug.Print ImageList_Draw(hImageList, 0&, Picture1.hDC, 0&, 0&,
ILD_TRANSPARENT)
Debug.Print Err.LastDllError
End Sub
Private Function flGetDesktopListViewHandle() As Long
Dim hDummy As Long
hDummy = FindWindow("Progman", vbNullString)
hDummy = FindWindowEx(hDummy, 0, "SHELLDLL_defVIEW", vbNullString)
flGetDesktopListViewHandle = FindWindowEx(hDummy, 0, "SysListView32",
vbNullString)
End Function