Results 1 to 8 of 8

Thread: Getting icon from hwnd?

  1. #1

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    Getting icon from hwnd?

    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!!

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    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:
    1. Option Explicit
    2. 'Constants ------------
    3. Private Const MAX_PATH = 260
    4. 'ShellInfo Flags
    5. Private Const SHGFI_DISPLAYNAME = &H200
    6. Private Const SHGFI_EXETYPE = &H2000
    7. Private Const SHGFI_SYSICONINDEX = &H4000 'System icon index
    8. Private Const SHGFI_LARGEICON = &H0       'Large icon
    9. Private Const SHGFI_SMALLICON = &H1       'Small icon
    10. Private Const SHGFI_SHELLICONSIZE = &H4
    11. Private Const SHGFI_TYPENAME = &H400
    12.  
    13. Private Const ILD_TRANSPARENT = &H1       'Display transparent
    14. Private Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME _
    15.         Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX _
    16.         Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE
    17. '----------------------
    18.  
    19. 'Enumerations ---------
    20. Public Enum IconSizeConstants 'Icon sizes
    21.     [16 by 16]
    22.     [32 by 32]
    23. End Enum
    24. '----------------------
    25.  
    26. 'Types ----------------
    27. Private Type SHFILEINFO
    28.   hIcon As Long
    29.   iIcon As Long
    30.   dwAttributes As Long
    31.   szDisplayName As String * MAX_PATH
    32.   szTypeName As String * 80
    33. End Type
    34. '----------------------
    35.  
    36. 'API Declarations -----
    37. 'comctl32.dll:
    38. Private Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal himl&, ByVal i&, ByVal hDCDest&, ByVal x&, ByVal y&, ByVal flags&) As Long
    39. 'kernel32:
    40. Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, ByVal Source As Long, ByVal length As Long)
    41. 'shell32.dll:
    42. 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
    43. '----------------------
    44.  
    45. Public Sub GetIcon(ByVal hDC As Long, _
    46.     ByVal Filename As String, _
    47.     Optional ByVal Index As Long = 0, _
    48.     Optional ByVal IconSize As IconSizeConstants = [32 by 32])
    49.     On Local Error Resume Next
    50.     Dim ShInfo As SHFILEINFO
    51.     Dim IconHandle As Long, IconSizeFlag As Long
    52.  
    53.     'Select whether we want a small or a large icon
    54.     IconSizeFlag = IIf(IconSize = [16 by 16], SHGFI_SMALLICON, SHGFI_LARGEICON)
    55.     'Get a handle to the icon
    56.     IconHandle = SHGetFileInfo(Filename, 0&, ShInfo, Len(ShInfo), _
    57.         BASIC_SHGFI_FLAGS Or IconSizeFlag)
    58.     'If it's found draw it to the specified hDC
    59.     If IconHandle <> 0 Then Call ImageList_Draw(IconHandle, _
    60.         ShInfo.iIcon, hDC, 0, 0, ILD_TRANSPARENT)
    61. End Sub

  3. #3

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    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.

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Try this flag ILD_MASK in addition to ILD_TRANSPARENT...look at the API Viewer for its declaration.
    Baaaaaaaaah

  5. #5
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I was wondering that myself. In case you don't have the API Viewer here's the constant:

    VB Code:
    1. 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

  6. #6

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    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

  7. #7
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    Just convert the icon to a bitmap (then you could use SetMenuBitmaps)

  8. #8

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width