Date: 02-16-2022
Return to Index
created by gbSnippets
'There are several ways in which to access icons which ship with Windows. This
'snippet covers the handful of system icons which are accessed with the LoadIcon
'API by using special constants in lieu of the usual file name or resource name
'usually associated with the LoadIcon API.
'No resource file nor icon files are required to access these system icons.
'Primary Code:
'The use of LoadIonc can load one of the 5 system icons, returning a handle
'to the icon which may be used in a number of ways. In this example, the icon
'is used with the WM_SetIcon message to set the icon of a dialog.
hIcon = LoadIcon(ByVal %Null, ByVal %IDI_Information)
SendMessage hDlg, %WM_SETICON, %ICON_BIG, hIcon
SendMessage hDlg, %WM_SETICON, %ICON_SMALL, hIcon
'Another option is to use the icon handle to add icons to an imagelist, which
'can then be attached to a toolbar or can be used with various GRAPHIC statements.
'Here's the code for adding the icons to an imagelist.
ImageList New Icon 16,16,24,10 To hLst
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Application)
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Error)
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Exclamation)
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Information)
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Question)
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg, hIcon As DWord, Icon() As Long, hLst As Long
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,300,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Cycle Dialog Icon", 60,60,160,25
Control Add Button, hDlg, 200,"Create Toolbar w/Icons", 60,95,160,25
Dim Icon(4)
Array Assign Icon() = %IDI_Application, %IDI_Error, %IDI_Exclamation, %IDI_Information, %IDI_Question
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Static i As Long
Incr i
hIcon = LoadIcon(ByVal %Null, ByVal Icon(i Mod 5))
SendMessage hDlg, %WM_SETICON, %ICON_BIG, hIcon
SendMessage hDlg, %WM_SETICON, %ICON_SMALL, hIcon
' SetClassLong hDlg, %GCL_HICON, LoadIcon(ByVal %NULL, ByVal %IDI_INFORMATION)
End If
If CB.Msg = %WM_Command AND CB.Ctl = 200 AND CB.Ctlmsg = %BN_Clicked Then
'add toolbar
Control Add Toolbar, hDlg, 500,"", 0,0,0,0
'create imagelist
ImageList New Icon 16,16,24,10 To hLst
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Application)
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Error)
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Exclamation)
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Information)
ImageList Add Icon hLst, LoadIcon(ByVal %Null, ByVal %IDI_Question)
'attach imagelist
Toolbar Set ImageList hDlg, 500, hLst, 0
'create buttons
Toolbar Add Button hDlg, 500, 1, 200, %TbStyle_Button, "Application"
Toolbar Add Button hDlg, 500, 2, 201, %TbStyle_Button, "Error"
Toolbar Add Button hDlg, 500, 3, 202, %TbStyle_Button, "Exclamation"
Toolbar Add Button hDlg, 500, 4, 202, %TbStyle_Button, "Information"
Toolbar Add Button hDlg, 500, 5, 202, %TbStyle_Button, "Question"
End If
End Function
'gbs_00476
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm