CHANGING WINDOW ICONS AT RUNTIME (intermediate level)

Home

Remembering Logo
Turtle graphics tutorial
Scripting language tutorial
Changing window icons at runtime
A real spinner control
Tip Corner - Select Case
Hocus Focus
Illuminated Radiobuttons

It is possible to have a different icon in each window of a program. We can use the Liberty BASIC icon editor to create an icon and to include it in the runtime engine so that it becomes the icon for each window in a program. It is sometimes interesting or useful to have individual icons for the different windows in a program, and this can be achieved quite easily.

To do this, include all icons required in the program's distribution. When needed, the icons must first be loaded from disk with a function call from user32.dll called LoadImageA. Here it is, with all arguments documented:

IconFile$ = "guy.ico"
CallDLL #user32, "LoadImageA", _
_NULL As long, _ 'instance handle, can be null (=0)
IconFile$ As ptr, _ 'file name
_IMAGE_ICON As long, _ 'type
16 As long, _ 'width
16 As long, _ 'height
_LR_LOADFROMFILE As long, _ 'flags
hIcon As long 'returns icon handle


The function returns a handle to the icon that is now loaded into memory. A call to SendMessageA with the windows message _WM_SETICON, the handle of the window and the handle of the loaded icon will cause the icon to appear as the window's icon:

'Set the icon.
     CallDLL #user32, "SendMessageA", _
     hWnd As long, _ 'window handle
     _WM_SETICON As long, _ 'message to set icon
     _ICON_SMALL As long, _ '_ICON_SMALL/_ICON_BIG
     hIcon As long, _ 'icon handle
     hOldIcon As long 'returns handle to old icon

Whenever we load images, we must destroy them or unload them before ending the program to free the memory that they consumed. There is a special function for this called DestroyIcon.

CallDLL #user32, "DestroyIcon", _
     hIcon As long, _ 'icon handle
     result As long

For a working example, see ICON.BAS - adapted from an example by Brent Thorn, used by permission. Thanks, Brent!

Please see the zip file attached to this newsletter for a copy of icon.bas and the two icon files it uses.

Home

Remembering Logo
Turtle graphics tutorial
Scripting language tutorial
Changing window icons at runtime
A real spinner control
Tip Corner - Select Case
Hocus Focus
Illuminated Radiobuttons