SlowXMainClient实现窗体最小化到系统托盘

2015-03-07 20:07:23  访问(1548) 赞(0) 踩(0)


相关下载:SlowXMainClient实现窗体最小化到系统托盘     

  • 
    
            /// <summary>
            /// 当前窗体状态
            /// </summary>
            protected FormWindowState curFormWindowState
                =
                FormWindowState.Normal;
    
            /// <summary>
            /// 窗体变化的代码
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void MainForm_SizeChanged(object sender, EventArgs e)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.Hide();
                }
                else
                {
                    // 暂存当前窗体的状态 //
                    curFormWindowState = this.WindowState;
                }
            }
    
            /// <summary>
            /// 系统托盘“显示”菜单事件 - 显示窗体
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                ShowWindowClick();
            }
    
    
            /// <summary>
            /// 显示窗体的事件
            /// </summary>
            protected void ShowWindowClick()
            {
                this.Show();
    
                // 如果是最小化的状态 //
                if (this.WindowState == FormWindowState.Minimized)
                {
                    if (curFormWindowState == FormWindowState.Maximized)
                    {
                        this.WindowState = FormWindowState.Maximized;
                    }
                    else
                    {
                        curFormWindowState = FormWindowState.Normal;
                        this.WindowState = FormWindowState.Normal;
                    }
                }
            }
    
            /// <summary>
            /// 系统托盘双击显示窗体事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void notifyIcon_Main_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                // 如果显示,则隐藏 //
                if (this.Visible)
                {
                    this.Hide();
                }
                else
                {
                    ShowWindowClick();
                }
            }
    
            /// <summary>
            /// 系统托盘“退出”菜单事件 - 窗体退出
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    

  • MainForm.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace SlowXMainClient
    {
        /// <summary>
        /// 主窗体
        /// </summary>
        public partial class MainForm : Form
        {
            /// <summary>
            /// 主窗体
            /// </summary>
            public MainForm()
            {
                InitializeComponent();
            }
    
            /// <summary>
            /// 窗体加载
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void MainForm_Load(object sender, EventArgs e)
            {
                toolStripStatusLabel_WebSite.Alignment = ToolStripItemAlignment.Right;
            }
    
    
            /// <summary>
            /// 当前窗体状态
            /// </summary>
            protected FormWindowState curFormWindowState
                =
                FormWindowState.Normal;
    
            /// <summary>
            /// 窗体变化的代码
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void MainForm_SizeChanged(object sender, EventArgs e)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.Hide();
                }
                else
                {
                    // 暂存当前窗体的状态 //
                    curFormWindowState = this.WindowState;
                }
            }
    
            /// <summary>
            /// 系统托盘“显示”菜单事件 - 显示窗体
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                ShowWindowClick();
            }
    
    
            /// <summary>
            /// 显示窗体的事件
            /// </summary>
            protected void ShowWindowClick()
            {
                this.Show();
    
                // 如果是最小化的状态 //
                if (this.WindowState == FormWindowState.Minimized)
                {
                    if (curFormWindowState == FormWindowState.Maximized)
                    {
                        this.WindowState = FormWindowState.Maximized;
                    }
                    else
                    {
                        curFormWindowState = FormWindowState.Normal;
                        this.WindowState = FormWindowState.Normal;
                    }
                }
            }
    
            /// <summary>
            /// 系统托盘双击显示窗体事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void notifyIcon_Main_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                // 如果显示,则隐藏 //
                if (this.Visible)
                {
                    this.Hide();
                }
                else
                {
                    ShowWindowClick();
                }
            }
    
            /// <summary>
            /// 系统托盘“退出”菜单事件 - 窗体退出
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void toolStripStatusLabel_WebSite_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("http://www.slowx.net");
            }
    
        }
    }
    
    

    MainForm.Designer.cs

    namespace SlowXMainClient
    {
        partial class MainForm
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
                this.toolStrip1 = new System.Windows.Forms.ToolStrip();
                this.toolStripButton_搜索 = new System.Windows.Forms.ToolStripButton();
                this.toolStripComboBox_搜索 = new System.Windows.Forms.ToolStripComboBox();
                this.toolStripDropDownButton操作 = new System.Windows.Forms.ToolStripDropDownButton();
                this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
                this.contextMenuStrip_NotifyIcon = new System.Windows.Forms.ContextMenuStrip(this.components);
                this.显示ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
                this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.notifyIcon_Main = new System.Windows.Forms.NotifyIcon(this.components);
                this.statusStrip1 = new System.Windows.Forms.StatusStrip();
                this.toolStripStatusLabel_WebSite = new System.Windows.Forms.ToolStripStatusLabel();
                this.toolStrip1.SuspendLayout();
                this.contextMenuStrip_NotifyIcon.SuspendLayout();
                this.statusStrip1.SuspendLayout();
                this.SuspendLayout();
                // 
                // toolStrip1
                // 
                this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.toolStripButton_搜索,
                this.toolStripComboBox_搜索,
                this.toolStripDropDownButton操作,
                this.toolStripSeparator1});
                this.toolStrip1.Location = new System.Drawing.Point(0, 0);
                this.toolStrip1.Name = "toolStrip1";
                this.toolStrip1.Size = new System.Drawing.Size(828, 25);
                this.toolStrip1.TabIndex = 8;
                this.toolStrip1.Text = "toolStrip1";
                // 
                // toolStripButton_搜索
                // 
                this.toolStripButton_搜索.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
                this.toolStripButton_搜索.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.toolStripButton_搜索.Name = "toolStripButton_搜索";
                this.toolStripButton_搜索.Size = new System.Drawing.Size(36, 22);
                this.toolStripButton_搜索.Text = "搜索";
                // 
                // toolStripComboBox_搜索
                // 
                this.toolStripComboBox_搜索.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
                this.toolStripComboBox_搜索.DropDownWidth = 150;
                this.toolStripComboBox_搜索.Name = "toolStripComboBox_搜索";
                this.toolStripComboBox_搜索.Size = new System.Drawing.Size(150, 25);
                // 
                // toolStripDropDownButton操作
                // 
                this.toolStripDropDownButton操作.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
                this.toolStripDropDownButton操作.Image = ((System.Drawing.Image)(resources.GetObject("toolStripDropDownButton操作.Image")));
                this.toolStripDropDownButton操作.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.toolStripDropDownButton操作.Name = "toolStripDropDownButton操作";
                this.toolStripDropDownButton操作.Size = new System.Drawing.Size(45, 22);
                this.toolStripDropDownButton操作.Text = "操作";
                // 
                // toolStripSeparator1
                // 
                this.toolStripSeparator1.Name = "toolStripSeparator1";
                this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
                // 
                // contextMenuStrip_NotifyIcon
                // 
                this.contextMenuStrip_NotifyIcon.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.显示ToolStripMenuItem,
                this.toolStripSeparator3,
                this.退出ToolStripMenuItem});
                this.contextMenuStrip_NotifyIcon.Name = "contextMenuStrip_NotifyIcon";
                this.contextMenuStrip_NotifyIcon.Size = new System.Drawing.Size(101, 54);
                // 
                // 显示ToolStripMenuItem
                // 
                this.显示ToolStripMenuItem.Name = "显示ToolStripMenuItem";
                this.显示ToolStripMenuItem.Size = new System.Drawing.Size(100, 22);
                this.显示ToolStripMenuItem.Text = "显示";
                this.显示ToolStripMenuItem.Click += new System.EventHandler(this.显示ToolStripMenuItem_Click);
                // 
                // toolStripSeparator3
                // 
                this.toolStripSeparator3.Name = "toolStripSeparator3";
                this.toolStripSeparator3.Size = new System.Drawing.Size(97, 6);
                // 
                // 退出ToolStripMenuItem
                // 
                this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem";
                this.退出ToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.Shift)
                            | System.Windows.Forms.Keys.Q)));
                this.退出ToolStripMenuItem.ShowShortcutKeys = false;
                this.退出ToolStripMenuItem.Size = new System.Drawing.Size(100, 22);
                this.退出ToolStripMenuItem.Text = "退出";
                this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click);
                // 
                // notifyIcon_Main
                // 
                this.notifyIcon_Main.ContextMenuStrip = this.contextMenuStrip_NotifyIcon;
                this.notifyIcon_Main.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon_Main.Icon")));
                this.notifyIcon_Main.Text = "霜叶工作室(www.slowx.net)";
                this.notifyIcon_Main.Visible = true;
                this.notifyIcon_Main.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon_Main_MouseDoubleClick);
                // 
                // statusStrip1
                // 
                this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.toolStripStatusLabel_WebSite});
                this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow;
                this.statusStrip1.Location = new System.Drawing.Point(0, 459);
                this.statusStrip1.Name = "statusStrip1";
                this.statusStrip1.Size = new System.Drawing.Size(828, 22);
                this.statusStrip1.TabIndex = 10;
                this.statusStrip1.Text = "statusStrip1";
                // 
                // toolStripStatusLabel_WebSite
                // 
                this.toolStripStatusLabel_WebSite.Name = "toolStripStatusLabel_WebSite";
                this.toolStripStatusLabel_WebSite.Size = new System.Drawing.Size(175, 17);
                this.toolStripStatusLabel_WebSite.Text = "霜叶工作室(www.slowx.net)";
                this.toolStripStatusLabel_WebSite.Click += new System.EventHandler(this.toolStripStatusLabel_WebSite_Click);
                // 
                // MainForm
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(828, 481);
                this.Controls.Add(this.statusStrip1);
                this.Controls.Add(this.toolStrip1);
                this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
                this.Name = "MainForm";
                this.Text = "霜叶工作室(www.slowx.net)";
                this.Load += new System.EventHandler(this.MainForm_Load);
                this.SizeChanged += new System.EventHandler(this.MainForm_SizeChanged);
                this.toolStrip1.ResumeLayout(false);
                this.toolStrip1.PerformLayout();
                this.contextMenuStrip_NotifyIcon.ResumeLayout(false);
                this.statusStrip1.ResumeLayout(false);
                this.statusStrip1.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.ToolStrip toolStrip1;
            private System.Windows.Forms.ToolStripButton toolStripButton_搜索;
            private System.Windows.Forms.ToolStripComboBox toolStripComboBox_搜索;
            private System.Windows.Forms.ToolStripDropDownButton toolStripDropDownButton操作;
            private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
            private System.Windows.Forms.ContextMenuStrip contextMenuStrip_NotifyIcon;
            private System.Windows.Forms.ToolStripMenuItem 显示ToolStripMenuItem;
            private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
            private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;
            private System.Windows.Forms.NotifyIcon notifyIcon_Main;
            private System.Windows.Forms.StatusStrip statusStrip1;
            private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel_WebSite;
        }
    }
    
    

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)