首先需要引用  System.Windows;  System.Drawing;

    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("NotifyIcon.ico");
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip();
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);
}
/// <summary>
/// 窗体状态改变时候触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SysTray_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Minimized)
{ //this.Visibility = Visibility.Hidden;
}
} /// <summary>
/// 退出选项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param> 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();
}
} /// <summary>
/// 鼠标单击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param> 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.WindowState = WindowState.Normal;
//this.Activate();
}
}
}
    // 改变窗体关闭按钮事件
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;//取消窗体关闭
this.Visibility = Visibility.Hidden;//窗体隐藏
}
}

WPF 程序启动显示为通知区域的图标方法的更多相关文章

  1. wpf 程序启动显示图片

    一.设置图片的生成操作 程序启动时会出现0.5秒的图片显示,再显示程序界面. 二.写代码实现相同效果 /// <summary> /// App.xaml 的交互逻辑 /// </s ...

  2. Win7通知区域的图标怎么去除?

    由于本人有洁癖,最近在用win7的时候,很收不了已经卸载了的一些软件,在win7右下角的通知区域图标中还留有痕迹,于是上网查找了下解决方案. 用以下方法完美解决问题. 这里依然是以注册表的修改方法为主 ...

  3. 关于WPF程序启动性能

    项目里WPF的启动时间太久(>1分钟),显然是不能接受的.超过10秒,连Loading的等待框都会让用户感到厌烦. 1. 症状 项目的结构是1个WPF主进程,启动3个WPF子进程.子进程在启动时 ...

  4. WPF 程序中启动和关闭外部.exe程序

    当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...

  5. 处理win7任务栏通知区域图标异常问题

    故障现象:安装的某软件比如QQ,应用程序运行图标始终没有在win7任务栏通知区域显示出来,经观查发现win7任务栏通知区域有几个已删除应用的图标出现,应该是有故障了. 故障现象一:已经卸载的程序,还在 ...

  6. WPF程序在Windows 7下应用Windows 8主题

    这篇博客介绍如何在Windows 7下应用Windows 8的主题. 首先我们先看一个很常见的场景,同样的WPF程序(样式未重写)在不同的操作系统上展示会有些不同.这是为什么呢?WPF程序启动时会加载 ...

  7. PreApplicationStartMethodAttribute程序启动扩展

    一.PreApplicationStartMethodAttribute 类简介 应用程序启动时提供的扩展的支持. 命名空间:   System.Web程序集:  System.Web(位于 Syst ...

  8. WPF ListView 居中显示

    原文:WPF ListView 居中显示 今天遇到的问题: 方法1:设置GridViewColumn的ActualWidth <ListView > <ListView.View&g ...

  9. Win10任务栏通知区域上已卸载程序无效图标选项如何清除?

    在Win10系统中,大部分用户都已经知道在“选择在任务栏上显示哪些图标”来让一些软年图标显示,一些隐藏,不过使用Win10系统久了之后发现,在设置通知区域图标中有很多已经卸载程序的无效选项!这让设置时 ...

随机推荐

  1. Linux学习笔记(16)shell基础之Bash变量

    1. 用户自定义变量 (1)变量设置规则 ① 变量名称可由字母.数字和下划线组成,但不能以数字开头: ② 变量的默认类型为字符串类型,如果要对数值运算,则必须指定变量类型为数值型: ③ 变量用等号连接 ...

  2. CSS 基本1

    HTML元素可以分为两种: 块级元素 内联元素(行内元素) 两者的区别在于以下三点: 块级元素会独占一行(即无法与其他元素显示在同一行内,除非你显示修改元素的 display 属性),而内联元素则都会 ...

  3. 封装用className选元素

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. jQuery全局函数

    全局函数是对jQuery对象的扩展,其中扩展方法包括: 一,extend扩展: //调用全局函数$(document).ready(function () { $.myFunction(); $.my ...

  5. loadrunner通过字符串左右边界切割字符串

    void web_reg_save_param_custom(char *sourceStr, char* outpuStr, char *leftBdry, char *rightBdry){    ...

  6. JAVA第三周作业

    一:枚举 package homework; public class EnumTest { public static void main(String[] args) { Size s=Size. ...

  7. DSP using MATLAB 示例Example3.7

    上代码: x1 = rand(1,11); x2 = rand(1,11); n = 0:10; alpha = 2; beta = 3; k = 0:500; w = (pi/500)*k; % [ ...

  8. json时间格式化问题

    function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseInt(jsonDate.rep ...

  9. spring mvc 返回json

    服务器端返回的是文本,客户端得到文本后将文本转换成json就可以了,服务器端将对象转换成json 的文本形式,并且需要设定文本的类型为text/plain,charset=UTF-8 所以在 appl ...

  10. 通过adb命令打印log

    1.adb logcat  --打印当前设备上所有日志 2.adb logcat | findstr *** --过滤仅含***的日志 3.adb logcat *:W  --过滤打印严重级别W及以上 ...