Set Icons for any Form or Program

By | 2002-06-01

With this code you can place any Icon in the title bar of any Window, just by reffering to a .ico file or to the position of the Icon in a DLL.

Original Author: J. van Gils

Inputs

Handle of the window you want to change the icon of.

Assumptions

You need to have the Window Handle (hWnd) of the window whitch Icon you want to change. This can be done by searching/finding it with the API-call
Declare Function FindWindowEx Lib “user32” Alias “FindWindowExA” (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Side Effects

none (that I know of)

API Declarations

Declare Function ExtractIcon Lib “shell32.dll” Alias “ExtractIconA” (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Declare Function DefWindowProc Lib “user32” Alias “DefWindowProcA” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const WM_SETICON = &H80

Code

Public Function SetIcon(FormhWnd As Long)
Dim x, i As Long
  i = ExtractIcon(0, "c:SomeDll.DLL", 3)
   'In this case you will extract the 3rd icon from SomeDll.DLL. In this
   'way you can extract any icon you want, just by reffering to the icon
   '(number) of the icon you want to extract in the dll. If you want to
   'know the iconnumbers of a dll, you will have to use a recource editor
   '(like Borland Recource Workshop). You can also extract the Icon Handle
   'of a .ico file just by using some code like:
   'i=ExtractIcon(0,"c:SomeIconFile.ico",0)
   'where SomeIconFile is the name of the icon you want to use.
   'Now finally set the icon in the title bar of the window
  x = DefWindowProc(FormhWnd, WM_SETICON, &H1, i)
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.