Click to See Complete Forum and Search --> : icon from handle??
prye
Jul 6th, 2000, 02:59 PM
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
:(
William
Jul 6th, 2000, 11:03 PM
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
prye
Jul 7th, 2000, 05:02 AM
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 :)
William
Jul 7th, 2000, 05:18 AM
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
kedaman
Jul 10th, 2000, 08:43 AM
... is not too hard:
'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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.