 |
| VB How-To Ask your "How do I do this with VB?" questions in this forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 21st, 2005, 02:39 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Images in menus
Is it possible to add images to my existing vb6 menus like you can find in most of microsoft's programs?
|
|

April 21st, 2005, 02:52 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Yes. It requires the use of APIs to do so though. (The means to do this is not contained in VB, but VB will link to the Windows libraries which can.)
|
|

April 22nd, 2005, 02:53 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanx Brian, I found three functions that can do what I want. Three public functions called SetMenuBitmapImage, GetMenu, GetSubMenu.
|
|

April 22nd, 2005, 07:10 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
can u send me that public functions so that i can also include images in my menu based applications
|
|

April 22nd, 2005, 07:25 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sure ...
The code is as follows. I simplified it abit to only work on a single menu-submenu set but you can easily modify it to work on more than one.
Open a new project and add a single menu to you project using the Menu Editor. In the Caption of the initial menu type in "Menu" and call it "mnuOne", then add a submenu to "Menu" and call it "mnuTwo". Then go into code view and type the follow:
Option Explicit
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 (single line)
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long (single line)
' Constant for SetMenuItemBitmaps
Private Const MF_BYPOSITION = &H400&
' Constant for LoadImage
Private Const IMAGE_BITMAP = &O0
Private Const LR_LOADFROMFILE = 16
Private Const LR_CREATEDIBSECTION = 8192
Private Sub Form_Load()
Dim hMenu As Long
Dim hSubMenu As Long
Dim hMenuImg As Long
Dim sFileName As String
' Get the bitmap
sFileName = App.Path & "\Icons\program.bmp"
hMenuImg = LoadImage(0, sFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE Or LR_CREATEDIBSECTION)
' Get the menu item handle
hMenu = GetMenu(Me.hwnd)
hSubMenu = GetSubMenu(hMenu, 0)
' Set the "mnuTwo" bitmap to the one that is loaded in memory
Call SetMenuItemBitmaps(hSubMenu, 3, MF_BYPOSITION, hMenuImg, 0)
End Sub
This article was originally posted on vbrun - microsoft
|
|

April 22nd, 2005, 11:07 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Were there specifics on the properties of the bitmaps that are usable?
32 Ã 32 only?
16 Ã 16 only?
Color resolution limits?
|
|

April 23rd, 2005, 03:09 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ah, sorry Brian! The image size should be 10x10 bitmap, color depth does not matter although I use grayscale for better quality. This has been updated in Visual Basic 8 / .Net which allows you to use 24-bit true color images.
Hope the script helps
|
|

April 25th, 2005, 06:27 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The code is not displaying the menu image for me.
I am getting the menu but no image.
The menu icon I used is the floppyicon .
Why is it not working for me?
|
|

April 25th, 2005, 09:04 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi JelfMaria ... it might be because I'm referring to a folder called \icons\ on my c-drive. Make sure that you are referring to the exact path to where you are storing your image and not that you are using my path!
If it still does not work just post your script so I can see what might be going wrong.
|
|

April 26th, 2005, 12:40 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You are mentioning only app.path so i think it wont be a problem and i stored Icons folder in app.path.
|
|
 |