Показать полную графическую версию : [решено] работа с контекстым меню
alexey_a
25-05-2010, 16:49
Всем привет! кто-нибудь знает, как с помощью AutoIt можно управлять контекстным меню (обычно это окна типа: [Class: #32768]). в частности интересует возможность управлять менюшками, которые появляютя, если кликнуть правой кнопкой мыши по какой-нить иконке в трее.
madmasles
25-05-2010, 17:49
alexey_a,
Попробуйте так. Вместо "AdMunch.exe" поставьте нужную Вам Программу. Если раскомментировать последние строки в функции, то у меня закрывает Ad Muncher.
#NoTrayIcon (http://www.autoitscript.com/autoit3/docs/keywords.htm##notrayicon)
#include (http://www.autoitscript.com/autoit3/docs/keywords.htm##include) <SysTray_UDF.au3>
$Pid = ProcessExists (http://www.autoitscript.com/autoit3/docs/functions/ProcessExists.htm)("AdMunch.exe")
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) $Pid Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $Old_Mouse_Pos = MouseGetPos (http://www.autoitscript.com/autoit3/docs/functions/MouseGetPos.htm)()
MouseMove (http://www.autoitscript.com/autoit3/docs/functions/MouseMove.htm)(@DesktopWidth (http://www.autoitscript.com/autoit3/docs/macros.htm#@desktopwidth) / 2, @DesktopHeight (http://www.autoitscript.com/autoit3/docs/macros.htm#@desktopheight), 0)
$Index = _SysTrayIconIndex($Pid)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) @error (http://www.autoitscript.com/autoit3/docs/macros.htm#@error) Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
_SysTrayIconClick($Index, "Right")
Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _SysTrayIconClick($iIndex, $sButton = "Main")
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $TrayIcon_Pos = _SysTrayIconPos($iIndex)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) IsArray (http://www.autoitscript.com/autoit3/docs/functions/IsArray.htm)($TrayIcon_Pos) Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return) SetError (http://www.autoitscript.com/autoit3/docs/functions/SetError.htm)(1, 0, 0)
MouseClick (http://www.autoitscript.com/autoit3/docs/functions/MouseClick.htm)($sButton, $TrayIcon_Pos[0], $TrayIcon_Pos[1], 1, 0)
;MouseClick("left", $TrayIcon_Pos[0] - 15, $TrayIcon_Pos[1] - 15, 1, 0)
;MouseMove($Old_Mouse_Pos[0], $Old_Mouse_Pos[1], 0)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc) ;==>_SysTrayIconClickSysTray_UDF (http://www.autoitscript.com/forum/index.php?showtopic=103494&st=0&p=733204&hl=systray_udfau3&fromsearch=1&#entry733204)
alexey_a
26-05-2010, 10:42
Спасибо, madmasles!
я тут написал немного другую функцию. она мне больше подходит, так как менюшки, с которыми я работаю, периодически меняются (количество пунктов меню, очередность). поэтому после каждого изменения для каждого пункта в меню измерять его положение относительно окошка, мне не очень хочется. вот код функции:
; #Функция# -----------------------------------------------------------------------------------------------------------------------------
; Имя....................: SelectItemFromMonitorMenu
; Описание ..............: Выбирает пункт контекстного меню для иконки вашего приложения в трее
; Параметры .............: $item - текст пункта меню (например: "Exit")
; Возвращаемые значения .: Успешно - 1
; Ошибка - 0
; Заметки ...............: $your_applications_window_title - title окошка вашего приложения, которое отображается в трее.
; Вы можете его увидеть в секции ToolsBar в AutoIt Window Info, если наведете на курсор на иконку в трее.
; -----------------------------------------------------------------------------------------------------------------------------------------
#Include <GuiToolBar.au3>
#include <GuiMenu.au3>
Func SelectItemFromMonitorMenu($item)
Opt("MouseCoordMode", 1)
$ind = 0
$hWnd = ControlGetHandle("[CLASS:Shell_TrayWnd]","","[CLASS:ToolbarWindow32; TEXT:Notification Area]")
if $hWnd = "" Then
ConsoleWrite("Can't get hwnd of bottom toolbar.")
Return 0
EndIf
$tray_icon_count = _GUICtrlToolbar_ButtonCount($hWnd)
For $i = 0 to $tray_icon_count
$command_id = _GUICtrlToolbar_IndexToCommand($hWnd, $i)
$text = _GUICtrlToolbar_GetButtonText($hWnd, $command_id)
if StringInStr($text,$your_applications_window_title) = 0 Then
ContinueLoop
Else
$index = $i
ExitLoop
EndIf
Next
_GUICtrlToolbar_ClickIndex($hWnd, $index, "right")
Sleep(100)
$cur_pos = MouseGetPos()
if IsArray($cur_pos) = 0 Then
ConsoleWrite("Can't get position of current mouse coordinate")
Return 0
EndIf
if WinExists("[Class:#32768]") = 0 Then
ConsoleWrite("Context menu is not appeared for tray icon.")
Return 0
Else
$h_win = WinGetHandle("[Class:#32768]")
if $h_win = "" Then
ConsoleWrite("Can't get hwnd for application's context menu window.")
Return 0
EndIf
$hMenu = _SendMessage($h_win, $MN_GETHMENU,0,0)
$it_index = _GUICtrlMenu_FindItem($hMenu, $item,True)
if $it_index = -1 Then
ConsoleWrite("Can't find item " & $item & " in the application's context menu window.", $fail)
Return 0
EndIf
$it_pos = _GUICtrlMenu_GetItemRect($h_win, $hMenu, $it_index)
if IsArray($it_pos) = 0 Then
ConsoleWrite("Can't get position of item " & $item & " in the application's context menu window.", $fail)
Return 0
EndIf
MouseClick("left", $it_pos[0] + ($it_pos[2]-$it_pos[0])/2, $it_pos[1] + ($it_pos[3]-$it_pos[1])/2)
EndIf
MouseMove($cur_pos[0], $cur_pos[1])
Return 1
EndFunc
madmasles
26-05-2010, 16:55
alexey_a,
У меня не работает, ругается на необъявленную переменную $your_applications_window_title, и не понял, откуда $MN_GETHMENU и $fail.
alexey_a
28-05-2010, 14:52
там же вверху написано, что:
$your_applications_window_title - title окошка вашего приложения, которое отображается в трее. Вы можете его увидеть в секции ToolsBar в AutoIt Window Info.
$MN_GETHMENU - это стандартная константа, объявленная в ...\AutoIt3\include\WindowsConstants.au3
$fail надо удалить и запятую, которая стоит перед ним. то есть заменить
ConsoleWrite("Can't find item " & $item & " in the application's context menu window.", $fail)
на
ConsoleWrite("Can't find item " & $item & " in the application's context menu window.").
Это моя ошибка. просто у меня в скриптах я использую свою функцию для логирования, а не ConsoleWrite. и так получилось,что переделывая скрипт для того, чтобы разместить на форуме, я заменял мою функцию на ConsoleWrite. и для одно замены сделал это неправильно, забыл удалить второй параметр $fail.
вот код функции: »
не понял как ей пользоваться ?
не находит приложение
в строчке :
ControlGetHandle("[CLASS:Shell_TrayWnd]","","[CLASS:ToolbarWindow32; TEXT:Notification Area]") »
менял "TEXT:Notification Area" на что попало и включал мозг,а потом снова отключал входил в нирвану но никак.
© OSzone.net 2001-2012
vBulletin v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.