Jump to content

no message when selecting menu


Rantanplan
 Share

Recommended Posts

I have the problem that my application stops when selecting a menu (disabled or enabled - does no matter)

In sample code there is a menu, which is disabled after pressing "OK". By selecting the disabled "File"-menu or the enabled "Help"-menu during running of the Function "_doSomething(), the execution just stops. I want to know now how it is possible to get an information that a menu (NOT a Menuitem!) was selected.

Can anybody help me with this topic?

Thx

Rantanplan

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)
Global $randomLabel,$filemenu

_Main()

Func _Main()
    Local $fileitem, $recentfilesmenu, $separator1
    Local $exititem, $helpmenu, $aboutitem, $okbutton, $cancelbutton
    Local $msg, $file
    #forceref $separator1

    GUICreate("GUI menu", 300, 200)

    $filemenu = GUICtrlCreateMenu("File")
    $fileitem = GUICtrlCreateMenuItem("Open...", $filemenu)
    $recentfilesmenu = GUICtrlCreateMenu("Recent Files", $filemenu)
    $separator1 = GUICtrlCreateMenuItem("", $filemenu)
    $exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
    $helpmenu = GUICtrlCreateMenu("?")
    $aboutitem = GUICtrlCreateMenuItem("About", $helpmenu)

    $okbutton = GUICtrlCreateButton("OK", 50, 130, 70, 20)

    $cancelbutton = GUICtrlCreateButton("Cancel", 180, 130, 70, 20)

    $randomLabel = GUICtrlCreateLabel("XX",50,50)

    GUISetState()
    While 1
        $msg = GUIGetMsg()


        Select
            Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton
                ExitLoop

            Case $msg = $fileitem
                $file = FileOpenDialog("Choose file...", @TempDir, "All (*.*)")
                If @error <> 1 Then GUICtrlCreateMenuItem($file, $recentfilesmenu)

            Case $msg = $exititem
                ExitLoop

            Case $msg = $okbutton
                _doSomething()

            Case $msg = $aboutitem
                MsgBox(0, "About", "GUI Menu Test")
        EndSelect
    WEnd

    GUIDelete()

    Exit
EndFunc   ;==>_Main


Func _doSomething()
    Local $i,$n
    GUICtrlSetState($filemenu,$GUI_DISABLE)
    For $i = 1 To 50 Step 1
        $n = Random(1,100,1)
        GUICtrlSetData($randomLabel,$n)
        Sleep(300)
    Next
    GUICtrlSetState($filemenu,$GUI_ENABLE)
EndFunc
Link to comment
Share on other sites

i didnt get your point

can you explain more so i can help you

thanks and i'm waiting your reply

Hello cobako,

just compile the script and run it. By pressing "OK" you will see some numbers appear. While those numbers change, select the disabled menu "File". You will see that the numbers are not changing any more. The focus is stolen. If you click on the number which is displayed now, the focus is taken back to the "number-changing-prog".

Now I just want to know, if there is any chance to get the event, that the menu is chosen.

Thx

Rantanplan

Link to comment
Share on other sites

my edit will Notify u when menu clicked

here you are

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
Opt('MustDeclareVars', 1)
Global $randomLabel,$filemenu

_Main()

Func _Main()
    Local $fileitem, $recentfilesmenu, $separator1
    Local $exititem, $helpmenu, $aboutitem, $okbutton, $cancelbutton
    Local $msg, $file
    #forceref $separator1

    GUICreate("GUI menu", 300, 200)

    $filemenu = GUICtrlCreateMenu("File")
    $fileitem = GUICtrlCreateMenuItem("Open...", $filemenu)
    $recentfilesmenu = GUICtrlCreateMenu("Recent Files", $filemenu)
    $separator1 = GUICtrlCreateMenuItem("", $filemenu)
    $exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
    $helpmenu = GUICtrlCreateMenu("?")
    $aboutitem = GUICtrlCreateMenuItem("About", $helpmenu)

    $okbutton = GUICtrlCreateButton("OK", 50, 130, 70, 20)

    $cancelbutton = GUICtrlCreateButton("Cancel", 180, 130, 70, 20)

    $randomLabel = GUICtrlCreateLabel("XX",50,50)

    GUISetState()
    GUIRegisterMsg(0x0211,"WM_ENTERMENULOOP")
    While 1
        $msg = GUIGetMsg()


        Select
            Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton
                ExitLoop

            Case $msg = $fileitem
                $file = FileOpenDialog("Choose file...", @TempDir, "All (*.*)")
                If @error <> 1 Then GUICtrlCreateMenuItem($file, $recentfilesmenu)

            Case $msg = $exititem
                ExitLoop

            Case $msg = $okbutton
                _doSomething()

            Case $msg = $aboutitem
                MsgBox(0, "About", "GUI Menu Test")
        EndSelect
    WEnd

    GUIDelete()

    Exit
EndFunc   ;==>_Main


Func _doSomething()
    Local $i,$n
    GUICtrlSetState($filemenu,$GUI_DISABLE)
    For $i = 1 To 50 Step 1
        $n = Random(1,100,1)
        GUICtrlSetData($randomLabel,$n)
        Sleep(300)
    Next
    GUICtrlSetState($filemenu,$GUI_ENABLE)
EndFunc
Func WM_ENTERMENULOOP()
    MsgBox(0,"Menu","Menu Clicked")
EndFunc

if you want to get notify when an menu has been exited ,use this code

GUIRegisterMsg(0x0212,"WM_EXITMENULOOP")

Func WM_EXITMENULOOP()
    MsgBox(0,"Menu","Menu has been exited")
EndFunc
Link to comment
Share on other sites

  • Moderators

Rantanplan,

You can tell the menu has been clicked by registering the WM_ENTERMENULOOP message and then reset the focus to the main GUI like this: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>

Opt('MustDeclareVars', 1)
Global $randomLabel,$filemenu, $hGUI, $fMenuDisable = False

_Main()

Func _Main()
    Local $fileitem, $recentfilesmenu, $separator1
    Local $exititem, $helpmenu, $aboutitem, $okbutton, $cancelbutton
    Local $msg, $file
    #forceref $separator1

    $hGUI = GUICreate("GUI menu", 300, 200)

    $filemenu = GUICtrlCreateMenu("File")
    $fileitem = GUICtrlCreateMenuItem("Open...", $filemenu)
    $recentfilesmenu = GUICtrlCreateMenu("Recent Files", $filemenu)
    $separator1 = GUICtrlCreateMenuItem("", $filemenu)
    $exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
    $helpmenu = GUICtrlCreateMenu("?")
    $aboutitem = GUICtrlCreateMenuItem("About", $helpmenu)

    $okbutton = GUICtrlCreateButton("OK", 50, 130, 70, 20)

    $cancelbutton = GUICtrlCreateButton("Cancel", 180, 130, 70, 20)

    $randomLabel = GUICtrlCreateLabel("XX",50,50)

    GUISetState()
    GUIRegisterMsg(0x0211,"_WM_ENTERMENULOOP") ; WM_ENTERMENULOOP
    While 1
        $msg = GUIGetMsg()


        Select
            Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton
                ExitLoop

            Case $msg = $fileitem
                $file = FileOpenDialog("Choose file...", @TempDir, "All (*.*)")
                If @error <> 1 Then GUICtrlCreateMenuItem($file, $recentfilesmenu)

            Case $msg = $exititem
                ExitLoop

            Case $msg = $okbutton
                _doSomething()

            Case $msg = $aboutitem
                MsgBox(0, "About", "GUI Menu Test")
        EndSelect
    WEnd

    GUIDelete()

    Exit
EndFunc   ;==>_Main


Func _doSomething()
    Local $i,$n
    $fMenuDisable = True
    For $i = 1 To 50 Step 1
        $n = Random(1,100,1)
        GUICtrlSetData($randomLabel,$n)
        Sleep(300)
    Next
    $fMenuDisable = False
EndFunc

Func _WM_ENTERMENULOOP()
    ConsoleWrite("Menu Clicked" & @CRLF)
    If $fMenuDisable Then ControlFocus($hGUI, "", $randomLabel)
EndFunc

Please ask if you have any questions. :)

M23

Edit: I see cobako beat me to it. :shocked:

But please do NOT use a MsgBox in a registered message handler. ;) As it says in the Help file:

"Warning: blocking of running user functions which executes window messages with commands such as "Msgbox()" can lead to unexpected behavior, the return to the system should be as fast as possible !!!"

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

wakillon,

He want "get an information that a menu (NOT a Menuitem!) was selected."

Which is exactly what the script does. ;)

your solution doesn't precise which menu is selected!

But that was not part of the question! :)

The OP wanted to prevent the menu bar stealing focus from the main GUI and stopping the running function. By looking to see if the $fMenuDisable flag is set, the script resets the focus to the main GUI when the menu bar is clicked and the function is running. This prevents the function from pausing. Once the function is terminated, the menu bar behaves as normal.

Anyway, let us see if the OP is happy with what he has got so far. If not, then you can delve further into MSDN to try and find how to get the specific menu item which was clicked! ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

Hi again,

If anyone is interested, I have namaged to solve the "which menu item was pressed" problem: ;)

#include <GUIConstantsEx.au3>
#Include <GuiMenu.au3>

$hGUI = GUICreate("Test", 500, 500)

$mFilemenu = GUICtrlCreateMenu("File")
$mExititem = GUICtrlCreateMenuItem("Exit", $mFilemenu)
$mSpecialitem = GUICtrlCreateMenu("Special")
$mHelpmenu = GUICtrlCreateMenu("?")
$mAboutitem = GUICtrlCreateMenuItem("About", $mHelpmenu)

GUISetState()

$hMenu = _GUICtrlMenu_GetMenu($hGUI)
$iCount = _GUICtrlMenu_GetItemCount($hMenu) - 1

GUIRegisterMsg(0x0211,"_WM_ENTERMENULOOP") ; WM_ENTERMENULOOP

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $mExititem
            Exit
        Case $mAboutitem
            MsgBox(0, "Solved", "That was hard work!")
    EndSwitch

WEnd

Func _WM_ENTERMENULOOP($hWnd, $iMsg, $wParam, $lParam)

    For $i = 0 To $iCount

        Local $tRect = _GUICtrlMenu_GetItemRectEx($hGUI, $hMenu, $i)
        Local $aMousePos = MouseGetPos()
        Local $aRes = DllCall("User32.dll", "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $aMousePos[0], "int", $aMousePos[1])
        If Not @error And $aRes[0] Then
            ConsoleWrite("You clicked: " & _GUICtrlMenu_GetItemText($hMenu, $i) & @CRLF)
            ExitLoop
        EndIf

    Next

EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...