Icons on a PopupMenu
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Icons on a PopupMenu

  1. #1
    Join Date
    Jun 2003
    Location
    philippines
    Posts
    109

    Icons on a PopupMenu

    hello. good Day.

    i have created a menu, that will be displayed whenever i click my right mouse button.
    my problem is want my menu to include an icon for each menu item, and i dont know how??????

    thanks for the help.

  2. #2
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646
    Try www.vbaccelerator.com, look for IconMenu, the site have a variety of library for adding icons to menus and the best part is it's free and with full source code

  3. #3
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    Form:

    Code:
    Private Bitmaps As New CMenuBitmap
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim tmp As String
        Dim ltmp As Long
        Set Bitmaps = New CMenuBitmap
    
    ' For an image list, first number is the menu number, ie file=0, edit=1
    ' second number is menu item 1=new, 2=spacer, 3=exit
        With Bitmaps
            .SetBitmap Me.hWnd, 0, 2, ImageList1.ListImages(2).Picture, ImageList1.ListImages(2).Picture
        End With
    
    ' For an ressource file you'll need an array of picture boxes 1 per picture
    ' then load the picture into the picturebox, then use the code above with modifications
        Picture1.Picture = LoadResPicture("1", 0)
        Picture2.Picture = LoadResPicture("Cut", 0)
        With Bitmaps
            .SetBitmap Me.hWnd, 0, 0, Picture1.Picture, Picture1.Picture
            .SetBitmap Me.hWnd, 1, 0, Picture2.Picture, Picture2.Picture
        End With
         
        Set Bitmaps = Nothing
    End Sub
    
    Private Sub menuFileEnvelope_Click()
    ' Only if you specify a different checked graphic will this make any difference
        menuFileEnvelope.Checked = Not menuFileEnvelope.Checked
    End Sub
    In a class named "CMenuBitmap":
    Code:
    Private Const MF_BYPOSITION = &H400&
    Private Const MF_BYCOMMAND = &H0&
    
    Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    Private Declare Function SetMenuItemBitmaps Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
    Private Declare Function GetMenuString Lib "user32" Alias "GetMenuStringA" (ByVal hMenu As Long, ByVal wIDItem As Long, ByVal lpString As String, ByVal nMaxCount As Long, ByVal wFlag As Long) As Long
    
    Option Explicit
    
    Public Function SetBitmap(ByVal hWnd As Long, ByVal TopMenuPos As Integer, ByVal OptionMenuPos As Integer, ByVal BitMapChecked As Long, ByVal BitMapUnChecked As Long) As String
        Dim lngMenuID As Long
        Dim lngSubMenuID As Long
        Dim tmp As String
         
        lngMenuID = GetMenu(hWnd)
         
        If lngMenuID > 0 Then
            lngSubMenuID = GetSubMenu(lngMenuID, TopMenuPos)
            If lngSubMenuID > 0 Then
                If SetMenuItemBitmaps(lngSubMenuID, OptionMenuPos, MF_BYPOSITION, BitMapChecked, BitMapUnChecked) <> 1 Then
                    SetBitmap = "Unable to set bitmap"
                End If
            Else
                SetBitmap = "No Sub menus"
            End If
        Else
            SetBitmap = "No Menus"
        End If
    ' Create buffer
        tmp = Space( 128 )
        GetMenuString lngSubMenuID, OptionMenuPos, tmp, 127, MF_BYPOSITION
    
        SetBitmap = Trim(tmp)
    End Function
    Last edited by Cimperiali; July 18th, 2003 at 03:18 AM.

  4. #4
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110

  5. #5
    Join Date
    May 2003
    Location
    Newyork
    Posts
    50
    dinesh.. ur code is cool.. but it shows only pics..
    is there any way to show both caption and bmp on menus..

    thanks in advance..

  6. #6
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    in Form

    Code:
    Private Sub Form_Load()
      hMenu& = GetMenu(Form1.hWnd)
    
    'Get the handle of the first submenu (Hello)
        hSubMenu& = GetSubMenu(hMenu&, 0)
    
    'Get the menuId of the first entry (Bitmap)
       hID& = GetMenuItemID(hSubMenu&, 0)
    
    'Add the bitmap
    SetMenuItemBitmaps hMenu&, hID&, MF_BITMAP, _
    picFace(0).Picture, _
    picFace(0).Picture
    End Sub
    in a Module


    Code:
    Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
    Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    
    Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    
    Declare Function SetMenuItemBitmaps Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
    
    Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
    
    Public Const MF_BITMAP = &H4&
    Public Const MIIM_ID = &H2
    Public Const MIIM_TYPE = &H10
    Public Const MFT_STRING = &H0&
    
    Type MENUITEMINFO
        cbSize As Long
        fMask As Long
        fType As Long
        fState As Long
        wID As Long
        hSubMenu As Long
        hbmpChecked As Long
        hbmpUnchecked As Long
        dwItemData As Long
        dwTypeData As String
        cch As Long
    End Type
    
    Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
    
    Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal b As Boolean, lpMenuItemInfo As MENUITEMINFO) As Boolean
    Few changes to earlier one
    Last edited by Cimperiali; July 18th, 2003 at 03:17 AM.

  7. #7
    Join Date
    Jun 2003
    Location
    philippines
    Posts
    109

    thanks

    thanks so much guys for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center