Date: 02-16-2022
Return to Index
created by gbSnippets
Small icons returned by SHGetFileInfo are usually 16x16 Pixels, While
SetMenuItemBitmaps takes 13x13 bitmaps (actually GetSystemMetrics()
SM_CXMENUCHECK x SM_CYMENUCHECK). So the result will probably look
weird. If you want good looking icons in your menus, you either have
to dig into owner drawing them (see http://vbaccelerator.com For
samples) Or Get a third party Control that handles it For you.
>When retrieving the file info I Get an Icon Handle AND I need to reformat
>the Handle to a Bitmap Handle in order to Display it.
The GetIconInfo API can Do that For you.
http://support.microsoft.com/kb/298144
Function SetMenuIcon(FrmHwnd As Long, IconName As String, MainMenuNumber As Long, MenuItemNumber As Long, Flags As Long) As Long
On Error Resume Next
Local MemIcon As ICONINFO
Local lngMenu As Long, lngSubMenu As Long, lngMenuItemID As Long, hIcon As Long, hBmp&
Local AsciizIcon As Asciiz * 100
AsciizIcon = IconName
hIcon = LoadIcon(GetModuleHandle(""), AsciizIcon)
GetIconInfo hIcon, MemIcon
hBmp = MemIcon.hBmColor
lngMenu = GetMenu(FrmHwnd)
lngSubMenu = GetSubMenu(lngMenu, MainMenuNumber)
lngMenuItemID = GetMenuItemID(lngSubMenu, MenuItemNumber)
Function = SetMenuItemBitmaps(lngMenu, lngMenuItemID, Flags, hBmp, hBmp)
End Function
' hIcon = LoadImage(%Null, "info.ico", %IMAGE_ICON, 0, 0, %LR_LoadFromFile)
' Local MemIcon As ICONINFO
' GetIconInfo hIcon, MemIcon
' hBmp = MemIcon.hBmColor
' SetMenuItemBitmaps hMenuFile, MenuPos, %MF_ByPosition, hBMP, hBMP
' Graphic Bitmap Load "16x16_2.bmp", 16,16 To hBMP
' Graphic Get DC To hDC
' hDC2 = CreateCompatibleDC(hDC)
' hBMP2 = CreateCompatibleBitmap(hDC2, 16, 16)
' SetMenuItemBitmaps hMenuFile, MenuPos, %MF_ByPosition, hBMP2, hBMP2
' hBmp = LoadImage(GetModuleHandle(""), "16x16.bmp", %IMAGE_BITMAP, 0, 0, 0)
' SetMenuItemBitmaps hMenuFile, MenuPos, %MF_ByPosition, hBMP, hBMP
' hIcon = LoadIcon(%Null, "lathe.ico")
' GetIconInfo hIcon, MemIcon
' hBmp = MemIcon.hBmColor
' SetMenuItemBitmaps hMenuFile, MenuPos, %MF_ByPosition, hBMP, hBMP
' hIcon = LoadImage(0, "info.ico", %IMAGE_ICON, 0, 0, %LR_LoadFromFile Or %LR_MonoChrome)
' Local MemIcon As ICONINFO
' GetIconInfo hIcon, MemIcon
' hBmp = MemIcon.hBmColor
' SetMenuItemBitmaps hMenuFile, MenuPos, %MF_ByPosition, hBMP, hBMP
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32api.inc"
#Resource "gbsnippets.pbr"
%IDM_Open = 400 : %IDM_Save = 401 : %IDM_Exit = 403 : %IDM_Sep = 800
Global hDlg, hMenu, hMenuFile, hBmp As DWord
Function PBMain()
Dialog New Pixels, 0, "Bitmap in Menu Demo",300,300,250,150, %WS_OverlappedWindow To hDlg
AddMenu
Dialog Show Modal hDlg
End Function
Sub AddMenu()
Menu New Bar To hMenu
Menu New Popup To hMenuFile
Menu Add Popup, hMenu, "&File", hMenuFile, %MF_Enabled
Menu Add String, hMenuFile, "&Open" + $Tab + "Ctrl-O", %IDM_Open, %MF_Enabled Or %MF_Checked
Menu Add String, hMenuFile, "&Save" + $Tab + "Ctrl-S", %IDM_Save, %MF_Enabled Or %MF_Checked
Menu Add String, hMenuFile, "-", %IDM_Sep, 0
Menu Add String, hMenuFile, "E&xit", %IDM_Exit, %MF_Enabled Or %MF_Checked
Menu Attach hMenu, hDlg
'add B&W bitmaps (resources)
hBmp = LoadImage(GetModuleHandle(""), "bopen", %IMAGE_BITMAP, 0, 0, %LR_MonoChrome)
SetMenuItemBitmaps hMenuFile, 0, %MF_ByPosition, hBMP, hBMP
hBmp = LoadImage(GetModuleHandle(""), "bsave", %IMAGE_BITMAP, 0, 0, %LR_MonoChrome)
SetMenuItemBitmaps hMenuFile, 1, %MF_ByPosition, hBMP, hBMP
hBmp = LoadImage(GetModuleHandle(""), "bwhite", %IMAGE_BITMAP, 0, 0, %LR_MonoChrome)
SetMenuItemBitmaps hMenuFile, 3, %MF_ByPosition, hBMP, hBMP
' Could also load from file (monochrome bitmaps)
' hBmp = LoadImage(%Null, "13x13.bmp", %IMAGE_BITMAP, 0, 0, %LR_LoadFromFile Or %LR_MonoChrome) 'from file
End Sub
'gbs_00434
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm