Is it possible to get another window's icon from its handle? Is there an easy way to get folder icons with this also?
Thanks for any assistance!!:)
Printable View
Is it possible to get another window's icon from its handle? Is there an easy way to get folder icons with this also?
Thanks for any assistance!!:)
There was a topic about this the other day in the General VB section, take a look in there for the hWnd question. But as for the file/folder icons, this is a function I use to draw file icons to a picture box/hDC:
VB Code:
Option Explicit 'Constants ------------ Private Const MAX_PATH = 260 'ShellInfo Flags Private Const SHGFI_DISPLAYNAME = &H200 Private Const SHGFI_EXETYPE = &H2000 Private Const SHGFI_SYSICONINDEX = &H4000 'System icon index Private Const SHGFI_LARGEICON = &H0 'Large icon Private Const SHGFI_SMALLICON = &H1 'Small icon Private Const SHGFI_SHELLICONSIZE = &H4 Private Const SHGFI_TYPENAME = &H400 Private Const ILD_TRANSPARENT = &H1 'Display transparent Private Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME _ Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX _ Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE '---------------------- 'Enumerations --------- Public Enum IconSizeConstants 'Icon sizes [16 by 16] [32 by 32] End Enum '---------------------- 'Types ---------------- Private Type SHFILEINFO hIcon As Long iIcon As Long dwAttributes As Long szDisplayName As String * MAX_PATH szTypeName As String * 80 End Type '---------------------- 'API Declarations ----- 'comctl32.dll: Private Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal himl&, ByVal i&, ByVal hDCDest&, ByVal x&, ByVal y&, ByVal flags&) As Long 'kernel32: Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, ByVal Source As Long, ByVal length As Long) 'shell32.dll: Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long '---------------------- Public Sub GetIcon(ByVal hDC As Long, _ ByVal Filename As String, _ Optional ByVal Index As Long = 0, _ Optional ByVal IconSize As IconSizeConstants = [32 by 32]) On Local Error Resume Next Dim ShInfo As SHFILEINFO Dim IconHandle As Long, IconSizeFlag As Long 'Select whether we want a small or a large icon IconSizeFlag = IIf(IconSize = [16 by 16], SHGFI_SMALLICON, SHGFI_LARGEICON) 'Get a handle to the icon IconHandle = SHGetFileInfo(Filename, 0&, ShInfo, Len(ShInfo), _ BASIC_SHGFI_FLAGS Or IconSizeFlag) 'If it's found draw it to the specified hDC If IconHandle <> 0 Then Call ImageList_Draw(IconHandle, _ ShInfo.iIcon, hDC, 0, 0, ILD_TRANSPARENT) End Sub
Thanks for the info.
I was just reading that when I searched again. I don't know how
I missed it. The SendMessage function works good enough for
the folder and other icons.
Is there a way to preserve the icon's transparency though?
I'm planning on adding the icon's to a menu.
Try this flag ILD_MASK in addition to ILD_TRANSPARENT...look at the API Viewer for its declaration.
I was wondering that myself. In case you don't have the API Viewer here's the constant:
VB Code:
Public Const ILD_MASK = &H10
I couldn't get it to work myself, although I am a bit thick, so that could be the cause :D
That doesn't seem to be working the best.
I would like to add icons to menus and SetMenuItemBitmaps
probably doesn't support that.
How would I get the icons to be transparent in the menu?
Windows does it
Just convert the icon to a bitmap (then you could use SetMenuBitmaps)
I thought that's what I did.. I saved the icon to a file, then loaded it back in. That's all I knew of how to convert the file type.
Would that method perserve transparency though?