-
May 21st, 2024, 04:02 AM
#1
Thread Starter
Addicted Member
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!
-
May 21st, 2024, 04:49 AM
#2
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
-
May 21st, 2024, 06:42 AM
#3
Thread Starter
Addicted Member
Re: Menu icon from Resource file
 Originally Posted by BenJones
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).
-
May 21st, 2024, 10:54 AM
#4
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
-
May 22nd, 2024, 09:19 AM
#5
Re: Menu icon from Resource file
 Originally Posted by beic
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?
-
May 22nd, 2024, 09:54 AM
#6
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.
-
May 22nd, 2024, 10:27 AM
#7
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
-
May 22nd, 2024, 10:36 AM
#8
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)
-
May 23rd, 2024, 03:51 AM
#9
Thread Starter
Addicted Member
Re: Menu icon from Resource file
 Originally Posted by xiaoyao
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.
-
May 23rd, 2024, 04:25 AM
#10
Re: Menu icon from Resource file
you can add ico,or png format
-
May 23rd, 2024, 04:33 AM
#11
Thread Starter
Addicted Member
Re: Menu icon from Resource file
 Originally Posted by xiaoyao
you can add ico,or png format
How? (and without using ImageList control)
Last edited by beic; May 23rd, 2024 at 04:43 AM.
-
May 25th, 2024, 03:42 AM
#12
Thread Starter
Addicted Member
Re: Menu icon from Resource file
 Originally Posted by beic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|