Results 1 to 12 of 12

Thread: Menu icon from Resource file

  1. #1

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    176

    Question Menu icon from Resource file

    Hi there,

    Is there a way to load Icons/Bitmaps from embedded Resource for Menu items?

    Currently I'm using SetMenuItemBitmaps API with PictureBox arrays like this:
    Code:
    Call MenuIconAdd(Me, 0, 0, picMnu(0).Picture.Handle)
    Call MenuIconAdd(Me, 0, 1, picMnu(1).Picture.Handle, picMnu(1).Picture.Handle)
    Thanks!

  2. #2
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    685

    Re: Menu icon from Resource file

    check this post shows how to load bitmaps into a picture box from a res file
    https://www.vbforums.com/showthread....-resource-file

  3. #3

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    176

    Re: Menu icon from Resource file

    Quote Originally Posted by BenJones View Post
    check this post shows how to load bitmaps into a picture box from a res file
    https://www.vbforums.com/showthread....-resource-file
    Hi Ben,

    Yes, I know that, but I wish to load Picture or Icon from the Resource directly into the Menu (item) with API (without using PictureBox or any other Handler control).

  4. #4
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    685

    Re: Menu icon from Resource file

    I think I remmber an API call in wndows were you chould load a picture into a handle ex HDC then you chould use that to draw the image using bitblt or something I did it my self for a few things before but were my examples are god only knows since I not used vb6 in about 6 years now hope you get it sorted just found this I duno it it will help any

    https://www.codeproject.com/Messages...B-with-GDI-Api

  5. #5
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,941

    Re: Menu icon from Resource file

    Quote Originally Posted by beic View Post
    Hi there,

    Is there a way to load Icons/Bitmaps from embedded Resource for Menu items?

    Currently I'm using SetMenuItemBitmaps API with PictureBox arrays like this:
    Code:
    Call MenuIconAdd(Me, 0, 0, picMnu(0).Picture.Handle)
    Call MenuIconAdd(Me, 0, 1, picMnu(1).Picture.Handle, picMnu(1).Picture.Handle)
    Thanks!
    You should upload a complete source code example project, what is MenuIconAdd?

  6. #6
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,941

    Re: Menu icon from Resource file

    Code:
      Set Picture1.Picture = LoadResPicture(101, vbResBitmap)
      lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, Picture1.Picture, Picture1.Picture)
      
      Set Picture2.Picture = LoadResPicture(102, vbResBitmap)
      lRet = SetMenuItemBitmaps(sHandle, 1, MF_BYPOSITION, Picture2.Picture, Picture2.Picture)
    Code:
        Static pic1 As StdPicture
        Static pic2 As StdPicture
        Set pic1 = LoadResPicture(101, vbResBitmap)
        lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, pic1.Handle, pic1.Handle)
        
        Set pic2 = LoadResPicture(102, vbResBitmap)
        lRet = SetMenuItemBitmaps(sHandle, 1, MF_BYPOSITION, pic2.Handle, pic2.Handle)
    Last edited by xiaoyao; May 22nd, 2024 at 10:18 AM.

  7. #7
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,941

    Re: Menu icon from Resource file

    Code:
        Static pic1 As StdPicture
        Static pic2 As StdPicture
        If App.LogMode Then 'EXE
            Dim m_hIcon As Long
            m_hIcon = LoadBitmap(App.hInstance, 101)
            lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, m_hIcon, m_hIcon)
            m_hIcon = LoadBitmap(App.hInstance, 102)
            lRet = SetMenuItemBitmaps(sHandle, 1, MF_BYPOSITION, m_hIcon, m_hIcon)
          Else
          
        Set pic1 = LoadResPicture(101, vbResBitmap)
        lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, pic1.Handle, pic1.Handle)
        
        Set pic2 = LoadResPicture(102, vbResBitmap)
        lRet = SetMenuItemBitmaps(sHandle, 1, MF_BYPOSITION, pic2.Handle, pic2.Handle)
        End If

  8. #8
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,941

    Re: Menu icon from Resource file

    Private Const IMAGE_BITMAP = 0
    Private Const LR_SHARED = &H8000&
    Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As Long, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long


    Code:
             m_hIcon = LoadImage(App.hInstance, 101, IMAGE_BITMAP, 80&, 80&, LR_SHARED) '
            lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, m_hIcon, m_hIcon)
            
            m_hIcon = LoadImage(App.hInstance, 102, IMAGE_BITMAP, 80&, 80&, LR_SHARED) '
            lRet = SetMenuItemBitmaps(sHandle, 1, MF_BYPOSITION, m_hIcon, m_hIcon)

  9. #9

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    176

    Re: Menu icon from Resource file

    Quote Originally Posted by xiaoyao View Post
    You should upload a complete source code example project, what is MenuIconAdd?
    Hi xiaoyao, thanks for your support, yes, here it is, also tried to combine a few things, but the IDE part is not working.

    Code:
    Public Sub MenuIconAdd(objFRM As Object, lgMenu As Long, lgSubMenu As Long, lgResId1 As Long, Optional lgResId2 As Long = 0)
    
     Dim hMenu As Long
     Dim hSubMenu As Long
     Dim hBmp1 As Long
     Dim hBmp2 As Long
    
     hMenu = GetMenu(objFRM.hwnd)
     hSubMenu = GetSubMenu(hMenu, lgMenu)
     
     If App.LogMode Then ' EXE
      hBmp1 = LoadImage(App.hInstance, lgResId1, IMAGE_BITMAP, 0&, 0&, LR_LOADTRANSPARENT Or LR_CREATEDIBSECTION Or LR_SHARED)
      
      If lgResId2 > 0 Then
       hBmp2 = LoadImage(App.hInstance, lgResId2, IMAGE_BITMAP, 0&, 0&, LR_LOADTRANSPARENT Or LR_CREATEDIBSECTION Or LR_SHARED)
      Else
       hBmp2 = hBmp1
      End If
    
      Call SetMenuItemBitmaps(hSubMenu, lgSubMenu, MF_BYPOSITION, hBmp1, hBmp2)
           
     Else ' IDE
      Static pic1 As StdPicture
      Static pic2 As StdPicture
     
      Set pic1 = LoadResPicture(lgResId1, vbResBitmap)
      
      If lgResId2 > 0 Then
       Set pic2 = LoadResPicture(lgResId2, vbResBitmap)
      Else
       Set pic2 = pic1
      End If
    
      Call SetMenuItemBitmaps(hSubMenu, lgSubMenu, MF_BYPOSITION, pic1.Handle, pic2.Handle)
        
     End If
    
     Exit Sub
    End Sub
    I have BITMAP's located in the Resource from 200 and ICON's from 300, they are all in 256 color (the icons too but with transparent background).

    I tried IMAGE_ICON to load, but without success.
    Last edited by beic; May 23rd, 2024 at 04:04 AM.

  10. #10
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,941

    Re: Menu icon from Resource file

    you can add ico,or png format

  11. #11

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    176

    Re: Menu icon from Resource file

    Quote Originally Posted by xiaoyao View Post
    you can add ico,or png format
    How? (and without using ImageList control)
    Last edited by beic; May 23rd, 2024 at 04:43 AM.

  12. #12

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    176

    Re: Menu icon from Resource file

    Quote Originally Posted by beic View Post
    Hi xiaoyao, thanks for your support, yes, here it is, also tried to combine a few things, but the IDE part is not working.

    Code:
    Public Sub MenuIconAdd(objFRM As Object, lgMenu As Long, lgSubMenu As Long, lgResId1 As Long, Optional lgResId2 As Long = 0)
    
     Dim hMenu As Long
     Dim hSubMenu As Long
     Dim hBmp1 As Long
     Dim hBmp2 As Long
    
     hMenu = GetMenu(objFRM.hwnd)
     hSubMenu = GetSubMenu(hMenu, lgMenu)
     
     If App.LogMode Then ' EXE
      hBmp1 = LoadImage(App.hInstance, lgResId1, IMAGE_BITMAP, 0&, 0&, LR_LOADTRANSPARENT Or LR_CREATEDIBSECTION Or LR_SHARED)
      
      If lgResId2 > 0 Then
       hBmp2 = LoadImage(App.hInstance, lgResId2, IMAGE_BITMAP, 0&, 0&, LR_LOADTRANSPARENT Or LR_CREATEDIBSECTION Or LR_SHARED)
      Else
       hBmp2 = hBmp1
      End If
    
      Call SetMenuItemBitmaps(hSubMenu, lgSubMenu, MF_BYPOSITION, hBmp1, hBmp2)
           
     Else ' IDE
      Static pic1 As StdPicture
      Static pic2 As StdPicture
     
      Set pic1 = LoadResPicture(lgResId1, vbResBitmap)
      
      If lgResId2 > 0 Then
       Set pic2 = LoadResPicture(lgResId2, vbResBitmap)
      Else
       Set pic2 = pic1
      End If
    
      Call SetMenuItemBitmaps(hSubMenu, lgSubMenu, MF_BYPOSITION, pic1.Handle, pic2.Handle)
        
     End If
    
     Exit Sub
    End Sub
    I have BITMAP's located in the Resource from 200 and ICON's from 300, they are all in 256 color (the icons too but with transparent background).

    I tried IMAGE_ICON to load, but without success.
    Also, I just noticed that the mentioned code will create a sort of "memory leak" and the main Form will deform after a while, without any error message whatsoever.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width