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. 存储构造题(Print Check)

    连接:Print Check 题意:n行m列的矩阵,有k次涂色,每一涂一行或者一列,求最后的涂色结果. 从数据的大小看,暴力肯定要TLE: 问题是如何存储数据. 首先:我们只要最后的涂色结果. 其次: ...

  2. JS初级-作用域

    作用域:域:空间.范围.区域--作用:读.写    script        全局变量.全局函数        自上而下        函数        由里到外        {}    浏览器 ...

  3. 【kd-tree】bzoj1941 [Sdoi2010]Hide and Seek

    枚举每个点,计算离他最近的和最远的点. #include<cstdio> #include<cmath> #include<algorithm> using nam ...

  4. ThinkPHP中add()方法的返回值

    在处理业务数据的时候,有时会遇到需要取得刚insert的数据的主键id值. 以前的做法都是先插入数据后,再通过查询获得id值. 后来在学习ThinkPHP开发的时候才知道,add()方法在写入成功后得 ...

  5. linux 中如何修改时间 date

    修改linux的时间可以使用date指令  修改日期: 时间设定成2009年5月10日的命令如下: #date -s 05/10/2009  修改时间: 将系统时间设定成上午10点18分0秒的命令如下 ...

  6. 【07_226】Invert Binary Tree

    Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary ...

  7. Modifiers: virtual, override, new, abstract, sealed, internal

    internal 声明类.类成员.接口或接口成员具有内部可见性. internal 修饰符使类.接口或成员仅在当前包中可见. 当前包之外的代码不能访问 internal 成员.只有在同一程序集的文件中 ...

  8. delphi 获取两个颜色差值

    前面说了已经获取到颜色值了,现在需要比较两个颜色的差值. 两个颜色的根据RGB的差来取,有两种情况: 1.(R的平方+G的平方+B的平方)开根号,再两个颜色值相减获取差值. 2.(((R1-R2)的平 ...

  9. 【原创】Windows Server 文件夹权限小问题

    服务器:Windows Server 2008 R2 Standard 做文件服务器 问题:在资源管理器里给账号设置了R/W权限,但是一直有问题,写失败. 解决:需要在server manager-r ...

  10. 我的Linux随笔目录

    现在整理博客的时间少了,大多是在用为知笔记收藏和整理,一次集中发点Linux相关随笔整理和一个目录,是按时间顺序来的.每一篇都是自己用过之后整理的,应用场景已经尽可能的说明了,不明白的可以Q我,上班时 ...