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. android system.img

    哥们要我做些模拟包,给过来的是mtk的底包,需要从system.img中提取部分文件. 网上一找资料,说是yaffs2文件系统,同时以前做linux的时候也是用yaffs2,感觉碰到老朋友了,不管三七 ...

  2. java 字符串截取

    截取指定长度的字符串,如果超出就用more的内容来替换 截取的字节数,截取的时候,有可能会少截取一位(当最后一位是1个双字节的话,会少截取一个) public class Test {    publ ...

  3. Unity5的AssetBundle的一点使用心得

    昨天一位朋友在我这里留言,想让我写点Unity5的AssetBundle心得.于是我就看了相关的介绍,和自己确切的做了一次.下面来谈谈所谓的心得. 如果你觉得自己对AssetBundle不熟悉,建议先 ...

  4. Linux tar打包命令

    Linux tar打包命令: 范例一:将整个 /etc 目录下的文件全部打包成为 /tmp/etc.tar [root@linux ~]# tar -cvf /tmp/etc.tar /etc < ...

  5. MySQL 半同步复制

    在主库初次启动时,执行如下语句加载semisync_master插件: mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_m ...

  6. [转载]理解HTML语义化

    声明: 本文转载于:freeyiyi1993博客. 原文地址:http://www.cnblogs.com/freeyiyi1993/p/3615179.html 1.什么是HTML语义化? < ...

  7. C++函数模版

    如果对于两种不同类型的数值进行比较,可能会写出以下的代码: int compare(const string &v1, const string &v2) { if (v1 < ...

  8. css伪类运用

    <!doctype html><html><head><meta charset="utf-8"><title>CSS输 ...

  9. Magicodes.WeiChat——利用纷纭打造云日志频道

    纷纭,是个免费的渠道集成工具.这里我就不多介绍了,右侧是飞机票:https://lesschat.com/ 在开发或者在运维情况下,我们经常需要查看并关注服务器端日志以确保程序是否健康运行.尤其是在微 ...

  10. (转)【ASP.NET Web API】Authentication with OWIN

    概述 本文说明了如何使用 OWIN 来实现 ASP.NET Web API 的验证功能,以及在客户端与服务器的交互过程中,避免重复提交用户名和密码的机制. 客户端可以分为两类: JavaScript: ...