wpf 最小化托盘

NotifyIcon 类

指定可在通知区域创建图标的组件。 此类不能被继承。



	private NotifyIcon _notifyIcon = null;
	private void Init()
        {
            _notifyIcon = new NotifyIcon();
            _notifyIcon.BalloonTipText = "Hello,模拟键盘";
            _notifyIcon.Text = "模拟键盘";
            //_notifyIcon.Icon = new System.Drawing.Icon(@"Appicon.ico");//程序图标
            _notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);//当前程序图标
            _notifyIcon.Visible = true;


            //打开菜单项
            System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
            open.Click += new EventHandler(Show);
            //隐藏菜单项
            System.Windows.Forms.MenuItem hide = new System.Windows.Forms.MenuItem("Hide");
            hide.Click += new EventHandler(Hide);
            //退出菜单项
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
            exit.Click += new EventHandler(Close);
            //关联托盘控件
            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open,hide, exit };
            _notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

            //双击图标
            _notifyIcon.MouseDoubleClick += OnMouseDoubleClickHandler;


            Hide(null,null);
        }

        private void OnMouseDoubleClickHandler(object sender, EventArgs e)
        {
            this.Show(null,null);
        }

        private void Show(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Visible;
            this.ShowInTaskbar = true;
            this.Activate();
        }

        private void Hide(object sender,EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Hidden;
        }

        private void Close(object sender, EventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值