• 系统托盘编程


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace TrayIcon
    {
        public partial class Form1 : Form
        {
            private Icon ico = new Icon("..\\..\\qq.ico");
            private NotifyIcon trayIcon;
            private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
            private System.Windows.Forms.ToolStripMenuItem showMainFrameToolStripMenuItem;
            private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;

            public Form1()
            {
                InitializeComponent();

                InitContextMenu();

                this.ShowInTaskbar = false;   //以下两句用来使窗体在启动时不显示
                this.WindowState = FormWindowState.Minimized;
                InitTrayIcon();
            }

            //private void Form1_Activated(object sender, EventArgs e)
            //{
            //    //this.Show();  //也可以用这句使窗体在启动时不显示
            //}

            private void InitTrayIcon()
            {
                trayIcon = new NotifyIcon();
                trayIcon.Icon = ico;
                trayIcon.Text = "托盘程序";
                trayIcon.Visible = true;
                //trayIcon.Click += new EventHandler(trayIcon_Click);
                trayIcon.DoubleClick += new EventHandler(trayIcon_DoubleClick);
                trayIcon.ContextMenuStrip= contextMenuStrip1;
            }

            void trayIcon_DoubleClick(object sender, EventArgs e)
            {
                // Show the form when the user double clicks on the notify icon.

                // Set the WindowState to normal if the form is minimized.
                if (this.WindowState == FormWindowState.Minimized)
                    this.WindowState = FormWindowState.Normal;

                // Activate the form.
                this.Activate();
                //throw new Exception("The method or operation is not implemented.");
            }

            //void trayIcon_Click(object sender, EventArgs e)
            //{
            //    MessageBox.Show("托盘程序");
            //    //throw new Exception("The method or operation is not implemented.");
            //}

            private void showMainFrameToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Activate();
                this.Show();
                MessageBox.Show("!!!!");
            }

            private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
            {
                trayIcon.Visible = false;
                this.Close();
                Application.Exit();
            }

            private void InitContextMenu()
            {
                //
                // contextMenuStrip1
                //
                this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.showMainFrameToolStripMenuItem,
                this.exitToolStripMenuItem});
                this.contextMenuStrip1.Name = "contextMenuStrip1";
                this.contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
                //
                // showMainFrameToolStripMenuItem
                //
                this.showMainFrameToolStripMenuItem.Name = "showMainFrameToolStripMenuItem";
                this.showMainFrameToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
                this.showMainFrameToolStripMenuItem.Text = "ShowMainFrame";
                this.showMainFrameToolStripMenuItem.Click += new System.EventHandler(this.showMainFrameToolStripMenuItem_Click);
                //
                // exitToolStripMenuItem
                //
                this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
                this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
                this.exitToolStripMenuItem.Text = "Exit";
                this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click_1);
            }

            private void Form1_SizeChanged(object sender, EventArgs e)
            {
                //if (this.WindowState == FormWindowState.Minimized)
                //{
                //    this.Hide();           //不知道为什么不能加这个,加这个以后,双击时窗口便不能得新显示。

                //}
            }

        }
    }

  • 相关阅读:
    免费下载!8套最新出炉的扁平化设计风格图标
    modern.IE – Web 开发必备的 IE 浏览器测试工具
    优秀案例!教您如何设计现代简约风格网页
    Mondrian – 开源的矢量图形 Web 应用程序
    超精美!10款专业的高质量名片设计模板
    英文字体下载大全:35款高品质英文字体
    免费下载!Twitter Bootstrap V3 矢量界面素材
    WOW.js – 在页面滚动时展现动感的元素动画效果
    Cool!12幅由小图片组成的创意图像重组作品
    InstantClick – 快速响应!让你的网站与众不同
  • 原文地址:https://www.cnblogs.com/hbhbice/p/1714316.html
Copyright © 2020-2023  润新知