Sign in to follow this  
Followers 0
VAN0

WM_MENUCOMMAND doesn't work?

10 posts in this topic

Hello.

The _GUICtrlMenu_CreateMenu documentation states

_GUICtrlMenu_CreateMenu ( [$iStyle = 8] )

$iStyle 32 - Menu owner receives a WM_MENUCOMMAND message instead of a WM_COMMAND message for selections

So I tried modify the example provided to use WM_MENUCOMMAND: 

#include <GuiMenu.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $iMemo
Global Enum $idNew = 1000, $idOpen, $idSave, $idExit, $idCut, $idCopy, $idPaste, $idAbout

Example()

Func Example()
    Local $hGUI, $hFile, $hEdit, $hHelp, $hMain

    ; Create GUI
    $hGUI = GUICreate("Menu", 400, 300)

    ; Create File menu
    $hFile = _GUICtrlMenu_CreateMenu(8+32)
    _GUICtrlMenu_InsertMenuItem($hFile, 0, "&New", $idNew)
    _GUICtrlMenu_InsertMenuItem($hFile, 1, "&Open", $idOpen)
    _GUICtrlMenu_InsertMenuItem($hFile, 2, "&Save", $idSave)
    _GUICtrlMenu_InsertMenuItem($hFile, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hFile, 4, "E&xit", $idExit)

    ; Create Edit menu
    $hEdit = _GUICtrlMenu_CreateMenu(8+32)
    _GUICtrlMenu_InsertMenuItem($hEdit, 0, "&Cut", $idCut)
    _GUICtrlMenu_InsertMenuItem($hEdit, 1, "C&opy", $idCopy)
    _GUICtrlMenu_InsertMenuItem($hEdit, 2, "&Paste", $idPaste)

    ; Create Help menu
    $hHelp = _GUICtrlMenu_CreateMenu(8+32)
    _GUICtrlMenu_InsertMenuItem($hHelp, 0, "&About", $idAbout)

    ; Create Main menu
    $hMain = _GUICtrlMenu_CreateMenu(8+32)
    _GUICtrlMenu_InsertMenuItem($hMain, 0, "&File", 0, $hFile)
    _GUICtrlMenu_InsertMenuItem($hMain, 1, "&Edit", 0, $hEdit)
    _GUICtrlMenu_InsertMenuItem($hMain, 2, "&Help", 0, $hHelp)

    ; Set window menu
    _GUICtrlMenu_SetMenu($hGUI, $hMain)

    ; Create memo control
    $iMemo = GUICtrlCreateEdit("", 2, 2, 396, 276, 0)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    GUIRegisterMsg($WM_MENUCOMMAND, "WM_MENUCOMMAND")

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

; Handle menu commands
Func WM_MENUCOMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $ilParam
    Switch _WinAPI_LoWord($iwParam)
        Case $idNew
            MemoWrite("New")
        Case $idOpen
            MemoWrite("Open")
        Case $idSave
            MemoWrite("Save")
        Case $idExit
            Exit
        Case $idCut
            MemoWrite("Cut")
        Case $idCopy
            MemoWrite("Copy")
        Case $idPaste
            MemoWrite("Paste")
        Case $idAbout
            MemoWrite("About")
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MENUCOMMAND

; Write message to memo
Func MemoWrite($sMessage)
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite
But it doesn't work, nothing happens when any menu item clicked.

What I'm doing wrong?

Thanks.

Share this post


Link to post
Share on other sites



It appears that your program doesn't see that the menu item has been clicked.

I'm not very good with GUIs, but this video may help.


If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Share this post


Link to post
Share on other sites

Thanks for the reply.

The built-in functions work fine, no problem there, it's the UDF functions that seems don't work properly...

Share this post


Link to post
Share on other sites

The documented flags are wrong. You need to apply MNS_NOTIFYBYPOS to the header (main) menu ("MNS_NOTIFYBYPOS is a menu header style and has no effect when applied to individual sub menus.").

Also note: "The WM_MENUCOMMAND message gives you a handle to the menu—so you can access the menu data in the MENUINFO structure—and also gives you the index of the selected item, which is typically what applications need. In contrast, the WM_COMMAND message gives you the menu item identifier."

#include <GuiMenu.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
 
Global $iMemo
Global Enum $idNew = 1000, $idOpen, $idSave, $idExit, $idCut, $idCopy, $idPaste, $idAbout
 
Example()
 
Func Example()
Local $hGUI, $hFile, $hEdit, $hHelp, $hMain
 
; Create GUI
$hGUI = GUICreate("Menu", 400, 300)
 
; Create File menu
$hFile = _GUICtrlMenu_CreateMenu(8 + 32)
ConsoleWrite(@error & @CRLF)
_GUICtrlMenu_InsertMenuItem($hFile, 0, "&New", $idNew)
_GUICtrlMenu_InsertMenuItem($hFile, 1, "&Open", $idOpen)
_GUICtrlMenu_InsertMenuItem($hFile, 2, "&Save", $idSave)
_GUICtrlMenu_InsertMenuItem($hFile, 3, "", 0)
_GUICtrlMenu_InsertMenuItem($hFile, 4, "E&xit", $idExit)
 
; Create Edit menu
$hEdit = _GUICtrlMenu_CreateMenu(8 + 32)
_GUICtrlMenu_InsertMenuItem($hEdit, 0, "&Cut", $idCut)
_GUICtrlMenu_InsertMenuItem($hEdit, 1, "C&opy", $idCopy)
_GUICtrlMenu_InsertMenuItem($hEdit, 2, "&Paste", $idPaste)
 
; Create Help menu
$hHelp = _GUICtrlMenu_CreateMenu(8 + 32)
_GUICtrlMenu_InsertMenuItem($hHelp, 0, "&About", $idAbout)
 
; Create Main menu
$hMain = _GUICtrlMenu_CreateMenu($MNS_NOTIFYBYPOS)
_GUICtrlMenu_InsertMenuItem($hMain, 0, "&File", 0, $hFile)
_GUICtrlMenu_InsertMenuItem($hMain, 1, "&Edit", 0, $hEdit)
_GUICtrlMenu_InsertMenuItem($hMain, 2, "&Help", 0, $hHelp)
 
; Set window menu
_GUICtrlMenu_SetMenu($hGUI, $hMain)
 
; Create memo control
$iMemo = GUICtrlCreateEdit("", 2, 2, 396, 276, 0)
GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
GUISetState(@SW_SHOW)
 
; Loop until the user exits.
GUIRegisterMsg($WM_MENUCOMMAND, "WM_MENUCOMMAND")
 
; Loop until the user exits.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>Example
 
; Handle menu commands
Func WM_MENUCOMMAND($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $ilParam
 
ConsoleWrite($hWnd & @TAB & $iMsg & @TAB & $iwParam & @TAB & $ilParam & @CRLF)
ConsoleWrite(_GUICtrlMenu_GetItemText($ilParam, $iwParam) & @CRLF)
 
Switch _WinAPI_LoWord($iwParam)
Case $idNew
MemoWrite("New")
Case $idOpen
MemoWrite("Open")
Case $idSave
MemoWrite("Save")
Case $idExit
Exit
Case $idCut
MemoWrite("Cut")
Case $idCopy
MemoWrite("Copy")
Case $idPaste
MemoWrite("Paste")
Case $idAbout
MemoWrite("About")
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MENUCOMMAND
 
; Write message to memo
Func MemoWrite($sMessage)
GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite
1 person likes this

Share this post


Link to post
Share on other sites

That's it!

Thank you very much!

Share this post


Link to post
Share on other sites

KaFu,

The documented flags are wrong

Let me know how they should read and I will change the documentation. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

KaFu,

Danke - I will get the 2 pages aligned tomorrow. :)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

A little correction, the WM_MENUCOMMAND works, however the switch condition doesn't, it needs replace

_WinAPI_LoWord($iwParam)
with

_GUICtrlMenu_GetItemID($ilParam, $iwParam)

Share this post


Link to post
Share on other sites

KaFu,

_GUICtrlMenu_CreateMenu, _GUICtrlMenu_CreatePopup & _GUICtrlMenu_SetMenuStyle now all aligned with the same style values. :)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0