• C#窗体的最大化/最小化/还原/最小化到托盘/NotifyIcon的代码实现


    1.最小化

    WindowState = FormWindowState.Minimized;

    //最小化时隐藏窗体

    this.Visible=false;

    2.最大化

    WindowState == FormWindowState.Maximized;

    3.还原为正常

    WindowState == FormWindowState.Normal;

    4.在托盘显示

       打开 VS.net的工具箱,然后选择NotifyIcon,拖到Form上,在属性中Icon中设置图象添加一个ContextMenu,输入需要的选择项在 NotifyIcon中的ContextMenu属性中可以设置刚添加的ContextMenu 点托盘上的图标 就可以显示选择项  

    实例部分代码:

                  #region 还原窗体

                  private void normalForm()
                  {
                      //this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
                      //this.ClientSize = new System.Drawing.Size(504, 267);
                      this.Visible = true;
                      //this.WindowState=FormWindowState.Normal;
                  }
                  #endregion

               #region 最小化窗体并在托盘显示,隐藏窗体
               private void minForm()
               {
                   WindowState = FormWindowState.Minimized;
                   this.Visible = false;
                   this.notifyIconCMPC.Visible = true;
                   this.myTimer.Enabled = false;
                   //this.Hide();
                   //设置气球状工具提示显示的时间为10秒
                   this.notifyIconCMPC.ShowBalloonTip(30);
               }
               #endregion

                  #region         重写WndProc屏蔽掉关闭按钮
                  protected override void WndProc(ref Message m)
                  {
                      const int WM_SYSCOMMAND = 0x0112;
                      const int SC_CLOSE = 0xF060;
                      if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
                      {
                         this.Visible = false;
                         return;
                      }
                      base.WndProc(ref m);
                  }
                  #endregion

               private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
               {
                   if (this.WindowState == FormWindowState.Minimized || this.Visible == false)
                   {
                       this.normalForm();
                   }
                   else
                   {
                       minForm();
                   }
               }

      public partial class MainForm : Form
        {
            FormWindowState fws 
    = FormWindowState.Normal;

            
    public MainForm()
            {
                InitializeComponent();
                
    this.SizeChanged += new EventHandler(MainForm_SizeChanged);
                
    this.notifyIcon1.DoubleClick += new EventHandler(notifyIcon1_DoubleClick);
                
    this.notifyIcon1.Icon = new Icon("Virgo.ico");
                
    this.notifyIcon1.Visible = false;
            }

            
    void MainForm_SizeChanged(object sender, EventArgs e)
            {
                
    if (this.WindowState == FormWindowState.Minimized)
                {
                    
    this.ShowInTaskbar = false;
                    
    this.notifyIcon1.Visible = true;
                }
                
    else
                {
                    fws 
    = this.WindowState;
                }
            }

            
    void notifyIcon1_DoubleClick(object sender, EventArgs e)
            {
                
    if (this.WindowState == FormWindowState.Minimized)
                {
                    
    this.ShowInTaskbar = true;
                    
    this.notifyIcon1.Visible = false;
                    
    this.WindowState = fws;
                }
            }
        }

  • 相关阅读:
    利用 windbg 脚本动态调试代码
    GetHotkeys 通过驱动获取系统热键 [ 顺便写了 SSDT + Shadow SSDT ]
    获取系统热键链表windbg脚本 GetHotkeys windbg script
    利用SetSysColor函数实现主题修改
    Javascript文件夹选择框的两种解决方案
    Javascript使用AjaxPro构建自动补全,同时自动生成待输行【原创】
    在UpdatePanel中弹出对话框
    JS动态创建表格比较【转】
    JS精美日历时间控件
    myeclipse快捷键大全【转】
  • 原文地址:https://www.cnblogs.com/goody9807/p/1305065.html
Copyright © 2020-2023  润新知