Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Add Bitmap to Menu Item (B&W only)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin Add Bitmap to Menu Item (B&W only)

    Color bitmaps in menus are cool, but take a fair amount of code. If you'll settle for B&W bitmaps, it takes only 2 lines of code per menu item.

    Primary Code:
    Code:
    hBmp = LoadImage(GetModuleHandle(""), "bwhite", %IMAGE_BITMAP, 0, 0, %LR_MonoChrome)
       SetMenuItemBitmaps hMenuFile, 3, %MF_ByPosition, hBMP, hBMP
    You can load bitmaps from resources or files. The example below does it with resources, but a comment is provided that shows the API call to do it from files.

    The bitmaps should be monochrome and limited to 13x13 pixels (10x10 is better yet).

    Here's an example of what you get:



    This example uses my pbsnippets.pbr resource file. You can get it here. The pbr file includes five bitmaps in it that are suitable for use in menus: bnew, bopen, bsave, bprint, and bwhite (bwhite bitmap can be used to show no icon). I created them using MSPaint. I loaded icons, resized them, and saved them as monochrome bitmaps.

    And here is the compilable:
    Code:
    'Compilable Example:
    #Compile Exe
    #Dim All
    #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
    Last edited by Gary Beene; 5 Feb 2010, 01:41 PM.
Working...
X