Results 1 to 2 of 2
2008-05-26, 18:36 #1
- Join Date
- Mar 2001
- Springfield, Ohio, USA
- Thanked 2 Times in 2 Posts
Using DrawIcon api (2K or greater)
Given a filename, I want to add the file's default icon to an image control on my Userform at runtime.
I started with VB code that was available on the internet (URL is in the code). I tried it with VB6 & it works.
It gets the icon from the registry. Stepping thru the code in VBA, this seems to work. But then it uses DrawIcon from user32 on Picture1.hDC. With a UserForm, we only have an Image control, so I tried Image1.Picture.Handle and also Image1.Picture.hPal. With either way, DrawIcon returns a zero and no icon is drawn. I've attached the workbook with the code. TIA for any help. Sam<font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
<small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>
2008-05-26, 21:39 #2
- Join Date
- Mar 2002
- Thanked 30 Times in 30 Posts
Re: Using DrawIcon api (2K or greater)
As far as I know, an Image control on a VBA UserForm does not have a hDC (device context) property.
A userform does have a hDC. You could use the following code to draw the icon on the userform itself (you'd have to remove the image control, and move the command button and label down a bit).
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub CommandButton1_Click()
Dim rsp As Variant
Dim fso As Scripting.FileSystemObject
Dim f As Scripting.file
rsp = Application.GetOpenFilename()
If rsp <> False Then
Set fso = New Scripting.FileSystemObject
Set f = fso.GetFile(CStr(rsp))
Label1.Caption = f.Name
GetDefaultIcon f.Name, GetDC(FindWindow("ThunderDFrame", Me.Caption))