Sign in to follow this  
Followers 0
AndyS01

How to handle sys menu button click

9 posts in this topic

Is there a way to handle a system menu pick? I'm talking about the File, Help, etc. buttons that I created.

I created a system menu Options button and I want to rebuild the sub-menu entries that get popped up when I click on the Options button.

I tried this:

$iID_Menu = GUICtrlCreateMenu("&File")
$iID_Menu = GUICtrlCreateMenu("&Options")
[b]GUICtrlSetOnEvent($iID_Menu, "handle_OptionsMenuPick")[/b]
$iID_Menu = GUICtrlCreateMenu("&Help")

but I never get to the "handle_OptionsMenuPick" handler when I click on the Options button.

Share this post


Link to post
Share on other sites



AndyS01,

I am afraid you need something a bit more complicated than GUICtrlSetOnEvent for that. :)

Here is how you add items to the sysmenu and then read clicks on them:

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

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

GUISetState()

$hSystemMenu = _GUICtrlMenu_GetSystemMenu($hGUI)
_GUICtrlMenu_GetItemCount($hSystemMenu)
_GUICtrlMenu_AppendMenu($hSystemMenu, $MF_SEPARATOR, 0, "")
_GUICtrlMenu_AppendMenu($hSystemMenu, $MF_STRING, 0x3000, "New Sysmenu Item") ; You need to set the CmdID value here

GUIRegisterMsg($WM_SYSCOMMAND, "_WM_SYSCOMMAND")

$sMenuSelection = ""

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If $sMenuSelection <> "" Then
        MsgBox(0, "New Menu", "You selected " & $sMenuSelection)
        $sMenuSelection = ""
    EndIf

WEnd

Func _WM_SYSCOMMAND($hGUI, $iMsg, $wParam, $lParam)

    If $iMsg = $WM_SYSCOMMAND Then
        If _WinAPI_LoWord($wParam) = 0x3000 Then ; So you can detect it here!
            $sMenuSelection = "New Sysmenu Item"
        EndIf
    EndIf

EndFunc

All clear? ;)

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

I'm sorry, I didn't describe it clearly.

What I want is to catch the event when the Options button is pressed on the menu just below the System menu.

Here is the sanitized code:

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

Opt("GUICloseOnESC", 0) ; ESC does not close GUI
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
Opt("GUIEventOptions", 1) ;0=default, 1=just notification, 2=GuiCtrlRead tab index

$hGUI = GUICreate("Test", 200, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft') ; CLOSE events

$filemenu    = GUICtrlCreateMenu("&File")
$fileitem    = GUICtrlCreateMenu("Open", $filemenu)
$optionsmenu = GUICtrlCreateMenu("&Options")
$sub         = GUICtrlCreateMenuItem("abc", $optionsmenu)
$helpmenu    = GUICtrlCreateMenu("&Help")

; Register a handler for when the Options menu button is clicked
GUICtrlSetOnEvent($optionsmenu, "OptionsButtonHandler")

GUISetState()

While 1
    Sleep(300)
WEnd
Exit (1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func OptionsButtonHandler()
    ConsoleWrite("+++: OptionsButtonHandler() entered" & @CRLF)
EndFunc   ;==>OptionsButtonHandler

Func ExitStageLeft()
    Exit (1)
EndFunc   ;==>ExitStageLeft_normal

When I run this, I never get to the button handler.

Andy

Share this post


Link to post
Share on other sites

AndyS01,

As far as I know you can only set events for the menuitems, not the menu titles. This is a Windows thing, not an AutoIt limitation.

Why do you want to know when the menu title is pressed? ;)

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

You're trying to fire off the optionsbuttonhandler function by clicking on the menu rather than the submenu items. I'm not sure you can do that. Are you sure you need to have it fire on clicking the Option menu header or do you want it to fire when you click on a submenu item?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

AndyS01,

A bit more searching and I found this trick: ;)

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

Opt("GUICloseOnESC", 0) ; ESC does not close GUI
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
Opt("GUIEventOptions", 1) ;0=default, 1=just notification, 2=GuiCtrlRead tab index

$hGUI = GUICreate("Test", 200, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft') ; CLOSE events

$filemenu    = GUICtrlCreateMenu("&File")
$fileitem    = GUICtrlCreateMenu("Open", $filemenu)
$optionsmenu = GUICtrlCreateMenuItem("&Options", -1) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;$sub         = GUICtrlCreateMenuItem("abc", $optionsmenu)
$helpmenu    = GUICtrlCreateMenu("&Help")

; Register a handler for when the Options menu button is clicked
GUICtrlSetOnEvent($optionsmenu, "OptionsButtonHandler")

GUISetState()

While 1
    ;
    Sleep(300)
WEnd
Exit (1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func OptionsButtonHandler()
    ConsoleWrite("+++: OptionsButtonHandler() entered" & @CRLF)
EndFunc   ;==>OptionsButtonHandler

Func ExitStageLeft()
    Exit (1)
EndFunc   ;==>ExitStageLeft_normal

It looks like a menu, but you cannot have any items in it. :)

Does this help? ;)

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

When I press Options, I want to put the current value for a log file name onto the menu as one of the choices, and I can click on it to change it.

Andy

Share this post


Link to post
Share on other sites

OK, I think I got it. I save the ID of the submenu item containing the filename and I register a handler for it. In the handler, I do a GUICtrlSetData() on the submenu ID. This changes the text of the submenu item.

Here is some test code that shows an incrementing count of the number of times the submenu item was pressed.

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

Opt("GUICloseOnESC", 0) ; ESC does not close GUI
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
Opt("GUIEventOptions", 1) ;0=default, 1=just notification, 2=GuiCtrlRead tab index

$iMasterCount = 0

$hGUI = GUICreate("Test", 200, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft') ; CLOSE events

$filemenu             = GUICtrlCreateMenu("&File")
$fileitem             = GUICtrlCreateMenu("Open", $filemenu)
$optionsmenu          = GUICtrlCreateMenu("&Options")
[b]$iFileNameSubmenuItem = GUICtrlCreateMenuItem("Press Counter = 0", $optionsmenu)[/b]
$helpmenu             = GUICtrlCreateMenu("&Help")

; Register a handler for when the submenu button is clicked
[b]GUICtrlSetOnEvent($iFileNameSubmenuItem, "Options_SubMenu_Item_Handler")[/b]

GUISetState()

While 1
    Sleep(300)
WEnd
Exit (1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func Options_SubMenu_Item_Handler()
    ConsoleWrite("+++: Options_SubMenu_Item_Handler() entered" & @CRLF)
    local $str

    $iMasterCount += 1
    $str = StringFormat("Press Counter = %d", $iMasterCount)

    [b]GUICtrlSetData($iFileNameSubmenuItem, $str)[/b]
EndFunc   ;==>OptionsButtonHandler

Func ExitStageLeft()
    Exit (1)
EndFunc   ;==>ExitStageLeft_normal

Thank you for the help.

Share this post


Link to post
Share on other sites

AndyS01,

That is a long way from "Is there a way to handle a system menu pick" - but I am delighted you got what you wanted in the end. :)

One point - "register a handler" is usually taken to mean using GUIRegisterMsg to intercept a Windows message. What you are doing here is "defining an event function". ;)

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

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