WPF程序添加托盘图标,菜单的方法
来源:互联网 发布:没有windows update 编辑:程序博客网 时间:2024/05/22 16:42
前段时间做项目,客户需要程序最小化时到托盘,并添加托盘菜单,可以做退出,打开灯操作。
首先需要定义一个类。托盘菜单可用的。
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 命名空间
{
[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 > 0)
{
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, 0, ToMouseButton(e.Button)));
}
private void OnMouseUp(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseUpEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
}
private void OnMouseDoubleClick(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseDoubleClickEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
}
private void OnMouseClick(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseClickEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, 0, 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
}
}
然后在主程序前台代码这里引用
<Window x:Class="命名空间.WndMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tp="clr-namespace:命名空间"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
菜单调用的地方
<!--下面部分为系统托盘菜单-->
<tp:NotificationAreaIcon Text="测试托盘菜单" Icon="/hysaleclient;component/Resources/hycf1.ico" MouseDoubleClick="NotificationAreaIcon_MouseDoubleClick">
<tp:NotificationAreaIcon.MenuItems>
<wf:MenuItem Text="关于软件" Click="MenuItem_Click" ></wf:MenuItem >
<wf:MenuItem Text="打开" DefaultItem="True" Click="MenuItem_Click_1"></wf:MenuItem>
<wf:MenuItem Text="退出" Click="MenuItem_Click_2" ></wf:MenuItem>
</tp:NotificationAreaIcon.MenuItems>
</tp:NotificationAreaIcon>
后台代码部分
IsMinSized 是个布尔变量,初始设置为false,最小化后设置为true,也可以不用,直接用windowstate来做判断
/// <summary>
/// 鼠标双击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NotificationAreaIcon_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
OpenFromIcon();
}
}
/// <summary>
/// 点击图标展开程序
/// </summary>
private void OpenFromIcon()
{
this.Visibility = Visibility.Visible;
this.ShowInTaskbar = true;
this.WindowState = WindowState.Normal;
}
/// <summary>
/// 关于软件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("开发单位:*************************\r\n软件版本:1.0.10\r\n最后更新:2013-07-01", "关于软件");
}
/// <summary>
/// 打开程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Click_1(object sender, EventArgs e)
{
if (IsMinSized)
{
OpenFromIcon();
}
}
/// <summary>
/// 退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Click_2(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// 窗口尺寸变化事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_StateChanged(object sender, EventArgs e)
{
//如果最小化
if (this.WindowState == WindowState.Minimized)
{
IsMinSized = true;
this.ShowInTaskbar = false;
}
}
- WPF程序添加托盘图标,菜单的方法
- WPF工具栏图标托盘图标的添加方法
- Java程序添加托盘图标和托盘弹出菜单
- 隐藏指定程序托盘图标的方法
- 隐藏指定程序托盘图标的方法
- JAVA添加托盘图标和托盘弹出式菜单
- SWT 中实现最小化到托盘图标,并只能通过托盘的弹出菜单关闭程序
- SWT 中实现最小化到托盘图标,并只能通过托盘的弹出菜单关闭程序
- 为应用程序添加托盘图标的一种方法
- MFC 托盘中添加程序图标
- MFC 托盘中添加程序图标
- WPF中添加菜单里最前面的图标.
- [WPF] 托盘菜单的基本功能实现
- 托盘图标及菜单的处理
- MFC下托盘图标的实现和托盘菜单
- MFC下托盘图标的实现和托盘菜单
- 托盘图标的添加和消息响应
- WPF 托盘菜单 自定义菜单风格 的做法
- GCC 参数详解
- 十大前端开发框架(下)
- WebService 之调用天气预报
- 第25讲:设计模式总结
- asp.net(c#)快速生成Json格式的数据(JsonHelper)
- WPF程序添加托盘图标,菜单的方法
- Uva-348-Optimal Array Multiplication Sequence
- PHP字节转换函数
- 张贴关于scrum的工具
- C#面向对象设计模式纵横谈
- 2011-05-24 11:09 WINCE6.0 驱动音量调节的实现
- uitableviewcell选中后颜色更改
- WPAD:Web ProxyAutoDiscovery,Web代理服务器自动发现
- Asterrik一些常用命令