C#表單的最大化/最小化/還原/最小化到托盤/NotifyIcon的代碼實現

來源:互聯網
上載者:User

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;
            }
        }
    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.