Jump to content

Problem using menu


GeekIT
 Share

Go to solution Solved by Melba23,

Recommended Posts

Hi everyone  :bye:

I have a quick question about menus in autoit:

Is it normal that I can't use menu's in forms that don't have sub-menu's?

example:

$Form = GUICreate("Test.", 300, 200)
$Menu = GUICtrlCreateMenu("Menu")
While 1
Switch GUIGetMsg()
    Case $Menu
        MsgBox(0,'never works','never works!');    <--- why won't this work?

EndSwitch

Link to comment
Share on other sites

Welcome to AutoIt and the forum!

Did you check the example that can be found in the help file for function GUICtrlCreateMenu?

At least line

GUISetState()

is missing.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

BTW: Could you please enclose your code in AutoIt code tags (the blue "A" icon in the editor). Enhances readability :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

GeekIT,

You're also missing the WEND stmt to close the while loop.  Try it like this...

#include <GUIConstantsEx.au3>

$Form = GUICreate("Test.", 300, 200)

$Menu = GUICtrlCreateMenu("Menu")
$Menumsg1 = GUICtrlCreateMenuItem('Message # 1', $Menu)
$Menumsg2 = GUICtrlCreateMenuItem('Message # 2', $Menu)
GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Menumsg1
            MsgBox(0, 'never works', 'never works!');    <--- why won't this work?
        Case $Menumsg2
            MsgBox(0, '', 'Of course it works' & @crlf & 'Read the HELP file.');    <--- why won't this work?
    EndSwitch

WEnd

See the Help file for explanation...

kylomas

edit: spelling

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

I assume the missing WEnd statement is just a copy&paste mistake :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

GeekIT,

I can get as far as recognising the click on the menu bar. But as yet I cannot get out of the blocking menu loop - you need to press {ESCAPE} to reenter the script and get the dummy to fire: :(

#include <GUIConstantsEx.au3>
#include <Constants.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)

$cMenuDummy = GUICtrlCreateDummy()

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 $cMenuDummy
            MsgBox($MB_SYSTEMMODAL, "Hi", "I can be actioned!")
        Case $mAboutitem
            MsgBox($MB_SYSTEMMODAL, "Solved", "That was hard work!")
    EndSwitch

WEnd

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

    If $hWnd = $hGUI Then
        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)
                GUICtrlSendToDummy($cMenuDummy)
                ExitLoop
            EndIf
        Next
    EndIf

EndFunc
I will keep working on it this afternoon. In the mean time if you have any questions, please ask. :)

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

GeekIT,

You're also missing the WEND stmt to close the while loop.

 

I know, was just explaining my problem. I want to use a menu without sub menu's.

I want it so that you just have to click on the menu button itself and then do something...

Edited by GeekIT
Fixed tags
Link to comment
Share on other sites

GeekIT,

I can get as far as recognising the click on the menu bar. But as yet I cannot get out of the blocking menu loop - you need to press {ESCAPE} to reenter the script and get the dummy to fire: :(

I will keep working on it this afternoon. In the mean time if you have any questions, please ask. :)

 

Thanks but you don't have to do all that for me (:

I just wanted to know if this was normal. thanks for the help!

Edited by Melba23
Fixed tags
Link to comment
Share on other sites

  • Moderators
  • Solution

GeekIT,

Jos came up with a solution: :)

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

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

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

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $mExititem
            Exit
        Case $mSpecialitem
            MsgBox($MB_SYSTEMMODAL, "Hi", "I can be actioned!")
        Case $mAboutitem
            MsgBox($MB_SYSTEMMODAL, "Solved", "Thank Jos for that!")
    EndSwitch

WEnd
M23

Edit: And so did wraithdu:

#include <GUIConstantsEx.au3>
#include <Constants.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)

$cMenuDummy = GUICtrlCreateDummy()

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 $cMenuDummy
            MsgBox($MB_SYSTEMMODAL, "Hi", "I can be actioned!")
        Case $mAboutitem
            MsgBox($MB_SYSTEMMODAL, "Solved", "That was hard work!")
    EndSwitch

WEnd

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

    If $hWnd = $hGUI Then
        For $i = 0 To $iCount

            Local $tRect = _GUICtrlMenu_GetItemRectEx($hGUI, $hMenu, $i)
            Local $aMousePos = MouseGetPos()
            Local $tPOINT = DllStructCreate($tagPOINT)
            $tPOINT.x = $aMousePos[0]
            $tPOINT.y = $aMousePos[1]
            Local $aRes = DllCall("User32.dll", "int", "PtInRect", "struct*", $tRect, "struct", $tPOINT)
            If Not @error And $aRes[0] Then
                ConsoleWrite("You clicked: " & _GUICtrlMenu_GetItemText($hMenu, $i) & @CRLF)
                GUICtrlSendToDummy($cMenuDummy)
                ControlClick($hGUI, "", $hGUI)
                Return 0
            EndIf
        Next
    EndIf

EndFunc
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

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...