API: show Icon on a form (1 Viewer)

key

Registered User.
Local time
Yesterday, 22:37
Joined
Jun 19, 2002
Messages
45
Hi,

I'm trying to show the notepad icon on my form. The code below is the result approach.
Anyway, it doesn't work... I don't know how to refer to the hInst and hDC (see question marks). It should be the application object and form object... I'm trying, trying...

Many thanx for any suggestions

key


Option Compare Database
Option Explicit

Declare Function DrawIcon Lib "user32.dll" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long

Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long

Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Long) As Long


Public Function display()

Dim hIcon As Long
Dim retval As Long

hIcon = ExtractIcon(????, "C:\Winnt\notepad.exe", 0)

retval = DrawIcon(????, 100, 75, hIcon)

retval = DestroyIcon(hIcon)


End Function


P. S. just one more thing: in VB you can use app.hInstance calling the ExtractIcon function....
 
Last edited:

ghudson

Registered User.
Local time
Yesterday, 23:37
Joined
Jun 8, 2002
Messages
6,195
display custom icon in a forms title bar

Try this...

'place this sub in each forms "Load" event
Private Sub Form_Load()
SetFormIcon Me.hWnd, "C:\Icons\Icon1.ico" 'Location of icon file
End Sub

'copy below code in a new public module
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long

Private Const WM_SETICON = &H80
Private Const IMAGE_ICON = 1
Private Const LR_LOADFROMFILE = &H10
Private Const SM_CXSMICON As Long = 49
Private Const SM_CYSMICON As Long = 50

Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, LParam As Any) As Long

Public Function SetFormIcon(hWnd As Long, strIconPath As String) As Boolean
Dim lIcon As Long
Dim lResult As Long
Dim X As Long, Y As Long

X = GetSystemMetrics(SM_CXSMICON)
Y = GetSystemMetrics(SM_CYSMICON)
lIcon = LoadImage(0, strIconPath, 1, X, Y, LR_LOADFROMFILE)
lResult = SendMessage(hWnd, WM_SETICON, 0, ByVal lIcon)
End Function
 

Users who are viewing this thread

Top Bottom