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 ...
随机推荐
- Docker系列之Docker容器(读书笔记)
一.介绍 容器是独立运行的一个或一组应用,以及它们的运行态环境.对应的,虚拟机可以理解为模拟运行的一整套操作系统和排在上面的应用. 二.容器 2.1 启动容器 启动容器有两种方式,一种是基于镜像新建一 ...
- cvte前端笔试后的js原型总结
最近实习生招聘已经开始了,昨天晚上也终于迎来了第一场笔试,笔试的公司是cvte,笔试题总共27题,25道不定项还有2道编程题,虽然出的都是前端题,但是因为之前没有好好准备,还是很多做的不是很好o(╥﹏ ...
- 【NET CORE微服务一条龙应用】开始篇与目录
简介 随着业务的发展和变更,项目原先的分布式框架应用业务发展已有些不适应,所以18年初开始准备使用微服务框架,当时正好看到了ocelot项目,特意翻看了源码,发现很灵活和易扩展 于是就开始了微服务的开 ...
- Scala构建工具SBT
SBT(Simple Build Tool)是Scala的项目构建工具,拥有依赖管理,构建过程管理和打包等功能. SBT官网上给出各平台的安装方法: MAC: 使用homebrew安装 brew in ...
- Python装饰器举例分析
概述 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 我们要需要一个能测试函数运行时间的decorator,可以定义如 ...
- ASP .NET MVC HtmlHelper扩展——简化“列表控件”的绑定
在众多表单元素中,有一类<select>元素用于绑定一组预定义列表.传统的ASP.NET Web Form中,它对应着一组重要的控件类型,即ListControl,我们经常用到DropDo ...
- ECMAScript 引用类型
Object对象 新建对象 var obj = new Object() var obj ={} var obj={age:23} ... hasOwnProperty(property) 方法 va ...
- lucene 核心概念及入门
lucene Lucene介绍及核心概念 什么是Lucene Lucene是一套用于全文检索和搜索的开放源代码程序库,由Apache软件基金会支持和提供.Lucene提供了一个简单却强大的应用程序接口 ...
- Internet Download Manager IDM 破解器
IDM 全名Internet Download Manager 是一款国外的多线程下载神器(简称IDM),Internet Download Manager 支持多媒体下载.自动捕获链接.自动识别文 ...
- ngx-echart地图
一.运行截图 二.代码 html代码: <div style="padding:24px;"> <p style="font-size: 16px;ma ...