WPF应用程序最小化到系统托盘
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Drawing = System.Drawing;
using Forms = System.Windows.Forms; namespace WpfApplication1
{
/// <summary>
/// Represents a thin wrapper for <see cref="Forms.NotifyIcon"/>
/// </summary>
[ContentProperty("Text")]
[DefaultEvent("MouseDoubleClick")]
public class NotificationAreaIcon : FrameworkElement
{
Forms.NotifyIcon notifyIcon; public static readonly RoutedEvent MouseClickEvent = EventManager.RegisterRoutedEvent(
"MouseClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotificationAreaIcon)); public static readonly RoutedEvent MouseDoubleClickEvent = EventManager.RegisterRoutedEvent(
"MouseDoubleClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotificationAreaIcon)); public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon", typeof(ImageSource), typeof(NotificationAreaIcon)); public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(NotificationAreaIcon)); public static readonly DependencyProperty FormsContextMenuProperty =
DependencyProperty.Register("MenuItems", typeof(List<Forms.MenuItem>), typeof(NotificationAreaIcon), new PropertyMetadata(new List<Forms.MenuItem>())); protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e); // Create and initialize the window forms notify icon based
notifyIcon = new Forms.NotifyIcon();
notifyIcon.Text = Text;
if (!DesignerProperties.GetIsInDesignMode(this))
{
notifyIcon.Icon = FromImageSource(Icon);
}
notifyIcon.Visible = FromVisibility(Visibility); if (this.MenuItems != null && this.MenuItems.Count > )
{
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(this.MenuItems.ToArray());
} notifyIcon.MouseDown += OnMouseDown;
notifyIcon.MouseUp += OnMouseUp;
notifyIcon.MouseClick += OnMouseClick;
notifyIcon.MouseDoubleClick += OnMouseDoubleClick; Dispatcher.ShutdownStarted += OnDispatcherShutdownStarted;
} private void OnDispatcherShutdownStarted(object sender, EventArgs e)
{
notifyIcon.Dispose();
} private void OnMouseDown(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseDownEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, , ToMouseButton(e.Button)));
} private void OnMouseUp(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseUpEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, , ToMouseButton(e.Button)));
} private void OnMouseDoubleClick(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseDoubleClickEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, , ToMouseButton(e.Button)));
} private void OnMouseClick(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseClickEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, , ToMouseButton(e.Button)));
} private void OnRaiseEvent(RoutedEvent handler, MouseButtonEventArgs e)
{
e.RoutedEvent = handler;
RaiseEvent(e);
} public List<Forms.MenuItem> MenuItems
{
get { return (List<Forms.MenuItem>)GetValue(FormsContextMenuProperty); }
set { SetValue(FormsContextMenuProperty, value); }
} public ImageSource Icon
{
get { return (ImageSource)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
} public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
} public event MouseButtonEventHandler MouseClick
{
add { AddHandler(MouseClickEvent, value); }
remove { RemoveHandler(MouseClickEvent, value); }
} public event MouseButtonEventHandler MouseDoubleClick
{
add { AddHandler(MouseDoubleClickEvent, value); }
remove { RemoveHandler(MouseDoubleClickEvent, value); }
} #region Conversion members private static Drawing.Icon FromImageSource(ImageSource icon)
{
if (icon == null)
{
return null;
}
Uri iconUri = new Uri(icon.ToString());
return new Drawing.Icon(Application.GetResourceStream(iconUri).Stream);
} private static bool FromVisibility(Visibility visibility)
{
return visibility == Visibility.Visible;
} private MouseButton ToMouseButton(Forms.MouseButtons button)
{
switch (button)
{
case Forms.MouseButtons.Left:
return MouseButton.Left;
case Forms.MouseButtons.Right:
return MouseButton.Right;
case Forms.MouseButtons.Middle:
return MouseButton.Middle;
case Forms.MouseButtons.XButton1:
return MouseButton.XButton1;
case Forms.MouseButtons.XButton2:
return MouseButton.XButton2;
}
throw new InvalidOperationException();
} #endregion Conversion members
}
}
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
<utils:NotificationAreaIcon
Text="NotificationAreaApplication1"
Icon="/Gemr;component/App.ico"
MouseDoubleClick="OnNotificationAreaIconDoubleClick" Grid.Row="2">
<utils:NotificationAreaIcon.MenuItems>
<!--Click="OnMenuItemOpenClick"-->
<forms:MenuItem Text="Re-login" Click="OnReLoginClick" DefaultItem="True" />
<forms:MenuItem Text="Exit" Click="OnMenuItemExitClick" />
</utils:NotificationAreaIcon.MenuItems>
</utils:NotificationAreaIcon>
#region Notification Icon bool shouldClose;
WindowState lastWindowState; protected override void OnStateChanged(EventArgs e)
{
this.lastWindowState = WindowState;
} protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (!shouldClose)
{
e.Cancel = true;
Hide();
}
} private void Open()
{
Show();
WindowState = this.lastWindowState;
} //双击还原
private void OnNotificationAreaIconDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
Open();
}
} //re-login private void OnReLoginClick(object sender, EventArgs e)
{
this.Logout();
} private void OnMenuItemExitClick(object sender, EventArgs e)
{
this.shouldClose = true;
Close();
} #endregion
WPF应用程序最小化到系统托盘的更多相关文章
- MFC程序最小化到系统托盘及其响应函数
预备知识: Windows API函数: WINSHELLAPI BOOL WINAPI Shell_NotifyIcon( DWORD dwMessage, PNOTIFYICONDATA pnid ...
- WPF应用最小化至系统托盘运行
原文:WPF应用最小化至系统托盘运行 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lordwish/article/details/5178889 ...
- 将 VMware 最小化到系统托盘
1, 下载 Trayconizer官网地址: http://www.whitsoftdev.com/trayconizer/下载地址: http://www.whitsoftdev.com/files ...
- C# WinForm窗口最小化到系统托盘
* C# WinForm窗口最小化到系统托盘http://hi.baidu.com/kfxtgtqyapouyze/item/8ccfdcd5a174a7312a35c7c3 主要功能:(1).程序启 ...
- Delphi 7下最小化到系统托盘
在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本.定义如下: 123456789 _NOTIFY ...
- Delphi 7下最小化到系统托盘(主要是WM_TRAYMSG和WM_SYSCOMMAND消息)
在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本.定义如下: 123456789 _NOTIFY ...
- 将Mozilla ThunderBird最小化到系统托盘(转载)
转自:http://be-evil.org/mozilla-thunderbird-minize-to-tray.html Mozilla ThunderBird是一款非常不错的邮件客户端,但是其在默 ...
- electron监听系统托盘,electron是否最小化到系统托盘
在项目中需要判断窗口是否最小化在系统托盘上,任务栏那已经关闭,查了一晚上的api,始终找不到可以调用的方法,最后绞尽脑汁想到了一个办法,那就是在点右上角的关闭按钮时,加个全局变量,用来标识已经最小到系 ...
- VC实现将对话框最小化到系统托盘
1.minisysDlg.h头文件设置: 1)public: void setTray();//设置托盘 NOTIFYICONDATA nid;//NOTIFYICONDATA结构包含了系统用来 ...
随机推荐
- null 与 string.IsNullOrEmpty 区别
!= null 就是不为null!string.IsNullOrEmpty 不是null且不是""(string.Empty) -----------Response: != n ...
- Unity3D 中 用quaternion 来对一个坐标点进行旋转的初步体会
在unity3d中,用四元数来表示旋转,四元数英文名叫quaternion . 比如 transform.rotation 就是一个四元数,其由四个部分组成 Quaternion = (xi + yj ...
- hdu 1086 You can Solve a Geometry Problem too
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- 编程语言java-并发(锁)
文章转载自http://www.importnew.com/22078.html 悲观锁和乐观锁 我们都知道,CPU是时分复用的,就是CPU把时间片,分配给不同的thread/process轮流执行, ...
- Ubuntu12.04配置mod_python
安装: sudo apt-get install libapache2-mod-python python-mysqldb 然后编辑配置文件/etc/apache2/sites-enabled/000 ...
- jsp_属性范围_request
request属性范围表示在服务器跳转后,所有设置的内容依然会被保留下来.(服务器端跳转:页面跳转,地址栏不发生变化) 下面写个小例子测试下: (1)request_demo.jsp <%@ p ...
- MySql自动分区
自动分区需要开启MySql中的事件调度器,可以通过如下命令查看是否开启了调度器 show variables like '%scheduler%'; 如果没开启的话通过如下指令开启 ; 1.创建一个分 ...
- UISwitch(开关控件)、UISegmentedControl(分段控件)
一.UISwitch 1.初始化 UISwitch *s1 = [[UISwitch alloc]initWithFrame:CGRectMake(50, 170, 100, 200)]; 2.设 ...
- javaweb 乱码---汉字存入mysql数据库中变成乱码
今天郁闷了一天,java程序在向mysql插入数据前不是乱码,数据库安装时也选了编码为utf8(和我程序的编码格式一致).可是插入数据就变成乱码,相当郁闷. 原因:mysql的配置文件中的编码并没有改 ...
- kali linux 渗透测试视频教程 第五课 社会工程学工具集
第五课 社会工程学工具集 文/玄魂 教程地址:http://edu.51cto.com/course/course_id-1887.html 目录 第五课社会工程学工具集 SET SET的社会工程 ...