C#:实现托盘

1、向窗体上添加如下控件:MenuStrip menuStrip1, NotifyIcon ni_frmMain,Timer timer1, ContentMenuStrip cms_notify。其中notify中包含显示、退出等。

2、实现的代码:

        #region 托盘相关代码

        #region 私有方法 处理窗体的 显示 隐藏 关闭(退出)

        /// <summary>
        /// 显示
        /// </summary>
        private void ShowMainForm()
        {
            this.Show();
            this.WindowState = this.lastFormState;
            this.Activate();

            this.ShowInTaskbar = true;  //任务栏中显示窗体图标
            //this.ni_frmMain.Visible = false;//图盘中隐藏图标
        }

        /// <summary>
        /// 隐藏
        /// </summary>
        private void HideMainForm()
        {
            this.Hide();

            this.ShowInTaskbar = false;  //任务栏中显示窗体图标
            //this.ni_frmMain.Visible = true;//图盘中隐藏图标
        }

        /// <summary>
        /// 关闭(退出)
        /// </summary>
        private void ExitMainForm()
        {
            if (MessageBox.Show("您确定要退出主程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                this.ni_frmMain.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        #endregion

        #region 右键菜单处理,显示 隐藏 退出

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_notifyShow_Click(object sender, EventArgs e)
        {
            ShowMainForm();
        }

        /// <summary>
        /// 隐藏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_notifyHide_Click(object sender, EventArgs e)
        {
            HideMainForm();
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_notifyExit_Click(object sender, EventArgs e)
        {
            ExitMainForm();
        }

        #endregion

        #region 私有方法 双击托盘上图标时,显示窗体

        private void ni_frmMain_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                ShowMainForm();
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
                HideMainForm();
            }
        }

        #endregion

        #region 点最小化按钮时,最小化到托盘

        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
            {
                this.ShowInTaskbar = true;  //任务栏中窗体图标显示
                //this.ni_frmMain.Visible = false;
                this.lastFormState = this.WindowState;
            }
            else
            {
                HideMainForm();
            }
        }

        #endregion

        #region 窗体关闭时最小化到托盘
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //e.Cancel = true;
            //this.ShowInTaskbar = false;  //任务栏中窗体图标消失
            //this.ni_frmMain.Visible = true;
            //HideTipForm();
        }

        #endregion

        #region 图标闪烁 开启 关闭

        /// <summary>
        /// 开启
        /// </summary>
        private void StartFlicker()
        {
            this.timer1.Enabled = true;
            this.timer1.Start();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        private void CloseFlicker()
        {
            this.timer1.Stop();
            this.timer1.Enabled = false;
            this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0;
        }

        private int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (i < 1)
            {
                this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0;
                i++;
                return;
            }
            else
            {
                this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore1;
                i = 0;
            }
        }
        #endregion

        #endregion

时间: 2024-08-29 04:59:05

C#:实现托盘的相关文章

Qt实现系统托盘

        平时大多数看到的软件都是带系统托盘的,这样软件可以在后台运行,挺好的.今天看了下Qt自带的demon,中间有参考了下,实现了这个小小的功能,就将这个功能添加到了自己之前的一个项目中,效果蛮好的.界面如下所示: 右击系统托盘就会显示当前设定的各个菜单功能: 怎么样,效果还好吧.实现这个小功能厅满意的.右键点击弹出的菜单可以自行设定所需的.另外可以设定鼠标单击.双击.中间键分别按下时所触发的动作.主要代码如下所示: 1.首先需要添加头文件,Qsystem/trayIcon类.QMen

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

原版是从网上找了一位大神的,自己只是用了一点适合自己的. 具体实现 1.首先已经确认WPF中没有实现最小化托盘的类与方法,用到了winform中的程序集 using Drawing = System.Drawing;using Forms = System.Windows.Forms; 2.XAML的后代相应事件的Demo,只是为了看起来方便~!其中也包含了在任务栏中不现实图标,只在托盘中显示.双击实现窗口的还原.没用到大神写的,自己琢磨了个,令人想不到的蛋疼的后果还没出现,也就暂时这样了. 1

PYQT窗口托盘目录

#UI.py,通过UI设计师制作后直接转换为UI.py脚本 # -*- coding: utf-8 -*- from PyQt4 import QtCore, QtGui try:    _fromUtf8 = QtCore.QString.fromUtf8except AttributeError:    _fromUtf8 = lambda s: s class Ui_Form(object):    def setupUi(self, Form):        Form.setObjec

Qt系统托盘

Qt的系统托盘的使用,可比mfc中好多了!他封装了一个专门的QSystemTrayIcon类,建立系统托盘图标.其实在Qt提供的示例程序已经很不错了,$QTDIR\examples\desktop\systray在这里简单的实现一个系统托盘功能,对其系统托盘类的使用做以演示. #include <QtGui>class Window: public QWidget{    Q_OBJECT        public:    Window(); private:    void showMes

最小化到托盘,右键退出

1.添加notifyIcon1,并添加Icon图标(.ico文件) 2.添加contextMenuStrip1 3.contextMenuStrip1.Items属性添加选项 4.界面上双击选项编写事件 5.选项退出 private void toolStripMenuItem1_Click(object sender, EventArgs e) { Application.Exit(); } 6.主窗预定Resize事件 if (this.WindowState == FormWindowSt

C# NotifyIcon添加系统托盘

要求: 1 程序启动时,无系统托盘 2 程序最小化时,显示托盘,且程序隐藏 3 双击系统托盘,显示主界面,托盘隐藏 4 系统托盘右键,点击显示和退出按钮,主程序显示和退出 代码; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usin

WPF 系统托盘 图标闪烁

WPF消息通知 系统托盘,图标闪烁 1 using System.Windows.Forms; 2 3 using System.Windows.Threading; 4 5 public partial class Window : Window 6 { 7 private NotifyIcon notifyIcon; 8 DispatcherTimer icoTimer = new DispatcherTimer(); 9 string icoUrl = @"../../Red.ico&qu

Qt之自定义托盘

说起Qt,真是个不错的ui库,不仅仅ui做的好,其他方便也不差,在扩平台方便也是非常的强大.这篇文章我将会分析下qt的托盘,QSystemTrayIcon是qt的托盘类,托盘类的用途是什么我就不说了,自行百科就好,关键问题是我们要实现怎么的托盘. 说起常用的客户端软件,qq,微信等聊天工具,有这么几个托盘事件: 1.来消息图标闪烁 2.气泡消息提示 3.鼠标左键单击.左键双击.右键单击.滚动单击 上述这三种事件QSystemTrayIcon类都完全能够解决,但是托盘的hover事件却无能为力,如

DuiVision开发教程(11)-托盘图标和托盘菜单

DuiVision界面库封装了Windows托盘图标的相关操作,可以创建托盘图标,并设置图标文件.托盘的tip信息,也可以处理托盘的单击.双击.右键菜单的事件. 通过调用下面的函数可以进行托盘的初始化: DuiSystem::Instance()->InitTray(); 初始化一般放在主的事件处理类OnInit函数中,可以参考demo程序的代码.设置托盘的图标文件盒tip信息可以调用DuiSystem的SetTrayIcon.SetTrayTip函数. 托盘的右键操作是打开右键菜单,右键菜单在

最小化托盘的实现方法

在书上看到的,觉得有用,记下了. 首先,最小化托盘的基本原理是,将应用程序的主窗体隐藏,然后在托盘中绘制应用程序的图标.然后再为托盘图标添加一些事件处理. 核心函数是Shell_NotifyIcon()函数,负责向系统传递消息,添加.修改或删除托盘区的图标.原型: WINSHELLAPI BOOL WINAPI Shell_NotifyIcon( DWORD dwMessage, PNOTIFYCONDATA pnid ); 其中dwMessage标示功能,有NIM_ADD/NIM_DELETE