Results 1 to 5 of 5

Thread: icon from handle??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Posts
    19

    Unhappy

    i want to retrive a programs icon, the one that is in the top left corner of its window, Ive tried to get the programs exe name from its handle but then im stuck without its path, then i made a search for the exe and used the first result, but it was wrong most times. I would think there is some api function somewhere to retrive its icon from its handle, or classname.

    Can somone point me in the right direction here?, cause im stumped

  2. #2
    Member
    Join Date
    Apr 1999
    Location
    Reno, NV
    Posts
    57
    Hello
    To extract icons you need a couple of API calls.
    There was a tutorial about this (at this site I
    think?). Here are the calls and a retreive Function.
    This code is from the program I wrote so it might be a little different than the tutorial was.
    But it should give you the basic idea. Sorry I don't
    have time to explain now.

    Good luck
    -William

    Option Explicit

    Private Type PicBmp
    Size As Long
    tType As Long
    hBmp As Long
    hPal As Long
    Reserved As Long
    End Type
    Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
    End Type
    Private Declare Function OleCreatePictureIndirect _
    Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _
    ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    Private Declare Function ExtractIconEx Lib "shell32.dll" _
    Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal _
    nIconIndex As Long, phiconLarge As Long, phiconSmall As _
    Long, ByVal nIcons As Long) As Long
    Private Declare Function DestroyIcon Lib "user32" (ByVal _
    hicon As Long) As Long


    Public Function GetIconFromFile(FileName As String, _
    IconIndex As Long, UseLargeIcon As Boolean) As Picture

    'Parameters:
    'FileName - File (EXE or DLL) containing icons
    'IconIndex - Index of icon to extract, starting with 0
    'UseLargeIcon-True for a large icon, False for a small icon
    'Returns: Picture object, containing icon

    Dim hlargeicon As Long
    Dim hsmallicon As Long
    Dim selhandle As Long

    ' IPicture requires a reference to "Standard OLE Types."
    Dim pic As PicBmp
    Dim IPic As IPicture
    Dim IID_IDispatch As GUID

    If ExtractIconEx(FileName, IconIndex, hlargeicon, _
    hsmallicon, 1) > 0 Then

    If UseLargeIcon Then
    selhandle = hlargeicon
    Else
    selhandle = hsmallicon
    End If

    ' Fill in with IDispatch Interface ID.
    With IID_IDispatch
    .Data1 = &H20400
    .Data4(0) = &HC0
    .Data4(7) = &H46
    End With
    ' Fill Pic with necessary parts.
    With pic
    .Size = Len(pic) ' Length of structure.
    .tType = vbPicTypeIcon ' Type of Picture (bitmap).
    .hBmp = selhandle ' Handle to bitmap.
    End With

    ' Create Picture object.
    Call OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)

    ' Return the new Picture object.
    Set GetIconFromFile = IPic

    DestroyIcon hsmallicon
    DestroyIcon hlargeicon

    End If
    End Function

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Posts
    19
    hmm, i tried something like that before using shgetfileinfo, to extract the file icon from the file..but the problem is getting the full path to a exe from its handle or classname. ive searched and searched, news groups, msg boards, sites, irc...with no luck..im begining to think its not possable to get a programs icon from its handle..

    thanks for trying to help me though. its worth something

  4. #4
    Member
    Join Date
    Apr 1999
    Location
    Reno, NV
    Posts
    57
    Hey
    Sorry, I didn't fully understand your question.
    This is where the Icon comes from

    Public Function GetIconFromFile(FileName As String, _
    IconIndex As Long, UseLargeIcon As Boolean) As Picture

    You must send it the path and file name of the exe
    to get the Icon's hdc, not the handle of the exe.
    However the program this code is from grabs all the
    Icons in the exe/

    later
    -William

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    For getting the Icon handle from a window...

    ... is not too hard:
    Code:
    'In declarations
    Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
    Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    'In code
    Dim Handle as long
    Handle = GetClassLong(hWnd, -14)
    DrawIcon hdc,0,0,Handle
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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