我有一个自动脚本,其中我从系统托盘菜单条目中打开一个设置对话框。当以这种方式打开对话框时,通过按钮点击的消息不会处理。
另一方面,直接打开对话框时(如下面的代码中所示,您可以通过删除此通话来轻松测试并评论系统托盘条目的呼叫),然后成功处理消息。<<<<<<<<<<<<<<
这是我的脚本。直接调用SettingsDialog
(无需通过Systray菜单)时,确定和取消按钮可以工作,但否则不行。
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
;; Start program in system tray
SetupSystemTrayEntry()
;; When calling settings dialog directly, messages are handled properly
;;SettingsDialog()
Func SetupSystemTrayEntry()
Opt("TrayMenuMode", 1)
$settingsitem = TrayCreateItem("Settings")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")
TraySetState()
While 1
Local $traymsg = TrayGetMsg()
Select
Case $traymsg = 0
ContinueLoop
Case $traymsg = $settingsitem
SettingsDialog() ;; Bring up settings dialog
Case $traymsg = $exititem
Exit ;; Exit program
EndSelect
WEnd
EndFunc
Func SettingsDialog()
GUICreate("Settings", 400, 150, @DesktopWidth / 2 - 200, @DesktopHeight / 2 - 75)
$ok_button = GUICtrlCreateButton("OK", 100, 100, 80, 25, $BS_DEFPUSHBUTTON)
$cancel_button = GUICtrlCreateButton("Cancel", 200, 100, 80, 25)
GUISetState()
Do
;; These messages are never handled when the dialog is brought up from
;; the system tray menu entry above, but when calling this function
;; directly, it works
Local $settmsg = GUIGetMsg()
Select
Case $settmsg = $ok_button
ExitLoop
Case $settmsg = $cancel_button
ExitLoop
EndSelect
Until $settmsg = $GUI_EVENT_CLOSE
EndFunc
看起来都很好,除了您离开DO/直到循环后应该做某事。
Func SettingsDialog()
GUICreate("Settings", 400, 150, @DesktopWidth / 2 - 200, @DesktopHeight / 2 - 75)
$ok_button = GUICtrlCreateButton("OK", 100, 100, 80, 25, $BS_DEFPUSHBUTTON)
$cancel_button = GUICtrlCreateButton("Cancel", 200, 100, 80, 25)
GUISetState()
Do
;; These messages are never handled when the dialog is brought up from
;; the system tray menu entry above, but when calling this function
;; directly, it works
Local $settmsg = GUIGetMsg()
ConsoleWrite(@HOUR & ":" & @MIN & ":" & @SEC & "," & @MSEC & "msg = " & $settmsg & @CRLF)
Select
Case $settmsg = $ok_button
ExitLoop
Case $settmsg = $cancel_button
ExitLoop
EndSelect
Until $settmsg = $GUI_EVENT_CLOSE
Return GUIDelete()
Endfunc
我添加了2行,ConsoleWrite
在拨打GuigetMessage和循环后Return
。
当我运行此脚本时,通过单击"确定"或"取消"的设置对话框。
根据自动论坛上提供的解决方案,这是脚本的一个版本,它隐藏并在每个用法上显示对话框。有一个很大的警告(请阅读论坛线程以获取详细信息),这是关于对话框的创建,必须明确隐藏!
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
; Do not declare Global variables in a function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $settings_window_handle, $ok_button, $cancel_button
;; Set up settings dialog
InitSettingsDialog();
;; Start program in system tray
SetupSystemTrayEntry()
Func SetupSystemTrayEntry()
Opt("TrayMenuMode", 1)
$settingsitem = TrayCreateItem("Settings")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")
TraySetState()
While 1
Switch TrayGetMsg()
Case $settingsitem
ShowSettingsDialog() ;; Bring up settings dialog
Case $exititem
Exit ;; Exit program
EndSwitch
WEnd
EndFunc ;==>SetupSystemTrayEntry
Func InitSettingsDialog()
; Create
$settings_window_handle = GUICreate("Settings", 400, 150, @DesktopWidth / 2 - 200, @DesktopHeight / 2 - 75)
$ok_button = GUICtrlCreateButton("OK", 100, 100, 80, 25, $BS_DEFPUSHBUTTON)
$cancel_button = GUICtrlCreateButton("Cancel", 200, 100, 80, 25)
GUISetState(@SW_HIDE, $settings_window_handle)
EndFunc ;==>SettingsDialog
Func ShowSettingsDialog()
GUISetState(@SW_SHOW, $settings_window_handle)
While 1
Switch GUIGetMsg()
Case $ok_button
MsgBox(0, "test", "test")
ExitLoop
Case $cancel_button
ExitLoop
EndSwitch
WEnd
GUISetState(@SW_HIDE, $settings_window_handle)
EndFunc