码迷,mamicode.com
首页 > Windows程序 > 详细

WPF实现窗口最小化到托盘,并且实现右击菜单

时间:2015-11-22 23:31:34      阅读:764      评论:0      收藏:0      [点我收藏+]

标签:

原版是从网上找了一位大神的,自己只是用了一点适合自己的。

具体实现

1.首先已经确认WPF中没有实现最小化托盘的类与方法,用到了winform中的程序集

using Drawing = System.Drawing;
using Forms = System.Windows.Forms;

技术分享

2.XAML的后代相应事件的Demo,只是为了看起来方便~!其中也包含了在任务栏中不现实图标,只在托盘中显示。双击实现窗口的还原。没用到大神写的,自己琢磨了个,令人想不到的蛋疼的后果还没出现,也就暂时这样了。

技术分享
  1 namespace 最小化到托盘
  2 {
  3     /// <summary>
  4     /// MainWindow.xaml 的交互逻辑
  5     /// </summary>
  6     public partial class MainWindow : Window
  7     {
  8         public MainWindow()
  9         {
 10             //妈蛋找了你一天,,启动后不现实任务栏图标!!!!!!!!!!!!!!!!!!!!
 11             this.ShowInTaskbar = false;
 12             InitializeComponent();
 13         }
 14         /// <summary>
 15         /// 托盘右击菜单--上线按钮
 16         /// </summary>
 17         /// <param name="sender"></param>
 18         /// <param name="e"></param>
 19         private void MenuItem_OnlineClick(object sender, EventArgs e)
 20         {
 21             MessageBox.Show("提示上线!");
 22         }
 23 
 24         /// <summary>
 25         /// 托盘右击菜单--离开按钮
 26         /// </summary>
 27         /// <param name="sender"></param>
 28         /// <param name="e"></param>
 29         private void MenuItem_LeaveClick(object sender, EventArgs e)
 30         {
 31             MessageBox.Show("提示暂时离开!");
 32         }
 33         /// <summary>
 34         /// 托盘右击菜单--在忙按钮
 35         /// </summary>
 36         /// <param name="sender"></param>
 37         /// <param name="e"></param>
 38         private void MenuItem_BusyClick(object sender, EventArgs e)
 39         {
 40             MessageBox.Show("提示在忙!");
 41         }
 42         /// <summary>
 43         /// 托盘右击菜单--请勿打扰按钮
 44         /// </summary>
 45         /// <param name="sender"></param>
 46         /// <param name="e"></param>
 47         private void MenuItem_NoBotherClick(object sender, EventArgs e)
 48         {
 49             MessageBox.Show("提示请勿打扰!");
 50         }
 51         /// <summary>
 52         /// 托盘右击菜单--隐身按钮
 53         /// </summary>
 54         /// <param name="sender"></param>
 55         /// <param name="e"></param>
 56         private void MenuItem_HideClick(object sender, EventArgs e)
 57         {
 58             MessageBox.Show("提示隐身!");
 59         }
 60         /// <summary>
 61         /// 托盘右击菜单--离线按钮
 62         /// </summary>
 63         /// <param name="sender"></param>
 64         /// <param name="e"></param>
 65         private void MenuItem_OffLineClick(object sender, EventArgs e)
 66         {
 67             MessageBox.Show("提示离线!");
 68         }
 69         /// <summary>
 70         /// 托盘右击菜单--关于我们按钮
 71         /// </summary>
 72         /// <param name="sender"></param>
 73         /// <param name="e"></param>
 74         private void MenuItem_AboutUsClick(object sender, EventArgs e)
 75         {
 76             MessageBox.Show("提示关于我们!");
 77         }
 78         /// <summary>
 79         /// 托盘小图标的双击事件--最小化的状态下双击还原
 80         /// </summary>
 81         /// <param name="sender"></param>
 82         /// <param name="e"></param>
 83         private void NotificationAreaIcon_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
 84         {
 85             //if (e.ChangedButton==MouseButton.Left)
 86             //{
 87                 if (this.WindowState == WindowState.Minimized)
 88                 {
 89                     WindowState = WindowState.Normal;
 90                 }
 91            // }
 92         }
 93 
 94         private void MenuItem_ExitClick(object sender, EventArgs e)
 95         {
 96             this.Close();
 97         }
 98 
 99         private void Window_MouseMove_1(object sender, MouseEventArgs e)
100         {
101         }
102 
103         //以下代码想实现即时聊天的记住密码功能,度娘说要存入配置文件,没搞成功,希望看到的人帮我这个忙~
104         private void Button_Click_1(object sender, RoutedEventArgs e)
105         {
106             //点击按钮往配置文件存入信息
107             Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
108             cfa.AppSettings.Settings["LogName"].Value = "123";
109             cfa.Save();
110             MessageBox.Show(ConfigurationManager.AppSettings["LogName"]);
111         }
112     }
113 }
View Code

3.最主要的是大神写的这个类,学习编程五个月,能看懂的不是太多,稍微明白点意思,但是说不上来,直接贴代码吧。以后在研究!

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Windows;
  5 using System.Windows.Input;
  6 using System.Windows.Markup;
  7 using System.Windows.Media;
  8 using System.Windows.Threading;
  9 using Drawing = System.Drawing;
 10 using Forms = System.Windows.Forms;
 11 
 12 namespace 最小化到托盘
 13 {
 14     /// <summary>
 15     /// Represents a thin wrapper for <see cref="Forms.NotifyIcon"/>
 16     /// </summary>
 17     [ContentProperty("Text")]
 18     [DefaultEvent("MouseDoubleClick")]
 19     public class NotificationAreaIcon : FrameworkElement
 20     {
 21         Forms.NotifyIcon notifyIcon;
 22 
 23         public static readonly RoutedEvent MouseClickEvent = EventManager.RegisterRoutedEvent(
 24             "MouseClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotificationAreaIcon));
 25 
 26         public static readonly RoutedEvent MouseDoubleClickEvent = EventManager.RegisterRoutedEvent(
 27             "MouseDoubleClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotificationAreaIcon));
 28 
 29         public static readonly DependencyProperty IconProperty =
 30             DependencyProperty.Register("Icon", typeof(ImageSource), typeof(NotificationAreaIcon));
 31 
 32         public static readonly DependencyProperty TextProperty =
 33             DependencyProperty.Register("Text", typeof(string), typeof(NotificationAreaIcon));
 34 
 35         public static readonly DependencyProperty FormsContextMenuProperty =
 36             DependencyProperty.Register("MenuItems", typeof(List<Forms.MenuItem>), typeof(NotificationAreaIcon), new PropertyMetadata(new List<Forms.MenuItem>()));
 37 
 38         protected override void OnInitialized(EventArgs e)
 39         {
 40             base.OnInitialized(e);
 41 
 42             // Create and initialize the window forms notify icon based
 43             notifyIcon = new Forms.NotifyIcon();
 44             notifyIcon.Text = Text;
 45             if (!DesignerProperties.GetIsInDesignMode(this))
 46             {
 47                 notifyIcon.Icon = FromImageSource(Icon);
 48             }
 49             notifyIcon.Visible = FromVisibility(Visibility);
 50 
 51             if (this.MenuItems != null && this.MenuItems.Count > 0)
 52             {
 53                 notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(this.MenuItems.ToArray());
 54             }
 55 
 56             notifyIcon.MouseDown += OnMouseDown;
 57             notifyIcon.MouseUp += OnMouseUp;
 58             notifyIcon.MouseClick += OnMouseClick;
 59             notifyIcon.MouseDoubleClick += OnMouseDoubleClick;
 60 
 61             Dispatcher.ShutdownStarted += OnDispatcherShutdownStarted;
 62         }
 63 
 64         private void OnDispatcherShutdownStarted(object sender, EventArgs e)
 65         {
 66             notifyIcon.Dispose();
 67         }
 68 
 69         private void OnMouseDown(object sender, Forms.MouseEventArgs e)
 70         {
 71             OnRaiseEvent(MouseDownEvent, new MouseButtonEventArgs(
 72                 InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
 73         }
 74 
 75         private void OnMouseUp(object sender, Forms.MouseEventArgs e)
 76         {
 77             OnRaiseEvent(MouseUpEvent, new MouseButtonEventArgs(
 78                 InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
 79         }
 80 
 81         private void OnMouseDoubleClick(object sender, Forms.MouseEventArgs e)
 82         {
 83             OnRaiseEvent(MouseDoubleClickEvent, new MouseButtonEventArgs(
 84                 InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
 85         }
 86 
 87         private void OnMouseClick(object sender, Forms.MouseEventArgs e)
 88         {
 89             OnRaiseEvent(MouseClickEvent, new MouseButtonEventArgs(
 90                 InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
 91         }
 92 
 93         private void OnRaiseEvent(RoutedEvent handler, MouseButtonEventArgs e)
 94         {
 95             e.RoutedEvent = handler;
 96             RaiseEvent(e);
 97         }
 98 
 99         public List<Forms.MenuItem> MenuItems
100         {
101             get { return (List<Forms.MenuItem>)GetValue(FormsContextMenuProperty); }
102             set { SetValue(FormsContextMenuProperty, value); }
103         }
104 
105         public ImageSource Icon
106         {
107             get { return (ImageSource)GetValue(IconProperty); }
108             set { SetValue(IconProperty, value); }
109         }
110 
111         public string Text
112         {
113             get { return (string)GetValue(TextProperty); }
114             set { SetValue(TextProperty, value); }
115         }
116 
117         public event MouseButtonEventHandler MouseClick
118         {
119             add { AddHandler(MouseClickEvent, value); }
120             remove { RemoveHandler(MouseClickEvent, value); }
121         }
122 
123         public event MouseButtonEventHandler MouseDoubleClick
124         {
125             add { AddHandler(MouseDoubleClickEvent, value); }
126             remove { RemoveHandler(MouseDoubleClickEvent, value); }
127         }
128 
129         #region Conversion members
130 
131         private static Drawing.Icon FromImageSource(ImageSource icon)
132         {
133             if (icon == null)
134             {
135                 return null;
136             }
137             Uri iconUri = new Uri(icon.ToString());
138             return new Drawing.Icon(Application.GetResourceStream(iconUri).Stream);
139         }
140 
141         private static bool FromVisibility(Visibility visibility)
142         {
143             return visibility == Visibility.Visible;
144         }
145 
146         private MouseButton ToMouseButton(Forms.MouseButtons button)
147         {
148             switch (button)
149             {
150                 case Forms.MouseButtons.Left:
151                     return MouseButton.Left;
152                 case Forms.MouseButtons.Right:
153                     return MouseButton.Right;
154                 case Forms.MouseButtons.Middle:
155                     return MouseButton.Middle;
156                 case Forms.MouseButtons.XButton1:
157                     return MouseButton.XButton1;
158                 case Forms.MouseButtons.XButton2:
159                     return MouseButton.XButton2;
160             }
161             throw new InvalidOperationException();
162         }
163 
164         #endregion Conversion members
165     }
166 }
View Code

 

WPF实现窗口最小化到托盘,并且实现右击菜单

标签:

原文地址:http://www.cnblogs.com/gchlcc/p/4987042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!