Insert new icon in userform

Z

zz

I have the following code, this is use to apply a style to my userforms, so
it gets a controlbox like the regular Vb forms, it also inserts an icon in
the userform's caption.

My question is, is it really necessary that the hIcon be the result of the
ExtractIcon function?

instead, how could i pass an .ico file as a parameter so i can use my own
custom .ico file in each useform?




Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA"
(ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As
Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal
lParam As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA"
(ByVal hWnd As Long, ByVal lpString As String) As Long


Public Function Apply_Style(formcaption As String, iconnum As Integer)

Dim hWnd, eWnd As Long
Dim hIcon As Long
Dim f_style As Long

'Form window
hWnd = FindWindow("ThunderDFrame", formcaption)

'Application window
eWnd = FindWindow("XLMAIN", Application.Caption)

'Grab the icon from shell32.dll...
hIcon = ExtractIcon(0, "SHELL32.DLL", iconnum)

'draw the icon
SendMessage hWnd, &H80, True, hIcon
SendMessage hWnd, &H80, False, hIcon


f_style = GetWindowLong(hWnd, -16)
f_style = f_style Or &H30000 'system toolbox
f_style = f_style Or &H20000 'minimize
f_style = f_style Or &H10000 'maximize

SetWindowLong hWnd, -16, f_style


End Function

i use the function as follows apply_style me.caption, 16

' where " me.caption" is the caption of the current userform, and "16 " is
the index of the icon in the shell.dll icons list.
 
C

Cindy M.

Hi Zz,

You might try asking this in a newsgroup for "classic" Visual Basic. The folks
there may have a better idea what you can do with these Windows API functions.
(And if you get an answer, it would be cool if you could update the message
thread. Interesting thing...)
I have the following code, this is use to apply a style to my userforms, so
it gets a controlbox like the regular Vb forms, it also inserts an icon in
the userform's caption.

My question is, is it really necessary that the hIcon be the result of the
ExtractIcon function?

instead, how could i pass an .ico file as a parameter so i can use my own
custom .ico file in each useform?




Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA"
(ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As
Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal
lParam As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA"
(ByVal hWnd As Long, ByVal lpString As String) As Long


Public Function Apply_Style(formcaption As String, iconnum As Integer)

Dim hWnd, eWnd As Long
Dim hIcon As Long
Dim f_style As Long

'Form window
hWnd = FindWindow("ThunderDFrame", formcaption)

'Application window
eWnd = FindWindow("XLMAIN", Application.Caption)

'Grab the icon from shell32.dll...
hIcon = ExtractIcon(0, "SHELL32.DLL", iconnum)

'draw the icon
SendMessage hWnd, &H80, True, hIcon
SendMessage hWnd, &H80, False, hIcon


f_style = GetWindowLong(hWnd, -16)
f_style = f_style Or &H30000 'system toolbox
f_style = f_style Or &H20000 'minimize
f_style = f_style Or &H10000 'maximize

SetWindowLong hWnd, -16, f_style


End Function

i use the function as follows apply_style me.caption, 16

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
K

Karl E. Peterson

zz said:
I have the following code, this is use to apply a style to my
userforms, so it gets a controlbox like the regular Vb forms, it also
inserts an icon in the userform's caption.

My question is, is it really necessary that the hIcon be the result
of the ExtractIcon function?
No.

instead, how could i pass an .ico file as a parameter so i can use
my own custom .ico file in each useform?

Take a look at the LoadImage API.

http://groups.google.com/group/micr...st&q=loadimage+ico+vb&rnum=8#387a340f617528fe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top