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();
}
}
}
}
}
以上代码并非用户控件代码,只需加在主窗体中即可。
WPF之托盘图标的设定的更多相关文章
- WPF TextBox提示文字设定
WPF TextBox框提示文字,鼠标划入提示文字消失 <TextBox Width=" VerticalContentAlignment="Center" Bor ...
- 理解Xaml标记语言
理解XAML XAML基于XAML,因而具有与XAML相似的特性.在XAMl中,同样必须区分大小写,但是Xaml以.xaml作为扩展名,表示这是一个应用程序的标记扩展文件.WPF中的XAML主要用于创 ...
- WPF 学习笔记-在WPF下创建托盘图标
原文:WPF 学习笔记-在WPF下创建托盘图标 首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Co ...
- 关于WPF System.windows.Media.FontFamily 的类型初始值设定项引发异常问题解决方法
造成原因:此问题的根本原因是.NET Framework January 2018 Rollup(KB4055002)与已安装的.NET Framework 4.7.1产品版本之间的MSI安装交互.R ...
- C# Wpf 后台代码设定UIElement的Canvas位置属性值
后台in-code设定元件UIElement的Canvas位置属性的两种方法: 1.UIElement.SetValue()方法 uiElement.SetValue(Canvas.TopProper ...
- [WPF]GridView或DataGrid中自定义样式:依据某一列设定其对应行的样式(背景色,字体等)
附效果照一张: 本方法使用StyleSelector来 获得依据自定义逻辑的style. ① class ConditionalStyleSelector : StyleSelector { publ ...
- wpf ,只能窗口调整高度,并且设定最小值。
MaxWidth="900" WindowStyle="ToolWindow" ResizeMode="CanResize" WindowS ...
- c# wpf ComboBox 动态下拉框 及 动态默认值设定
1.下拉框声明 <ComboBox x:Name="DirComboBox" Width="150" Height="18" Marg ...
- 2000条你应知的WPF小姿势 基础篇<78-81 Dialog/Location/WPF设备无关性>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000ThingsYou Should Know About C# 和 2,00 ...
随机推荐
- 朴素贝叶斯分类器及Python实现
贝叶斯定理 贝叶斯定理是通过对观测值概率分布的主观判断(即先验概率)进行修正的定理,在概率论中具有重要地位. 先验概率分布(边缘概率)是指基于主观判断而非样本分布的概率分布,后验概率(条件概率)是根据 ...
- 数据分析之numpy模块
numpy(numerical python)是python语言的一个扩展程序库,支持大量的维度数组和矩阵运算,此外也针对数组提供大量的数学函数库. 一.创建数组 1 使用array()创建 impo ...
- 使用C#编写自己的代码生成器,附代码讲解(一)
使用过代码生成器的开发人员应该知道,通过代码生成器生成项目的代码,可以大大的减少重复编码的时间,提供项目开发的效率,将自己从繁杂重复的代码中解放出来.现在网络上也有很多的开源的代码生成器或者使用比较广 ...
- Extjs4.2+webAPI+EF实现分页以及webapi的数据传值(续)
现在领导又要增加功能,需要分页的时候,每页显示N条信息.由于是每个页面都要改,所有需要声明了一个扩展类代码如下: // Copyright : 欧蓝德畅电子技术有限公司. All rights res ...
- ECMAScript typeof用法
typeof 返回变量的类型字符串值 .其中包括 “object”.“number”.“string”.“undefined”.“boolean”. 1.在变量只声明.却不初始化值 Or 在变量没 ...
- [PHP] 数据结构-反转链表PHP实现
1.常见方法分为迭代和递归,迭代是从头到尾,递归是从尾到头2.设置两个指针,old和new,每一项添加在new的后面,新链表头指针指向新的链表头3.old->next不能直接指向new,而是应该 ...
- Java基础——Servlet(八)文件上传下载
一.简单的文件上传常见的组件Smartupload , Apache 的 commons FileUploadSmartupload上传的步骤: 1.初始化上传上下文 2.准备上传 3.保存文件 &l ...
- js之跑马灯广告
目标效果:每过1秒重复把广告的第一个字符放到最后,达到动态跑马灯效果 代码如下: <!DOCTYPE html> <html lang="en"> < ...
- Centos 7.x 安装 Docker-ce
Centos 下安装 Docker-ce CentOS 7.0, CentOS 7.2: cat > /etc/yum.repos.d/docker-main.repo << -'E ...
- JS中的三种循环
三种循环1.while 2.do while 3.for 1.while: 语法结构:while(条件){代码块:改变条件} 步骤:1.初始化变量 2.判断条件 3.执行代码块 4.改变初始条 ...