Что нового

[Элементы GUI] Вызвать ContextMenu программно.

cfvecm

Новичок
Сообщения
3
Репутация
0
Хочу чтобы при запуске скрипта появлялось сразу ContextMenu (созданное в скрипте). Желательно без эмуляции мыши. Возможно ли такое?
 

MnM

Post-Hardcore
Сообщения
679
Репутация
90
cfvecm
Код:
#include <GUIConstantsEx.au3>
#include <GUIMenu.au3>
#include <WinAPI.au3>
$gui = GUICreate("", 280, 148)
$Button = GUICtrlCreateButton("Button", 88, 40, 75, 25)
$hMenu=GUICtrlCreateContextMenu($Button)
$menuit=GUICtrlCreateMenuItem("Hello",$hMenu)
GUISetState(@SW_SHOW)
Sleep(1000)
Global $x=100,$y=50
__Coord($x,$y)
_GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($hMenu),$gui,$x,$y)
While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $menuit
			MsgBox(64,"","Hello Hell")
		Case $GUI_EVENT_CLOSE
			Exit
	EndSwitch
WEnd
Func __Coord(ByRef $iX,ByRef $iY)
	Local $tP=DllStructCreate("int x; int y")
	DllStructSetData($tP,"x",$iX)
	DllStructSetData($tP,"y",$iY)
	_WinAPI_ClientToScreen($gui,$tP)
	$iX=DllStructGetData($tP,"x")
	$iY=DllStructGetData($tP,"y")
EndFunc
 
Автор
C

cfvecm

Новичок
Сообщения
3
Репутация
0
А без BUTTON можно? Хотя и такой вариант меня устраивает. Спасибо.
 

MnM

Post-Hardcore
Сообщения
679
Репутация
90
cfvecm
Конечно можно и без кнопки, например элемент пустышку:
Код:
#include <GUIConstantsEx.au3>
#include <GUIMenu.au3>
#include <WinAPI.au3>
$gui = GUICreate("", 280, 148)
$Dummy = GUICtrlCreateDummy()
$hMenu=GUICtrlCreateContextMenu($Dummy)
$menuit=GUICtrlCreateMenuItem("Hello",$hMenu)
GUISetState(@SW_SHOW)
Sleep(1000)
Global $x=100,$y=55
__Coord($x,$y)
_GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($hMenu),$gui,$x,$y)
While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $menuit
			MsgBox(64,"","Hello Hell")
		Case $GUI_EVENT_CLOSE
			Exit
	EndSwitch
WEnd
Func __Coord(ByRef $iX,ByRef $iY)
	Local $tP=DllStructCreate("int x; int y")
	DllStructSetData($tP,"x",$iX)
	DllStructSetData($tP,"y",$iY)
	_WinAPI_ClientToScreen($gui,$tP)
	$iX=DllStructGetData($tP,"x")
	$iY=DllStructGetData($tP,"y")
EndFunc
 

AZJIO

Меценат
Меценат
Сообщения
2,879
Репутация
1,194
cfvecm
На офсайте взял месяц назад, ссылку искать лень...
Код:
; $aItems[0] is the number of the items
; "" = separator
Local $aItems[6] = [5, "Hello!", "AutoIt Script", "Popup", "", "Cancel"]
$iSel = _Popup($aItems)

MsgBox(64, "Selection", $aItems[$iSel])
Exit

Func _Popup($aItems, $hWnd = 0, $iX = Default, $iY = Default)
    ; Get the handle of the AutoIt window if $hWnd is 0
    Local $hWndBg
    If IsHWnd($hWnd) Then
        $hWndBg = $hWnd
    Else
        $hWndBg = WinGetHandle(AutoItWinGetTitle())
    EndIf
    
    If IsKeyword($iX) Then $iX = MouseGetPos(0)
    If IsKeyword($iY) Then $iY = MouseGetPos(1)
    
    Local $hPopup = DllCall("user32.dll", "handle", "CreatePopupMenu")
    If @error Then Return SetError(1, 0, 0)
    $hPopup = $hPopup[0]
    
    For $i = 1 To $aItems[0]
        Local $iFlag = 0    ; MF_STRING = 0x00000000
        If $aItems[$i] == "" Then $iFlag = 0x00000800   ; MF_SEPARATOR = 0x00000800
        
        ; Actually, InsertMenuItem is better than AppendMenu
        Local $aRet = DllCall("user32.dll", "bool", "AppendMenuW", "handle", $hPopup, "uint", $iFlag, "uint", $i, "wstr", $aItems[$i])
        If @error Then Return SetError(2, $i, 0)
    Next
    
    ; 0x0182 = TPM_NONOTIFY + TPM_RETURNCMD + TPM_RIGHTBUTTON
    Local $aSel = DllCall("user32.dll", "bool", "TrackPopupMenuEx", "handle", $hPopup, "uint", 0x0182, "int", $iX, "int", $iY, "hwnd", $hWndBg, "ptr", 0)
    If @error Then Return SetError(3, 0, 0)
    
    Local $aRet = DllCall("user32.dll", "bool", "DestroyMenu", "handle", $hPopup)
    If @error Then Return SetError(4, 0, 0)
    
    Return $aSel[0]
EndFunc ;==>_Popup
 
Верх