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应用程序最小化到系统托盘的更多相关文章

  1. MFC程序最小化到系统托盘及其响应函数

    预备知识: Windows API函数: WINSHELLAPI BOOL WINAPI Shell_NotifyIcon( DWORD dwMessage, PNOTIFYICONDATA pnid ...

  2. WPF应用最小化至系统托盘运行

    原文:WPF应用最小化至系统托盘运行 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lordwish/article/details/5178889 ...

  3. 将 VMware 最小化到系统托盘

    1, 下载 Trayconizer官网地址: http://www.whitsoftdev.com/trayconizer/下载地址: http://www.whitsoftdev.com/files ...

  4. C# WinForm窗口最小化到系统托盘

    * C# WinForm窗口最小化到系统托盘http://hi.baidu.com/kfxtgtqyapouyze/item/8ccfdcd5a174a7312a35c7c3 主要功能:(1).程序启 ...

  5. Delphi 7下最小化到系统托盘

    在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本.定义如下: 123456789   _NOTIFY ...

  6. Delphi 7下最小化到系统托盘(主要是WM_TRAYMSG和WM_SYSCOMMAND消息)

    在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本.定义如下: 123456789   _NOTIFY ...

  7. 将Mozilla ThunderBird最小化到系统托盘(转载)

    转自:http://be-evil.org/mozilla-thunderbird-minize-to-tray.html Mozilla ThunderBird是一款非常不错的邮件客户端,但是其在默 ...

  8. electron监听系统托盘,electron是否最小化到系统托盘

    在项目中需要判断窗口是否最小化在系统托盘上,任务栏那已经关闭,查了一晚上的api,始终找不到可以调用的方法,最后绞尽脑汁想到了一个办法,那就是在点右上角的关闭按钮时,加个全局变量,用来标识已经最小到系 ...

  9. VC实现将对话框最小化到系统托盘

    1.minisysDlg.h头文件设置: 1)public: void setTray();//设置托盘    NOTIFYICONDATA nid;//NOTIFYICONDATA结构包含了系统用来 ...

随机推荐

  1. oracle 游标使用大全

    转:http://www.cnblogs.com/fjfzhkb/archive/2007/09/12/891031.html oracle的游标和例子! 游标-----内存中的一块区域,存放的是se ...

  2. hdoj 1874 畅通工程续

    Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行 ...

  3. select 练习2

    group by :select 分组字段,聚合函数 from 表名 where  条件 group by 分组字段 having 过滤条件 select cno,avg(degree) from s ...

  4. 在centos上编译安装mariadb数据库

    一.安装前提(准备数据文件.安装其他依赖的软件) 1.准备数据存放的目录 [root@localhost ~]# fdisk /dev/sdb  (fdisk /dev/sdb 创建一个逻辑分区/de ...

  5. mysql 语句执行顺序问题

    今天在写程序的时候,做分页查找时无意中,将计算数据库查询数量的语句,放到了limit之中,导致出现了bug. 所以发现以下问题: select count(1) from table limit 0, ...

  6. centos下网络配置方法(网关、dns、ip地址配置)

    本文介绍了centos网络配置的方法,centos网络配置主要包括dns.网关.IP地址: 1.IP地址配置: /etc/sysconfig/network-scripts/ifcfg-eth0 2. ...

  7. SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN

    1 trunc(value,precision)按精度(precision)截取某个数字,不进行舍入操作.2 round(value,precision)根据给定的精度(precision)输入数值. ...

  8. C# ADO.NET SqlDataAdapter中传递参数

    ADO.NET的SQL语句中,往往不是静态的语句,而是需要接受传递过来的参数,比如典型的登录功能,需要查找指定的用户名: string sqlQuery = "SELECT * FROM W ...

  9. 那些年我们一起追过的Linux命令

    1. cd - 作用:返回上次的目录,类似于Windows的后退功能: 2. man ascii 作用:打印出八进制.十六进制和十进制的ASCII码表:(再也不用Google ASCII表啦) 3. ...

  10. [stm32] 利用uC-BmpCvt软件生成uc-gui可调用的bmp图片

    >_<:学习贴图[bmp图]:首先找一张bmp格式的图片,然后下载uC-BmpCvt软件,打开改图片 >_<:然后点击Image-Conver-Into-Best Palatt ...