Jump to content

Handling Top Menu Items as Clickable Buttons


RTFC
 Share

Recommended Posts

If you don't want the standard drop-down menu's from the top menu bar, here's a simple way to catch user clicks there, so you end up with a top button bar instead, without defining actual buttons. No handling is provided other than item ID written to console. Took me about a full day of forum grazing to cobble it together.:D

; edited Helpfile example script for _GUICtrlMenu_CreateMenu
#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WinAPIConv.au3>
#include <WindowsConstants.au3>

Example()

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

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

    ; Create menu items
    $hFile = _GUICtrlMenu_CreateMenu()
    $hEdit = _GUICtrlMenu_CreateMenu()
    $hHelp = _GUICtrlMenu_CreateMenu()

    ; Create Main menu
    $hMain = _GUICtrlMenu_CreateMenu()
    _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
    $g_idMemo = GUICtrlCreateEdit("", 2, 2, 396, 276, 0)
    GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
    GUISetState(@SW_SHOW)

    ; ADDED LINES   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    GUIRegisterMsg( $WM_MENUSELECT, "WM_MENUSELECT_Events")
    GUIRegisterMsg( $WM_ENTERMENULOOP, "WM_ENTERMENULOOP" )
    Global $hGuiHandle=WinGetHandle("","")  ; get GUI handle too

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

EndFunc   ;==>Example

;==========================================================================
; ADDED

Func WM_MENUSELECT_Events($hWndGUI, $MsgID, $wParam, $lParam)
    Static $first=False
    $first=Not $first   ; to filter out the dummy click that resets detection

    Local $index = _LoWord($wParam)
    Local $flags = _HiWord($wParam)
    If $first And BitAND($flags, $MF_MOUSESELECT) Then ConsoleWrite("top menu ID: " & $index & @CRLF)

    Return $GUI_RUNDEFMSG
EndFunc


Func _HiWord($x)
    Return BitShift($x, 16)
EndFunc


Func _LoWord($x)
    Return BitAND($x, 0xFFFF)
EndFunc


Func WM_ENTERMENULOOP( $hWnd, $iMsg, $iwParam, $ilParam )
    ControlClick($hGuiHandle, "", "")   ; dummy click at menu item 0 to reset detection
    Return $GUI_RUNDEFMSG
EndFunc

 

Link to comment
Share on other sites

  • Moderators

RTFC,

If you use the native menu functions it is a little bit easier:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

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

$mFilemenu = GUICtrlCreateMenu("&File")
$mExititem = GUICtrlCreateMenuItem("E&xit", $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

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

A light in the darkness as always, Melba.^_^ None of the previous discussions I read on this topic presented this (better) solution. I think it's about time you consider uploading your AutoIt knowledge to an AI for posterity's sake (maybe with a Yoda-voiced speech synthesizer?). Who wouldn't want a pocket-sized Melba sitting on their shoulder, whispering snazzy GUI solutions in one's ear? It would have saved me from writing this Rube-Goldberg contraption.;)

I guess you'd need a dummy control if all menu items are to function as buttons :

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

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

$mDummy = GUICtrlCreateMenu("")
$mSpecialitem = GUICtrlCreateMenuItem("&Special", -1)
$mSpecialitem2 = GUICtrlCreateMenuItem("&Special2", -1)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $mSpecialitem
            MsgBox($MB_SYSTEMMODAL, "Hi", "I can be actioned!")
        Case $mSpecialitem2
            MsgBox($MB_SYSTEMMODAL, "Hello", "I can ALSO be actioned!")
    EndSwitch
WEnd

 

Edited by RTFC
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

×
×
  • Create New...