Add icon to menu



Introduction
Here I am me again, with other programming trick. VB doesn't provide any support to icons in the menus (VB 6 and previous). Then, to accomplish such task it is necessary to run over to calls native API of Windows.
Purpose
Some programmers say that that is unnecessary. But it is common the users they to use the toolbar in most of the cases. It is interesting that he can associate the menu options the entrances in the toolbar.

For an application to be easy to use, it should be the most friendliest possible and not to demand of the user, complex reasoning.

The people use computers to activate your tasks and to win time.

The road of the stones
The best way to accomplish this task, would be with a visual atmosphere. Where you could choose in the list of available menus the item of wanted menu and in other (parallel) the wanted icon (what is made by other tools).

However, I will teach the less friendly, however functional way of the you make it.

API
Collapse
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

Const MF_BYPOSITION = &H400&

Code Piece

Collapse
Private Sub SetMenuIcon()
On Error Resume Next

Dim hMenu As Long

Dim hSubMenu As Long

Dim Ret As Long

'Get main menu ID


hMenu = GetMenu(hwnd)


'


'MENU FILE


'Get sub menu 0 (File items)


hSubMenu = GetSubMenu(hMenu, 0)


'set bitmap to menu item, by ordinal


Ret = SetMenuItemBitmaps(hSubMenu, 0, MF_BYPOSITION, iNew.Picture, iNew.Picture)

Ret = SetMenuItemBitmaps(hSubMenu, 1, MF_BYPOSITION, iOpen.Picture, iOpen.Picture)

Ret = SetMenuItemBitmaps(hSubMenu, 2, MF_BYPOSITION, iSave.Picture, iSave.Picture)

'Skip the separator (it's 3)


Ret = SetMenuItemBitmaps(hSubMenu, 4, MF_BYPOSITION, iSave.Picture, iSave.Picture)

'Skip the separator (it's 5)


Ret = SetMenuItemBitmaps(hSubMenu, 6, MF_BYPOSITION, iExit.Picture, iExit.Picture)

' The rest of the code was removed to maintain clear. See in the file 'ZIP the complete and functional example.


End Sub

The icon may be 14x15 pixels.
If you have some doubt or suggestion, order an email.

Contact
E-mail: willians@bb.com.br
MSN: willian_cpp_br@hotmail.com
ICQ# 89506722
Phone: +55 (64) 612-6030
Fax: +55 (64) 612-6010

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS
Read Comments

1 comments:

digital signature software said...

Hi there! glad to drop by your page and found these very interesting and informative stuff. Thanks for sharing, keep it up!