[已解决]如何在托盘菜单设置一个类似toggle(来回切换)的菜单项?
本帖最后由 痒痒 于 2009-12-31 11:54 编辑感谢jybb001 朋友,问题解决
例如下面代码中,
当主界面显示时,将托盘菜单第一项设置为“隐藏主窗口”,点击时能执行隐藏窗口操作
当主界面隐藏时,将托盘菜单第一项设置为“显示主窗口”,点击时能执行显示窗口操作#NoTrayIcon
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <Misc.au3>
Opt("TRAYMENUMODE", 1);设置运作模式
Opt("TRAYONEVENTMODE", 1)
Opt("GuiOneventmode", 1)
HotKeySet("{ESC}","_exit")
;=========== 创建GUI菜单和注册GUI和控件事件开始 ===========
$Form1 = GUICreate("", 164, 218, -1, -1, BitOR($WS_SYSMENU, $WS_CAPTION, $WS_POPUP, $WS_POPUPWINDOW, $WS_BORDER, $WS_CLIPSIBLINGS))
$Button1 = GUICtrlCreateButton("打开文件", 28, 40, 107, 33, 0)
$Button2 = GUICtrlCreateButton("关闭文件", 28, 88, 107, 33, 0)
$Button3 = GUICtrlCreateButton("最小到托盘", 28, 136, 107, 33, 0)
$Group1 = GUICtrlCreateGroup("点击按钮选择操作", 9, 9, 145, 177)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Checkbox1 = GUICtrlCreateCheckbox("随Windows自启动", 24, 192, 113, 17)
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "GuiMsg");注册gui事件
GUICtrlSetOnEvent($Button1, "GuiMsg");注册gui控件事件
GUICtrlSetOnEvent($Button2, "GuiMsg");注册gui控件事件
GUICtrlSetOnEvent($Button3, "GuiMsg");注册gui控件事件
GUICtrlSetOnEvent($Checkbox1,"GuiMsg");注册gui控件事件
;=========== 创建GUI菜单和注册GUI和控件事件结束 ===========
;=========== 创建托盘菜单和注册托盘菜单事件开始 ===========
$show = TrayCreateItem("显示主窗口")
$open = TrayCreateItem("")
$open = TrayCreateItem("打开")
$close = TrayCreateItem("关闭")
$exit = TrayCreateItem("")
$exit = TrayCreateItem("退出| ESC |");制作托盘菜单
TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "TrayMsg");注册托盘图标双击事件
TrayItemSetOnEvent($show, "TrayMsg");注册托盘菜单“显示主窗口”事件
TrayItemSetOnEvent($open, "TrayMsg");注册托盘菜单“弹出”事件
TrayItemSetOnEvent($close, "TrayMsg");注册托盘菜单“关闭”事件
TrayItemSetOnEvent($exit, "TrayMsg");注册托盘菜单“退出”事件
TraySetState(1);设置系统托盘图标状态
TraySetClick(16);设置鼠标在系统托盘图标里面的点击模式
;=========== 创建托盘菜单和注册托盘菜单事件结束 ===========
While 1
Sleep(200)
WEnd
Func GuiMsg();;GUI控件点击后的操作
Switch @GUI_CtrlId
Case $Button1
open()
Case $Button2
close()
Case $Button3, $GUI_EVENT_CLOSE
GUISetState(@SW_HIDE, $Form1)
EndSwitch
EndFunc ;==>GuiMsg
Func TrayMsg() ;托盘菜单点击后的操作
Switch @TRAY_ID
Case $open
open()
Case $close
close()
Case $show, $TRAY_EVENT_PRIMARYDOUBLE
GUISetState(@SW_SHOW, $Form1)
TrayItemSetText($show,"隐藏主窗口")
Case $exit
Exit
EndSwitch
EndFunc
Func open()
EndFunc ;==>open
Func close()
EndFunc ;==>close
Func _exit()
Exit
EndFunc ;==>_exit 本帖最后由 jybb001 于 2009-12-31 11:26 编辑
#NoTrayIcon
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <Misc.au3>
Opt("TRAYMENUMODE", 1);设置运作模式
Opt("TRAYONEVENTMODE", 1)
Opt("GuiOneventmode", 1)
HotKeySet("{ESC}","_exit")
;=========== 创建GUI菜单和注册GUI和控件事件开始 ===========
$Form1 = GUICreate("", 164, 218, -1, -1, BitOR($WS_SYSMENU, $WS_CAPTION, $WS_POPUP, $WS_POPUPWINDOW, $WS_BORDER, $WS_CLIPSIBLINGS))
$Button1 = GUICtrlCreateButton("打开文件", 28, 40, 107, 33, 0)
$Button2 = GUICtrlCreateButton("关闭文件", 28, 88, 107, 33, 0)
$Button3 = GUICtrlCreateButton("最小到托盘", 28, 136, 107, 33, 0)
$Group1 = GUICtrlCreateGroup("点击按钮选择操作", 9, 9, 145, 177)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Checkbox1 = GUICtrlCreateCheckbox("随Windows自启动", 24, 192, 113, 17)
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "GuiMsg");注册gui事件
GUICtrlSetOnEvent($Button1, "GuiMsg");注册gui控件事件
GUICtrlSetOnEvent($Button2, "GuiMsg");注册gui控件事件
GUICtrlSetOnEvent($Button3, "GuiMsg");注册gui控件事件
GUICtrlSetOnEvent($Checkbox1,"GuiMsg");注册gui控件事件
;=========== 创建GUI菜单和注册GUI和控件事件结束 ===========
;=========== 创建托盘菜单和注册托盘菜单事件开始 ===========
$show = TrayCreateItem("隐藏主窗口")
$open = TrayCreateItem("")
$open = TrayCreateItem("打开")
$close = TrayCreateItem("关闭")
$exit = TrayCreateItem("")
$exit = TrayCreateItem("退出| ESC |");制作托盘菜单
TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "TrayMsg");注册托盘图标双击事件
TrayItemSetOnEvent($show, "TrayMsg");注册托盘菜单“显示主窗口”事件
TrayItemSetOnEvent($open, "TrayMsg");注册托盘菜单“弹出”事件
TrayItemSetOnEvent($close, "TrayMsg");注册托盘菜单“关闭”事件
TrayItemSetOnEvent($exit, "TrayMsg");注册托盘菜单“退出”事件
TraySetState(1);设置系统托盘图标状态
TraySetClick(16);设置鼠标在系统托盘图标里面的点击模式
;=========== 创建托盘菜单和注册托盘菜单事件结束 ===========
While 1
Sleep(200)
WEnd
Func GuiMsg();;GUI控件点击后的操作
Switch @GUI_CtrlId
Case $Button1
open()
Case $Button2
close()
Case $Button3, $GUI_EVENT_CLOSE
GUISetState(@SW_HIDE, $Form1)
EndSwitch
EndFunc ;==>GuiMsg
Func TrayMsg() ;托盘菜单点击后的操作
Switch @TRAY_ID
Case $open
open()
Case $close
close()
Case $show, $TRAY_EVENT_PRIMARYDOUBLE
If WinGetState($Form1,"")=5 Then
GUISetState(@SW_SHOW, $Form1)
TrayItemSetText($show,"隐藏主窗口")
EndIf
If WinGetState($Form1,"")=7 Then
GUISetState(@SW_HIDE, $Form1)
TrayItemSetText($show,"显示主窗口")
EndIf
Case $exit
Exit
EndSwitch
EndFunc
Func open()
EndFunc ;==>open
Func close()
EndFunc ;==>close
Func _exit()
Exit
EndFunc ;==>_exit
Func TrayMsg() ;托盘菜单点击后的操作
Switch @TRAY_ID
Case $open
open()
Case $close
close()
Case $show, $TRAY_EVENT_PRIMARYDOUBLE
If WinGetState($Form1,"")=5 Then
GUISetState(@SW_SHOW, $Form1)
TrayItemSetText($show,"隐藏主窗口")
EndIf
If WinGetState($Form1,"")=7 Then
GUISetState(@SW_HIDE, $Form1)
TrayItemSetText($show,"显示主窗口")
EndIf
Case $exit
Exit
EndSwitch
EndFunc 回复 1# 痒痒
客气,共同学习而已。 呵呵,小虾必须经历的过程 我现在在做托盘菜单,正在完成中,进来学习一下。谢谢分享 很有用噢 做托盘菜单时可以试一下。。
页:
[1]