WPF 系统托盘制作方案与窗体自动隐藏

//系统托盘制作(引用命名空间System.Windows.Forms.NotifyIcon)
using System;
using System.ComponentModel;
using System.Windows;
namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private System.Windows.Forms.NotifyIcon m_notifyIcon;
        public Window1()
        {
            InitializeComponent();
            // initialise code here
            m_notifyIcon = new System.Windows.Forms.NotifyIcon();
            m_notifyIcon.BalloonTipText = "The app has been minimised. Click the tray icon to show." ;
            m_notifyIcon.BalloonTipTitle = "The App" ;
            m_notifyIcon.Text = "The App" ;
            m_notifyIcon.Icon = new System.Drawing.Icon( "TheAppIcon.ico" );
            m_notifyIcon.Click += new EventHandler(m_notifyIcon_Click);
        }
        private void OnClose( object sender, CancelEventArgs args)
        {
            m_notifyIcon.Dispose();
            m_notifyIcon = null ;
        }
        private WindowState m_storedWindowState = WindowState.Normal;
        private void OnStateChanged( object sender, EventArgs args)
        {
            if (WindowState == WindowState.Minimized)
            {
                Hide();
                if (m_notifyIcon != null )
                    m_notifyIcon.ShowBalloonTip(2000);
            }
            else
                m_storedWindowState = WindowState;
        }
        private void OnIsVisibleChanged( object sender, DependencyPropertyChangedEventArgs args)
        {
            CheckTrayIcon();
        }
        private void m_notifyIcon_Click( object sender, EventArgs e)
        {
            Show();
            WindowState = m_storedWindowState;
        }
        private void CheckTrayIcon()
        {
            ShowTrayIcon(!IsVisible);
        }
        private void ShowTrayIcon( bool show)
        {
            if (m_notifyIcon != null )
                m_notifyIcon.Visible = show;
        }
    }
}
 
///窗体自动隐藏
        private bool _IsHidded = false;
        private const int BORDER = 3;
        private const int AUTOHIDETIME = 50;
        private Location location = Location.None;
        private DispatcherTimer autoHideTimer = null;
        private WindowState winState = WindowState.Normal;
        public MainWindow()
        {
            InitializeComponent();
            //自动隐藏窗体
            this.autoHideTimer = new DispatcherTimer();
            this.autoHideTimer.Interval = TimeSpan.FromMilliseconds(AUTOHIDETIME);
            this.autoHideTimer.Tick += new EventHandler(AutoHideTimer_Tick);
        }

        /// <summary>
        /// 创建一个计时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AutoHideTimer_Tick(object sender, EventArgs e)
        {
            POINT p;
            if (!GetCursorPos(out p))
            {
                return;
            }

            if (p.x >= this.Left && p.x <= (this.Left + this.ActualWidth)
                && p.y >= this.Top && p.y <= (this.Top + this.ActualHeight))
            {
                this.ShowWindow();
            }
            else
            {
                this.HideWindow();
            }
        }
        /// <summary>
        /// 当窗体位置改变时触发事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLocationChanged(EventArgs e)
        {
            if (!this._IsHidded)
            {
                if (this.Top <= 0 && this.Left <= 0)
                {
                    this.location = Location.LeftTop;
                    this.HideWindow();
                }
                else if (this.Top <= 0 && this.Left >= SystemParameters.VirtualScreenWidth - this.ActualWidth)
                {
                    this.location = Location.RightTop;
                    this.HideWindow();
                }
                else if (this.Top <= 0)
                {
                    this.location = Location.Top;
                    this.HideWindow();
                }
                else
                {
                    this.location = Location.None;
                }
            }
            base.OnLocationChanged(e);
        }

        enum Location
        {
            None,
            Top,
            LeftTop,
            RightTop
        }
        /// <summary>
        /// 隐藏窗体
        /// </summary>
        private void ShowWindow()
        {
            if (this._IsHidded)
            {
                switch (this.location)
                {
                    case Location.Top:
                    case Location.LeftTop:
                    case Location.RightTop:
                        this.Top = 0;
                        this.Topmost = false;
                        this._IsHidded = false;
                        this.UpdateLayout();
                        break;
                    case Location.None:
                        break;
                }
            }
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        private void HideWindow()
        {
            if (!this._IsHidded)
            {
                switch (this.location)
                {
                    case Location.Top:
                        this.Top = BORDER - this.ActualHeight;
                        this.Topmost = true;
                        this._IsHidded = true;
                        this.autoHideTimer.Start();
                        break;
                    case Location.LeftTop:
                        this.Left = 0;
                        this.Top = BORDER - this.ActualHeight;
                        this.Topmost = true;
                        this._IsHidded = true;
                        this.autoHideTimer.Start();
                        break;
                    case Location.RightTop:
                        this.Left = SystemParameters.VirtualScreenWidth - this.ActualWidth;
                        this.Top = BORDER - this.ActualHeight;
                        this.Topmost = true;
                        this._IsHidded = true;
                        this.autoHideTimer.Start();
                        break;
                    case Location.None:
                        break;
                }
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int x;
            public int y;
            public POINT(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值