原版是从网上找了一位大神的,自己只是用了一点适合自己的。
具体实现
1.首先已经确认WPF中没有实现最小化托盘的类与方法,用到了winform中的程序集
using Drawing = System.Drawing;
using Forms = System.Windows.Forms;
2.XAML的后代相应事件的Demo,只是为了看起来方便~!其中也包含了在任务栏中不现实图标,只在托盘中显示。双击实现窗口的还原。没用到大神写的,自己琢磨了个,令人想不到的蛋疼的后果还没出现,也就暂时这样了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
namespace 最小化到托盘 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { //妈蛋找了你一天,,启动后不现实任务栏图标!!!!!!!!!!!!!!!!!!!! this.ShowInTaskbar = false; InitializeComponent(); } /// <summary> /// 托盘右击菜单--上线按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MenuItem_OnlineClick(object sender, EventArgs e) { MessageBox.Show("提示上线!"); } /// <summary> /// 托盘右击菜单--离开按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MenuItem_LeaveClick(object sender, EventArgs e) { MessageBox.Show("提示暂时离开!"); } /// <summary> /// 托盘右击菜单--在忙按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MenuItem_BusyClick(object sender, EventArgs e) { MessageBox.Show("提示在忙!"); } /// <summary> /// 托盘右击菜单--请勿打扰按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MenuItem_NoBotherClick(object sender, EventArgs e) { MessageBox.Show("提示请勿打扰!"); } /// <summary> /// 托盘右击菜单--隐身按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MenuItem_HideClick(object sender, EventArgs e) { MessageBox.Show("提示隐身!"); } /// <summary> /// 托盘右击菜单--离线按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MenuItem_OffLineClick(object sender, EventArgs e) { MessageBox.Show("提示离线!"); } /// <summary> /// 托盘右击菜单--关于我们按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MenuItem_AboutUsClick(object sender, EventArgs e) { MessageBox.Show("提示关于我们!"); } /// <summary> /// 托盘小图标的双击事件--最小化的状态下双击还原 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NotificationAreaIcon_MouseDoubleClick_1(object sender, MouseButtonEventArgs e) { //if (e.ChangedButton==MouseButton.Left) //{ if (this.WindowState == WindowState.Minimized) { WindowState = WindowState.Normal; } // } } private void MenuItem_ExitClick(object sender, EventArgs e) { this.Close(); } private void Window_MouseMove_1(object sender, MouseEventArgs e) { } //以下代码想实现即时聊天的记住密码功能,度娘说要存入配置文件,没搞成功,希望看到的人帮我这个忙~ private void Button_Click_1(object sender, RoutedEventArgs e) { //点击按钮往配置文件存入信息 Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cfa.AppSettings.Settings["LogName"].Value = "123"; cfa.Save(); MessageBox.Show(ConfigurationManager.AppSettings["LogName"]); } } } |
3.最主要的是大神写的这个类,学习编程五个月,能看懂的不是太多,稍微明白点意思,但是说不上来,直接贴代码吧。以后在研究!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
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 最小化到托盘 { /// <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 > 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 } } |
from:https://www.cnblogs.com/gchlcc/p/4987042.html