Jump to content
Sign in to follow this  

Context Menu Title & colors

Recommended Posts


Hi people, and happy new year for all of you,

anyone have a clue on adding a title to a Context menu in order to differentitate it from another identical context menu in the same program and/or changing the background color?

I copy below a help script I was just using for testing. As GUICtrlSetColor & GUICtrlSetBkColor doesn't do the magic I attempted to use GUICtrlCreateLabel but it doesn't paint the context menu area but the maingui one.

Thanks in advance for any help and regards.

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

Local $hGui = GUICreate("My GUI", 170, 40)

Local $idOptionsBtn = GUICtrlCreateButton("&Options", 10, 10, 70, 20, $BS_FLAT)

; At first create a dummy control for the options and a contextmenu for it
$idOptionsDummy = GUICtrlCreateDummy()
$idOptionsContext = GUICtrlCreateContextMenu($idOptionsDummy)

GUICtrlCreateLabel("", 1, 1, 10, 10)
GUICtrlSetBkColor(-1, 0x99d9EA) ;cyan
GUICtrlSetState(-1, $GUI_DISABLE)

GUICtrlCreateMenuItem("Common", $idOptionsContext)
GUICtrlCreateMenuItem("File", $idOptionsContext)
GUICtrlCreateMenuItem("", $idOptionsContext)
Local $idOptionsExit = GUICtrlCreateMenuItem("Exit", $idOptionsContext)

Local $idHelpBtn = GUICtrlCreateButton("&Help", 90, 10, 70, 20, $BS_FLAT)

; Create a dummy control and a contextmenu for the help too
Local $idHelpDummy = GUICtrlCreateDummy()
Local $idHelpContext = GUICtrlCreateContextMenu($idHelpDummy)
GUICtrlCreateMenuItem("Website", $idHelpContext)
GUICtrlCreateMenuItem("", $idHelpContext)
Local $idHelpAbout = GUICtrlCreateMenuItem("About...", $idHelpContext)


Local $idMsg

; Loop until the user exits.
While 1
    $idMsg = GUIGetMsg()

    Switch $idMsg
        Case $idOptionsExit, $GUI_EVENT_CLOSE

        Case $idOptionsBtn
            ShowMenu($hGui, $idMsg, $idOptionsContext)

        Case $idHelpBtn
            ShowMenu($hGui, $idMsg, $idHelpContext)

        Case $idHelpAbout
            MsgBox($MB_SYSTEMMODAL, "About...", "GUICtrlGetHandle-Sample")

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $idCtrl, $idContext)
    Local $aPos, $x, $y
    Local $hMenu = GUICtrlGetHandle($idContext)

    $aPos = ControlGetPos($hWnd, "", $idCtrl)

    $x = $aPos[0]
    $y = $aPos[1] + $aPos[3]

    ClientToScreen($hWnd, $x, $y)
    TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc   ;==>ShowMenu

; Convert the client (GUI) coordinates to screen (desktop) coordinates
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
    Local $tPoint = DllStructCreate("int;int")

    DllStructSetData($tPoint, 1, $x)
    DllStructSetData($tPoint, 2, $y)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($tPoint))

    $x = DllStructGetData($tPoint, 1)
    $y = DllStructGetData($tPoint, 2)
    ; release Struct not really needed as it is a local
    $tPoint = 0
EndFunc   ;==>ClientToScreen

; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc   ;==>TrackPopupMenu

Share this post

Link to post
Share on other sites

From the help, a little edited:

#include <GUIConstantsEx.au3>

GUICreate("My GUI", 300, 200)

Local $idFileMenu = GUICtrlCreateContextMenu()
GUICtrlCreateMenuItem("&Open", $idFileMenu)
GUICtrlCreateMenuItem("&Save", $idFileMenu)
GUICtrlCreateMenuItem("", $idFileMenu)
Local $idOptionsMenu = GUICtrlCreateMenu("O&ptions", $idFileMenu)
GUICtrlCreateMenuItem("View", $idOptionsMenu)
GUICtrlCreateMenuItem("", $idOptionsMenu)
GUICtrlCreateMenuItem("Tools", $idOptionsMenu)
GUICtrlCreateMenuItem("", $idFileMenu)
Local $idExitItem = GUICtrlCreateMenuItem("&Exit", $idFileMenu)

Local $idEndBtn = GUICtrlCreateButton("End", 110, 140, 70, 20)

SetMenuColor($idFileMenu, 0xEEBB99) ; BGR color value
SetMenuColor($idOptionsMenu, 0x66BB99); BGR color value


While 1
    $idMsg = GUIGetMsg()
    Switch $idMsg
        Case $idExitItem, $idEndBtn, $GUI_EVENT_CLOSE

; Apply the color to the menu
Func SetMenuColor($nMenuID, $nColor)
    Local Const $MIM_APPLYTOSUBMENUS = 0x80000000
    Local Const $MIM_BACKGROUND = 0x00000002
    Local $hMenu = GUICtrlGetHandle($nMenuID)
    Local $hBrush = DllCall("gdi32.dll", "hwnd", "CreateSolidBrush", "int", $nColor)
    $hBrush = $hBrush[0]
    Local $tMenuInfo = DllStructCreate("dword;dword;dword;uint;dword;dword;ptr")
    DllStructSetData($tMenuInfo, 1, DllStructGetSize($tMenuInfo))
    DllStructSetData($tMenuInfo, 2, BitOR($MIM_APPLYTOSUBMENUS, $MIM_BACKGROUND))
    DllStructSetData($tMenuInfo, 5, $hBrush)
    DllCall("user32.dll", "int", "SetMenuInfo", "hwnd", $hMenu, "ptr", DllStructGetPtr($tMenuInfo))
    $tMenuInfo = 0 ; release Struct not really needed as it is a local
EndFunc   ;==>SetMenuColor

Or use >GUI/Tray Menu with icons and colors

Edited by johnmcloud

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.