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

具体实现

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

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

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

 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[";
             cfa.Save();
             MessageBox.Show(ConfigurationManager.AppSettings["LogName"]);
         }
     }
 }

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

 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);

             )
             {
                 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
     }
 }

WPF实现窗口最小化到托盘,并且实现右击菜单的更多相关文章

  1. WinForm 之 窗口最小化到托盘及右键图标显示菜单

    Form最小化是指整个Form都缩小到任务栏上,但是窗体以Form的标题栏形式显示在任务栏上, 若是想让Form以Icon的形式显示在任务栏右下角,则需要给Form添加一个NotifyIcon控件. ...

  2. NotifyIcon 将窗口最小化到托盘

    1.设置窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标. 3.添加窗体最小化事件(首先需 ...

  3. C# WinForm窗口最小化到系统托盘

    * C# WinForm窗口最小化到系统托盘http://hi.baidu.com/kfxtgtqyapouyze/item/8ccfdcd5a174a7312a35c7c3 主要功能:(1).程序启 ...

  4. MFC窗口实现最小化到托盘 右键菜单和还原

    //.h文件 void toTray();//最小化到托盘 void DeleteTray();//删除托盘图标 afx_msg LRESULT OnShowTask(WPARAM wParam,LP ...

  5. wpf 窗口最小化后,触发某事件弹出最小化窗口并置顶

    //如果窗口最小化了弹出并置顶----事件触发调用 ShowWindowAsync(new System.Windows.Interop.WindowInteropHelper(CommonHelpe ...

  6. C#实现窗口最小化到系统托盘

    先添加notifyicon控件notifyIcon1 using System; using System.Collections.Generic; using System.ComponentMod ...

  7. C# 实现WinForm窗口最小化到系统托盘代码

    1.如果不想让程序在任务栏中显示,请把窗体的属性ShowInTaskbar设置为false; 2.如果想让程序启动时就最小化,请设置窗体的属性WindowState设置为Minimized.(Mini ...

  8. C++ 最小化到托盘

    #define WM_SHOWTASK (WM_USER + 1) void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID &a ...

  9. VC++ 最小化到托盘、恢复

    所谓的“托盘”,在Windows系统界面中,指的就是下面任务条右侧,有系统时间等等的标志的那一部分.在程序最小化或挂起时,但有不希望占据任务栏的时候,就可以把程序放到托盘区. 一.托盘编程相关函数   ...

随机推荐

  1. js开发工具箱

    昨天看到一位大牛的博客,里面有一篇文章“web前端开发分享-目录”,文章中提到的一个给前端er用的一个js开发工具箱.自己使用了一下,非常好用,代码压缩,代码美化,加密,解密之类基本功能都有,生成二维 ...

  2. 深度优先搜索 codevs 1031 质数环

    codevs 1031 质数环  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 一个大小为N(N<=17)的质数环是 ...

  3. 2014 Super Training #8 G Grouping --Tarjan求强连通分量

    原题:ZOJ 3795 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3795 题目大意:给定一个有向图,要求把点分为k个集 ...

  4. Google play billing(Google play 内支付) 上篇

    写在前面: 最近Google貌似又被全面封杀了,幸好在此之前,把Google play billing弄完了,现在写篇 博客来做下记录.这篇博客一是自己做个记录,二是帮助其他有需要的人.因为现在基本登 ...

  5. SAP中主数据和单据的删除

    在SAP实际操作的过程中,有些主数据或者单据需要删除,但是删除的方法却不尽相同,所以笔者今天总结了下,供大家参考. 1,用T-Code去删除 例如我们要删除某个物料,我们可以用T-Code MM06 ...

  6. cocoaPod相关问题

    cocoap简介: 1. 简介 CocoaPods是一个负责管理iOS项目中第三方开源代码的工具,其源码在Github上开源.使用CocoaPods可以节省设置和更新第三方开源库的时间并提高工作效率. ...

  7. 【转】【C#】ColorMatrix

    ColorMatrix(色彩矩阵),是GDI+里用来调整图片色彩的矩阵. 什么是矩阵,说白了就是C#里的二维数组. 那么这个矩阵调整色彩的原理是什么,他是怎么来调整色彩的呢?这个要从线性代数里的矩阵相 ...

  8. vim自动添加注释

    我想了下,要做好一件事, 1,首先喜欢它最才有动机去了解它 2,道听途说about那东西的,会去了解并去玩转 3,兴趣需要培养 一 添加vim头部信息. 系统:C67 追加以下代码到 /etc/vim ...

  9. C# WinForm PropertyGrid用法

    关于C# PropertyGrid的用法没有找到,找到一个C++的用法.模仿着使用了一下,感觉挺不错,分享一下.基本用法:拖个PropertyGrid,绑定一个属性类就行了. 大气象 Code hig ...

  10. python实现简易数据库之一——存储和索引建立

    最近没事做了一个数据库project,要求实现一个简单的数据库,能满足几个特定的查询,这里主要介绍一下我们的实现过程,代码放在过ithub,可参看这里.都说python的运行速度很慢,但因为时间比较急 ...