Erstellt ein Kontextmenü für ein Control oder das ganze GUI-Fenster.
GUICtrlCreateContextMenu ( [controlID] )
Parameter
| ControlID | [optional] Die Control-ID des Items wie von GUICtrlCreate... zurückgegeben. |
Rückgabewert
| Erfolg: | Gibt die Identifikationsnummer (Control-ID) des neuen Controls zurück. |
| Fehler: | Gibt 0 zurück. |
Bemerkungen
Nach dem Erstellen des Hauptitems für das Kontextmenü mit dieser Funktion können die Menüeinträge einzeln mit der Funktion GUICtrlCreateMenuItem erzeugt werden. Unter-Menüs können mit GUICtrlCreateMenu erzeugt werden.
Verwandte Funktionen
GUICtrlCreateMenuItem, GUICtrlCreateMenu, GUICtrlGetHandle, GUICtrlSetState, GUICtrlDelete
Beispiel
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
Opt('MustDeclareVars', 1)
Example1()
Example2()
; *******************
; * Erstes Beispiel *
; *******************
Func Example1()
Local $contextmenu, $button, $buttoncontext, $buttonitem, $msg
Local $newsubmenu, $textitem, $fileitem, $saveitem, $infoitem
; Rechtsklick im Anwendungsbereich, um Kontextmenü anzuzeigen.
; Rechtsklick auf den "OK" Button, um ein Control-spezifisches Kontextmenü anzuzeigen.
GUICreate("Mein GUI Kontextmenü", 300, 200)
$contextmenu = GUICtrlCreateContextMenu()
$button = GUICtrlCreateButton("OK", 100, 100, 70, 20)
$buttoncontext = GUICtrlCreateContextMenu($button)
$buttonitem = GUICtrlCreateMenuItem("Über Button", $buttoncontext)
$newsubmenu = GUICtrlCreateMenu("Neu", $contextmenu)
$textitem = GUICtrlCreateMenuItem("Text", $newsubmenu)
$fileitem = GUICtrlCreateMenuItem("Öffnen", $contextmenu)
$saveitem = GUICtrlCreateMenuItem("Speichern", $contextmenu)
GUICtrlCreateMenuItem("", $contextmenu) ; Trennstrich
$infoitem = GUICtrlCreateMenuItem("Info", $contextmenu)
GUISetState()
; Die Schleife wiederholt sich, bis der Benutzer die Beenden-Aktion der GUI auslöst
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()
EndFunc ;==>Example1
; ********************
; * Zweites Beispiel *
; ********************
Func Example2()
Local $hGui, $OptionsBtn, $OptionsDummy, $OptionsContext, $OptionsCommon, $OptionsFile, $msg
Local $OptionsExit, $HelpBtn, $HelpDummy, $HelpContext, $HelpWWW, $HelpAbout
$hGui = GUICreate("Meine GUI", 170, 40)
$OptionsBtn = GUICtrlCreateButton("&Optionen", 10, 10, 70, 20, $BS_FLAT)
; Zuerst ein Dummy-Control für die Optionen und ein Kontextmenü erzeugen
$OptionsDummy = GUICtrlCreateDummy()
$OptionsContext = GUICtrlCreateContextMenu($OptionsDummy)
$OptionsCommon = GUICtrlCreateMenuItem("Allgemein", $OptionsContext)
$OptionsFile = GUICtrlCreateMenuItem("Datei", $OptionsContext)
GUICtrlCreateMenuItem("", $OptionsContext)
$OptionsExit = GUICtrlCreateMenuItem("Beenden", $OptionsContext)
$HelpBtn = GUICtrlCreateButton("&Hilfe", 90, 10, 70, 20, $BS_FLAT)
; Dann ebenso ein Dummy-Control und ein Kontextmenü für die Hilfe erzeugen
$HelpDummy = GUICtrlCreateDummy()
$HelpContext = GUICtrlCreateContextMenu($HelpDummy)
$HelpWWW = GUICtrlCreateMenuItem("Webseite", $HelpContext)
GUICtrlCreateMenuItem("", $HelpContext)
$HelpAbout = GUICtrlCreateMenuItem("Über...", $HelpContext)
GUISetState()
While 1
$msg = GUIGetMsg()
Switch $msg
Case $OptionsExit, $GUI_EVENT_CLOSE
ExitLoop
Case $OptionsBtn
ShowMenu($hGui, $msg, $OptionsContext)
Case $HelpBtn
ShowMenu($hGui, $msg, $HelpContext)
Case $HelpAbout
MsgBox(64, "Über...", "GUICtrlGetHandle-Beispiel")
EndSwitch
WEnd
GUIDelete()
EndFunc ;==>Example2
; Zeigt ein Menü zugehörig einem bestimmten GUI-Control in einem bestimmten GUI-Fenster
Func ShowMenu($hWnd, $CtrlID, $nContextID)
Local $arPos, $x, $y
Local $hMenu = GUICtrlGetHandle($nContextID)
$arPos = ControlGetPos($hWnd, "", $CtrlID)
$x = $arPos[0]
$y = $arPos[1] + $arPos[3]
ClientToScreen($hWnd, $x, $y)
TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc ;==>ShowMenu
; Passt die Clientkoordinaten (GUI) den Bildschirmkoordinaten (Desktop) an
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
Local $stPoint = DllStructCreate("int;int")
DllStructSetData($stPoint, 1, $x)
DllStructSetData($stPoint, 2, $y)
DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))
$x = DllStructGetData($stPoint, 1)
$y = DllStructGetData($stPoint, 2)
; Freigabe der Struktur nicht unbedingt nötig, solange sie lokal ist
$stPoint = 0
EndFunc ;==>ClientToScreen
; Zeigt an den angegebenen Koordinaten (x, y) das Popupmenü (hMenu) zugehörig einem bestimmten GUI-Fenster (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