原文:WPF 学习笔记-在WPF下创建托盘图标

首先需要在项目中引用System.Windows.Forms,System.Drawing;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing; namespace WpfApplication1
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitialTray();
}
private System.Windows.Forms.NotifyIcon notifyIcon = null;
private void InitialTray()
{ //设置托盘的各个属性
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.BalloonTipText = \"程序开始运行\";
notifyIcon.Text = \"托盘图标\";
notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + \"\\\\wp.ico\");
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(2000);
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick); //设置菜单项
System.Windows.Forms.MenuItem menu1 = new System.Windows.Forms.MenuItem(\"菜单项1\");
System.Windows.Forms.MenuItem menu2 = new System.Windows.Forms.MenuItem(\"菜单项2\");
System.Windows.Forms.MenuItem menu = new System.Windows.Forms.MenuItem(\"菜单\", new System.Windows.Forms.MenuItem[] { menu1 , menu2 }); //退出菜单项
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem(\"exit\");
exit.Click += new EventHandler(exit_Click); //关联托盘控件
System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { menu , exit };
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen); //窗体状态改变时候触发
this.StateChanged += new EventHandler(SysTray_StateChanged);
}
///
/// 窗体状态改变时候触发
///
/// /// private void SysTray_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Minimized)
{
this.Visibility = Visibility.Hidden;
}
} ///
/// 退出选项
///
/// /// private void exit_Click(object sender, EventArgs e)
{
if (System.Windows.MessageBox.Show(\"确定要关闭吗?\",
\"退出\",
MessageBoxButton.YesNo,
MessageBoxImage.Question,
MessageBoxResult.No) == MessageBoxResult.Yes)
{
notifyIcon.Dispose();
System.Windows.Application.Current.Shutdown();
}
} ///
/// 鼠标单击
///
/// /// private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (this.Visibility == Visibility.Visible)
{
this.Visibility = Visibility.Hidden;
}
else
{
this.Visibility = Visibility.Visible;
this.Activate();
}
}
}
}
}

以上代码并非用户控件代码,只需加在主窗体中即可。

最新文章

  1. jQuery自定义漂亮的下拉框插件8种效果演示
  2. java利用透明的图片轮廓抠图
  3. ios 计算缓存大小
  4. C++拓扑排序
  5. 【CSS3】---:before :after生成内容
  6. Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project '项目名'
  7. 改进RazorPad
  8. MySQL数据备份之mysqldump
  9. DB2数据库常用基本操作命令
  10. iOS 请求数据 error
  11. loadrunner常用函数总结
  12. 提取Jar2Exe源代码,JavaAgent监控法
  13. Django(一) 安装使用基础
  14. java课程之团队开发冲刺1.2
  15. 【Python】【BugList13】req = requests.get(url=target)报错: (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)')
  16. PHP之null
  17. git命令操作的时候,出现中文名显示问题
  18. h5 手机端适配问题汇总
  19. Win10还原被Windows Defender隔离的文件
  20. python零散补充与总结

热门文章

  1. leetCode题解之求二叉树每层的平均值
  2. C语言高精度乘法
  3. Linux HugePages 配置与 Oracle 性能关系说明
  4. UIImageView的animationImages动画
  5. hibernate连接mysql,查询条件中有中文时,查询结果没有记录,而数据库有符合条件的记录(解决方法)
  6. docker 自制CentOS 6-lnp镜像
  7. ZT eoe android4.2 Bluetooth记录01-结构和代码分布
  8. [转] 29个你必须知道的Linux命令
  9. Alpha 冲刺报告(2/10)
  10. 推荐一个好用的以多tab标签方式打开windows CMD的工具