Einmal die rechte Maustaste, das andere Mal die linke

  • Wahrscheinlich bin ich mit Blindheit geschlagen.
    In der AutoIt-Hilfe steht unter GUICtrlCreateContextMenu ein Beispiel.
    Warum muss ich im Example1 auf das OK-Button mit der rechten Maustaste klicken, um das Kontextmenü anzeigen zu lassen, aber im Example2 mit der linken Maustaste auf Options oder Help?

    Vielen Dank schon mal für eine Antwort!

  • Weil beim zweiten Beispiel durch die Funktionen der Linksklick abgefragt wird und das GUICtrlCreateContextMenu dann aufgerufen wird.

    Andy hat mir ein Schnitzel gebacken aber da war ein Raupi drauf und bevor Oscar das Bugfixen konnte kam Alina und gab mir ein AspirinJunkie.

  • Im ersten Beispiel wird das Kontextmenü direkt den sichtbaren Controls zugeordnet. Standardmäßig wird ein Kontextmenü eines Controls mit der rechten Maustaste geöffnet.
    Im zweiten Beispiel werden die beiden Kontextmenüs Dummycontrols zugeordnet und "manuell" über eine Funktion (ShowMenu) aufgerufen sobald ein Button geklickt wurde.

  • Ich hab mal Beispiel 1 umgebaut das sollte dann recht einfach zu verstehen sein. Es wurde lediglich

    [autoit]

    If $iMsg = $button Then ShowMenu($hGui, $iMsg, $buttoncontext)

    [/autoit]

    Und die drei Funktionen aus Beispiel 2 hinzugefügt.

    Spoiler anzeigen
    [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    Example()

    [/autoit] [autoit][/autoit] [autoit]

    Func Example()
    $hGui = GUICreate("My GUI Context Menu", 300, 200)

    [/autoit] [autoit][/autoit] [autoit]

    Local $contextmenu = GUICtrlCreateContextMenu()

    [/autoit] [autoit][/autoit] [autoit]

    Local $newsubmenu = GUICtrlCreateMenu("new", $contextmenu)
    GUICtrlCreateMenuItem("text", $newsubmenu)

    [/autoit] [autoit][/autoit] [autoit]

    Local $button = GUICtrlCreateButton("OK", 100, 100, 70, 20)
    Local $buttoncontext = GUICtrlCreateContextMenu($button)
    Local $MenuAbout = GUICtrlCreateMenuItem("About button", $buttoncontext)

    [/autoit] [autoit][/autoit] [autoit]

    Local $MenuOpen = GUICtrlCreateMenuItem("Open", $contextmenu)
    Local $MenuSave = GUICtrlCreateMenuItem("Save", $contextmenu)
    GUICtrlCreateMenuItem("", $contextmenu) ; separator

    [/autoit] [autoit][/autoit] [autoit]

    Local $MenuInfo = GUICtrlCreateMenuItem("Info", $contextmenu)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    ; Run the GUI until the dialog is closed
    Local $iMsg = 0
    While 1
    $iMsg = GUIGetMsg()

    [/autoit] [autoit][/autoit] [autoit]

    If $iMsg = $GUI_EVENT_CLOSE Then ExitLoop

    [/autoit] [autoit][/autoit] [autoit]

    If $iMsg = $button Then ShowMenu($hGui, $iMsg, $buttoncontext)

    [/autoit] [autoit][/autoit] [autoit]

    If $iMsg = $MenuAbout Then MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'About')
    If $iMsg = $MenuOpen Then MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Open')
    If $iMsg = $MenuSave Then MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Save')
    If $iMsg = $MenuInfo Then MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Info')
    WEnd
    GUIDelete()
    EndFunc ;==>Example

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    $arPos = ControlGetPos($hWnd, "", $CtrlID)

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    ; 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

    [/autoit]

    Jetzt bekommt bei Rechtklick das normale Menü und bei Linksklick das andere.

    Andy hat mir ein Schnitzel gebacken aber da war ein Raupi drauf und bevor Oscar das Bugfixen konnte kam Alina und gab mir ein AspirinJunkie.

  • Recht vielen Dank, chip und name22, für Eure schnellen Hinweise.
    Das muss ich jetzt noch einmal unter diesen Aspekten richtig durchdenken.
    Vielen Dank nochmals.
    Gruß Dieter