本文主要是介绍WPF程序添加托盘图标,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
程序添加托盘图标
UI层
//添加handycontrol的引用xmlns:hc="https://handyorg.github.io/handycontrol"//添加NotifyIcon图标 实现单击 双击 二级菜单点击功能<hc:NotifyIconText="通知"Token="Info"><hc:NotifyIcon.ContextMenu><ContextMenu><MenuItem Command="hc:ControlCommands.PushMainWindow2Top" Header="Open" /><MenuItem Command="hc:ControlCommands.ShutdownApp" Header="Exit" /></ContextMenu></hc:NotifyIcon.ContextMenu><hc:Interaction.Triggers><hc:EventTrigger EventName="Click"><hc:EventToCommand Command="hc:ControlCommands.PushMainWindow2Top"/></hc:EventTrigger><hc:EventTrigger EventName="MouseDoubleClick"><hc:EventToCommand Command="hc:ControlCommands.PushMainWindow2Top"/></hc:EventTrigger></hc:Interaction.Triggers></hc:NotifyIcon>
后台调用,通过设置的Token值获取到NotifyIcon实例,显示图标
private void SendNotification(){NotifyIcon.ShowBalloonTip("托盘图标测试", "托盘", NotifyIconInfoType.None,"Info");}
当窗口关闭时,提示
//是否启用托盘图标显示功能private bool UseMode=true;protected override void OnClosing(CancelEventArgs e){if(UseMode){HandyControl.Controls.MessageBox.Info(Properties.Settings.Default.关闭软件提示, "提示");Hide();e.Cancel = true;}else{base.OnClosing(e);}}
Properties.Settings.Default.XXX :读取配置文件Settings.settings中的值
HandyControl.Controls.MessageBox.Info:HandyControl窗口提示信息
这篇关于WPF程序添加托盘图标的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!