C#托盤程式樣本

來源:互聯網
上載者:User
先拖一個NotifyIcon控制項,注意:notifyIcon1.Icon=new Icon("Zipped 4.ico")表徵圖檔案路徑及名字,否則拋異常代碼

        public MainForm()
        {
            InitializeComponent();
            InitNotifyIcon();            
        }

        private void InitNotifyIcon()
        {
            MenuItem[] mi=new MenuItem[3];
            mi[0]=new MenuItem();
            mi[0].Text="顯示";
            mi[0].Click+= new EventHandler(this.FormShow);        
            mi[1]=new MenuItem();
            mi[1].Text="-";    
            mi[2]=new MenuItem();
            mi[2].Text="退出";
            mi[2].Click+=new EventHandler(this.FormClose);    
            ContextMenu cm=new ContextMenu(mi);

            notifyIcon1.MouseDoubleClick+=new MouseEventHandler(notifyIcon1_MouseDoubleClick);
            notifyIcon1.Icon=new Icon("Zipped 4.ico");
            notifyIcon1.Text="已運行 - \\\\資料上報程式";
            notifyIcon1.Visible=true;
            notifyIcon1.ContextMenu=cm;
        }
        private void FormShow(object sender, EventArgs e)
        {
            this.Visible=true;
        }
        private void FormClose(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible=true;
        }
        
        const int WM_SYSCOMMAND = 0x112;
        const int SC_CLOSE = 0xF060;
        const int SC_MINIMIZE = 0xF020;
        const int SC_MAXIMIZE = 0xF030;
        protected override void WndProc(ref Message m)
        {
            if(m.Msg==WM_SYSCOMMAND)
            {
                if(m.WParam.ToInt32()==SC_CLOSE)
                {
                    this.Visible=false;
                    return;
                }
                if(m.WParam.ToInt32()==SC_MINIMIZE)
                {
                    this.Visible=false;
                    return;
                }
            }
            base.WndProc(ref m);
        }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.