Jump to content

Add a menu indicator to a button label


AndyS01
 Share

Recommended Posts

I have a button that has a context menu attached to it using GUICtrlCreateContextMenu() but I cannot figure out how to add a little black triangle to the right of the text to indicate that clicking on the button will bring up a menu.

Here is my sample code:

#include <GUIConstants.au3>
 
 
Opt("GUIOnEventMode", 1)
GUICreate("", 200, 200, 200, 200)
$button = GUICtrlCreateButton('context', 1, 1, 50, 25)
GUICtrlSetOnEvent(-1, 'handle_context_btn')
 
$context = GUICtrlCreateContextMenu($button)
GUICtrlCreateMenuItem('Sub Menu 1', $context)
GUICtrlSetOnEvent(-1, "handle_SubMenu1_click")
GUICtrlCreateMenuItem('Exit', $context)
GUICtrlSetOnEvent(-1, "handle_Exit_click")
 
GUICtrlCreateButton('close', 1, 30, 50, 25)
GUICtrlSetOnEvent(-1, 'handle_close_btn')
GUISetState()
 
While 1
Sleep(10)
WEnd
 
Func handle_close_btn()
Exit
EndFunc
 
Func handle_Exit_click()
Exit
EndFunc
 
Func handle_context_btn()
MouseClick('right')
EndFunc
 
Func handle_SubMenu1_click()
ConsoleWrite("+++: SubMenu1 pressed" & @CRLF)
EndFunc
 
Func handle_SubMenu2_click()
ConsoleWrite("+++: SubMenu2 pressed" & @CRLF)
EndFunc
Link to comment
Share on other sites

  • Moderators

AndyS01,

You need the $BS_SPLITBUTTON style to get the "menu" arrow on the button - I think it is Vista+ only.

All is explained in this thread. :graduated:

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

Thank you for the link. I added this to my test code and got the display I wanted. However, I have a problem with the notification.

If I left click on the left half of the button, I get the notification and in the handler, I simulate a right mouse button click and the drop down menu is displayed. If I left click on the right side (the black triangle), I don't get any notification. Is there a way to register a handler for the right side of the button?

Link to comment
Share on other sites

  • Moderators

AndyS01,

I do not understand your problem - why are you simulating right clicks in the handler? :graduated:

Here is the simplest script I can produce for you to test a split button. Left clicking on the button should give you "Button Pressed" in the SciTE console - left clicking on the arrow should bring up the menu and selecting the one available item should give you "Split Menu" instead. No need for any right click simulation and the menu only appears when you left click the arrow: :)

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

Opt("MustDeclareVars", 1)

Global $btn, $iMemo, $btn2

_Main()

Func _Main()

    GUICreate("Buttons", 400, 400)

    $btn = GUICtrlCreateButton("Split Button", 10, 10, 120, 30, $BS_SPLITBUTTON)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    GUISetState()

        While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $btn
                ConsoleWrite("Button Pressed" & @CRLF)
        EndSwitch
    WEnd

EndFunc   ;==>_Main

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    
    #forceref $hWnd, $Msg, $wParam
    
    Local $tNMBHOTITEM = DllStructCreate("hwnd hWndFrom;int IDFrom;int Code;dword dwFlags", $lParam)
    Local $nNotifyCode = DllStructGetData($tNMBHOTITEM, "Code")
    Local $hCtrl = DllStructGetData($tNMBHOTITEM, "hWndFrom")

    Switch $nNotifyCode
        Case $BCN_DROPDOWN
            _Popup_Menu($hCtrl)
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _Popup_Menu($hCtrl)

    Local $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Split Menu", 1000)
    Switch _GUICtrlMenu_TrackPopupMenu($hMenu, $hCtrl, -1, -1, 1, 1, 2)
        Case 1000
            ConsoleWrite("Split Menu" & @CRLF)
    EndSwitch
    _GUICtrlMenu_DestroyMenu($hMenu)

EndFunc   ;==>_Popup_Menu

Do you get the same result? ;)

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

  • 2 weeks later...

Oh-Oh, it doesn't work on XP systems. I wrote conditional code, so the buttons are normal on the XP machine and are split buttons on Windows 7 and Vista. So I still need to do something on the XP machine to indicate that the buttons have a drop down menu. I'm sure this must have been done by someone.

If ($flags <> -1) And (BitAND($flags, $BS_SPLITBUTTON)) Then
If (StringInStr(@OSVersion, "XP")) Then
$flags = BitAND($flags, BitNOT($BS_SPLITBUTTON))
EndIf
EndIf
Link to comment
Share on other sites

  • Moderators

AndyS01,

I did mention I thought it was Vista+. ;)

What about this as an alternative for XP: :graduated:

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

Global Enum $idOpen = 1000, $idSave, $idInfo

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

$hButton = GUICtrlCreateButton("XP Split", 10, 10, 60, 30)
$hSplit = GUICtrlCreateButton("6", 69, 10, 20, 30)
GUICtrlSetFont(-1, 10, 400, 0, "Webdings")

$hContextMenu = GUICtrlCreateContextMenu($hSplit)
$hContextHandle = GUICtrlGetHandle($hContextMenu)

GUICtrlCreateMenuItem("Open", $hContextMenu)
GUICtrlCreateMenuItem("Save", $hContextMenu)
GUICtrlCreateMenuItem("", $hContextMenu) ; separator
GUICtrlCreateMenuItem("Info", $hContextMenu)

GUISetState()

GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            ConsoleWrite("Button Pressed" & @CRLF)
        Case $hSplit
            ; Show the menu for a left click on the button
            _Menu()
    EndSwitch

WEnd

Func _Menu()

    $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $idOpen)
    _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $idSave)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $idInfo)
    _GUICtrlMenu_TrackPopupMenu($hMenu, $hGUI)
    _GUICtrlMenu_DestroyMenu($hMenu)

EndFunc

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

    #forceref $hWnd, $iMsg, $wParam, $lParam
    ; If the context menu was fired by a right click on the Split button then do nothing
    If $wParam = GUICtrlGetHandle($hSplit) Then
        Return True
    EndIf

EndFunc

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

    #forceref $hWnd, $iMsg, $lParam
    Switch $wParam
        Case $idOpen
            ConsoleWrite("Open" & @CRLF)
        Case $idSave
            ConsoleWrite("Save" & @CRLF)
        Case $idInfo
            ConsoleWrite("Info" & @CRLF)
    EndSwitch

EndFunc   ;==>WM_COMMAND

This way you get the context menu appearing only for a left click on the "split" button - right clicking does not fire it. :)

Not as sexy as the full $BS_SPLITBUTTON version, but XP users will not feel too deprived! ;)

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