Adding a Menu to a UserForm !

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,621
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I have been experimenting with this rather unusual area and found some difficulties.

The code below successfully adds a Menu Bar to a UserForm.It then adds a PopUp Menu Item to it and Two SubMenu items to the newly created PopUp Menu item.

The Two SubMenu Items are supposed to display a simple MsgBox each upon clicking on them.

The problem I am having is that these MsgBoxes appear when hovering the Mouse Cursor over the Submenu Items instead of appearing when clicking on them.

Any suggestions on how I could fix this would be much appreciated.

Ah, another thing : I would love to add like a FaceId or another Graphic to the SubMenus as well but I have been unsuccessful so far. :unsure:



Code in a Standard Module :

<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> FindWindow <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "FindWindowA" _
(<SPAN style="color:#00007F">ByVal</SPAN> lpClassName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lpWindowName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> CreateMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" () <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> CreatePopupMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" () <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> AppendMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "AppendMenuA" _
(<SPAN style="color:#00007F">ByVal</SPAN> hMenu <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wFlags <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wIDNewItem <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lpNewItem <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SetMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" (<SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> hMenu <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> DestroyMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" (<SPAN style="color:#00007F">ByVal</SPAN> hMenu <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SetWindowLong <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "SetWindow<SPAN style="color:#00007F">Long</SPAN>A" _
(<SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> nIndex <SPAN style="color:#00007F">As</SPAN> Long, <SPAN style="color:#00007F">ByVal</SPAN> dwNew<SPAN style="color:#00007F">Long</SPAN> <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> CallWindowProc Lib _
"user32" Alias "CallWindowProcA" (<SPAN style="color:#00007F">ByVal</SPAN> lpPrevWndFunc <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
ByVal Msg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CopyMemory <SPAN style="color:#00007F">Lib</SPAN> "kernel32" Alias "RtlMoveMemory" _
(Destination <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Source <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, ByVal Length <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>)

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> MF_POPUP = &H10
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> MF_STRING = &H0
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> WM_MENUSELECT = &H11F
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> GWL_WNDPROC = -4

<SPAN style="color:#00007F">Public</SPAN> hForm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Public</SPAN> hMenu <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Public</SPAN> hPopUpMenu <SPAN style="color:#00007F">As</SPAN> Long
<SPAN style="color:#00007F">Public</SPAN> lpPrevWndProc <SPAN style="color:#00007F">As</SPAN> Long
<SPAN style="color:#00007F">Public</SPAN> Rt <SPAN style="color:#00007F">As</SPAN> Long


    <SPAN style="color:#007F00">' Get the PopUpMenu index stored in the low integer of wParam</SPAN>
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Function</SPAN> LOWORD(dw <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    CopyMemory LOWORD, VarPtr(dw), 2
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>


<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Function</SPAN> CustomWinProc(<SPAN style="color:#00007F">ByVal</SPAN> hw <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> uMsg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
ByVal wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, ByVal lParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

    <SPAN style="color:#007F00">' Catch the MenuSelect message and display MsgBoxes</SPAN>
    <SPAN style="color:#00007F">If</SPAN> uMsg = WM_MENUSELECT And LOWORD(wParam) <> 0 <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Hello !    You Selected PopUp  " & LOWORD(wParam)
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    
    <SPAN style="color:#007F00">' Reroute all messages to native window procedure</SPAN>
    CustomWinProc = CallWindowProc _
    (lpPrevWndProc, hw, uMsg, wParam, lParam)

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>
</FONT>


Code in the UserForm Class Module :

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()

    <SPAN style="color:#007F00">' Retrieve the Form Handle</SPAN>
    hForm = FindWindow(vbNullString, Me.Caption)
    
    <SPAN style="color:#007F00">' Create Menu and PopUp</SPAN>
    hMenu = CreateMenu()
    hPopUpMenu = CreatePopupMenu()
    
    <SPAN style="color:#007F00">' Add Them To The Form</SPAN>
    Rt = AppendMenu(hPopUpMenu, MF_STRING, 1, "PopUp 1")
    Rt = AppendMenu(hPopUpMenu, MF_STRING, 2, "PopUp 2")
    Rt = AppendMenu(hMenu, MF_POPUP, hPopUpMenu, "Jaafar")
    Rt = SetMenu(hForm, hMenu)
    
    <SPAN style="color:#007F00">' Subclass The Form</SPAN>
    lpPrevWndProc = SetWindowLong(hForm, GWL_WNDPROC, AddressOf CustomWinProc)

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Terminate()

    <SPAN style="color:#007F00">' Clean up</SPAN>
    DestroyMenu hMenu
    SetWindowLong hForm, GWL_WNDPROC, lpPrevWndProc
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>


Regards.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I have example workbooks on doing this using 2 methods

1) API activated with Right click on userform
2) using native commandbars, so you can set the graphics etc.

let me know if you want it...... it is from a project I have yet to post to my site...
 
Upvote 0
Ivan F Moala said:
I have example workbooks on doing this using 2 methods

1) API activated with Right click on userform
2) using native commandbars, so you can set the graphics etc.

let me know if you want it...... it is from a project I have yet to post to my site...



Yes please to my Email address.

Thanks very much.
 
Upvote 0
Hi again,

I seem to have a problem receiving Mails sent from this board. :x
I Have sent a couple of test Emails to myself using the Email Button below but received nothing in my Yahoo inbox !

I am not sure why this is happening !
 
Upvote 0
I am now trying to add a Bitmap Picture to the first PopUp Menu Item(entry) but when I display the Form and Click on the Menu, the first entry is Blank instead of it displaying the Bitmap !

The second entry works OK and displays the string PopUp 2 as expected.

I have even tried other API functions like ModifyMenu and GetMenuItemID but without any luck.

I don't know what I am missing but the sequence of code lines seems logical to me.

By the way, Ivan, I didn't receive the workbook samples you mentioned.Did you send them or is it my email which is not working ?


Here is the amended Code that caters for the Bitmap picture:


1) In the Standard Module:


<font face=Courier New> <SPAN style="color:#007F00">' APIs And Constantes Declarations</SPAN>
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> FindWindow <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "FindWindowA" _
(<SPAN style="color:#00007F">ByVal</SPAN> lpClassName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lpWindowName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> CreateMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" () <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> CreatePopupMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" () <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> AppendMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "AppendMenuA" _
(<SPAN style="color:#00007F">ByVal</SPAN> hMenu <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wFlags <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wIDNewItem <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lpNewItem <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SetMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" (<SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> hMenu <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> DestroyMenu <SPAN style="color:#00007F">Lib</SPAN> "user32" (<SPAN style="color:#00007F">ByVal</SPAN> hMenu <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SetWindowLong <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "SetWindow<SPAN style="color:#00007F">Long</SPAN>A" _
(<SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> nIndex <SPAN style="color:#00007F">As</SPAN> Long, <SPAN style="color:#00007F">ByVal</SPAN> dwNew<SPAN style="color:#00007F">Long</SPAN> <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> CallWindowProc Lib _
"user32" Alias "CallWindowProcA" (<SPAN style="color:#00007F">ByVal</SPAN> lpPrevWndFunc <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
ByVal Msg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CopyMemory <SPAN style="color:#00007F">Lib</SPAN> "kernel32" Alias "RtlMoveMemory" _
(Destination <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Source <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Length <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>)

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> LoadImage <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "LoadImageA" _
(<SPAN style="color:#00007F">ByVal</SPAN> hInst <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lpsz <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, ByVal un1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
<SPAN style="color:#00007F">ByVal</SPAN> n1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, ByVal n2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, ByVal un2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>


<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> MF_POPUP = &H10
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> MF_STRING = &H0
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> WM_MENUSELECT = &H11F
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> GWL_WNDPROC = -4
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> MF_BITMAP = &H4&
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> IMAGE_BITMAP = 0
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> LR_LOADFROMFILE = &H10
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> BMP_Full_Path = "C:\Pretty.bmp"  <SPAN style="color:#007F00">' BitMap File</SPAN>


<SPAN style="color:#00007F">Public</SPAN> hForm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Public</SPAN> hMenu <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Public</SPAN> hPopUpMenu <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Public</SPAN> lpPrevWndProc <SPAN style="color:#00007F">As</SPAN> Long
<SPAN style="color:#00007F">Public</SPAN> Rt <SPAN style="color:#00007F">As</SPAN> Long
<SPAN style="color:#00007F">Public</SPAN> hBMP <SPAN style="color:#00007F">As</SPAN> Long


    <SPAN style="color:#007F00">' Get the PopUpMenu index stored in the low integer of wParam</SPAN>
<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Function</SPAN> LOWORD(dw <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    CopyMemory LOWORD, VarPtr(dw), 2
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>


<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Function</SPAN> CustomWinProc(<SPAN style="color:#00007F">ByVal</SPAN> hw <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> uMsg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
ByVal wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, ByVal lParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

    <SPAN style="color:#007F00">' Catch the MenuSelect message and display MsgBoxes</SPAN>
    <SPAN style="color:#00007F">If</SPAN> uMsg = WM_MENUSELECT And LOWORD(wParam) <> 0 <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Hello !    You Selected PopUp  " & LOWORD(wParam)
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    
    <SPAN style="color:#007F00">' Reroute all messages to native window procedure</SPAN>
    CustomWinProc = CallWindowProc _
    (lpPrevWndProc, hw, uMsg, wParam, lParam)

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>
</FONT>



2) In The Form Class Module:


<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()

     <SPAN style="color:#007F00">' Retrieve the Form Handle</SPAN>
    hForm = FindWindow(vbNullString, Me.Caption)
    
     <SPAN style="color:#007F00">' Create Menu and PopUp</SPAN>
    hMenu = CreateMenu()
    hPopUpMenu = CreatePopupMenu()
    
     <SPAN style="color:#007F00">' Get The Handle To A BitMap File</SPAN>
    hBMP = LoadImage(0, BMP_Full_Path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)

     <SPAN style="color:#007F00">' Append The BitMap To The First PopUp SubMenu Item</SPAN>
     <SPAN style="color:#007F00">' This Doesn't Seem To Work !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</SPAN>
    Rt = AppendMenu(hPopUpMenu, MF_BITMAP, 1, hBMP)
    
     <SPAN style="color:#007F00">' Append The String " PopUp 2 " To The Second PopUp SubMenu Item</SPAN>
    Rt = AppendMenu(hPopUpMenu, MF_STRING, 2, "PopUp 2")
    
     <SPAN style="color:#007F00">' Add Menu To The Form</SPAN>
    Rt = AppendMenu(hMenu, MF_POPUP, hPopUpMenu, "Jaafar")
    Rt = SetMenu(hForm, hMenu)
    
     <SPAN style="color:#007F00">' Subclass The Form</SPAN>
    lpPrevWndProc = SetWindowLong(hForm, GWL_WNDPROC, AddressOf CustomWinProc)

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Terminate()

    <SPAN style="color:#007F00">' Clean up</SPAN>
    DestroyMenu hPopUpMenu
    DestroyMenu hMenu
    SetWindowLong hForm, GWL_WNDPROC, lpPrevWndProc
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>


Regards.
 
Upvote 0
hello,

Given sample is working fine. but i need on click event. this is invoking msgbox when mouse move over it..

can i get how i can get the moveclick on the menu items.

thanks in advance
Sudhakar :)
 
Upvote 0
It is the WM_COMMAND that you need to Monitor

eg WM_COMMAND And (wParam And &HFFFF&) <> 0

Note: (wParam And &HFFFF&) yields the same result that you have for the lowbyte function.

Also for BMP images.....
1) Images need to be 13 X 13 Pixels or a least for me.
2) Image index positions are zero based ie 1st is 0 and not 1
 
Upvote 0
Hi
Excellent solution to a problem I've ben having BUT when I click to open the menu, it always returns the message that I selected Popup 1, i.e. the mouse will not go past the first option - is there a way that the click event can be used instead of hover over?
Secondly, how do I add more than one menu? and also how do i attach the appropriate routines to each item of the menu?
Any help gratefully received
Regards
Martin
 
Upvote 0
Hi martin

I will have a solution up on my site soon .......unless you want to get one now ... then email me.
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,375
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top