1. 程式人生 > >wpf C#編寫最小化時隱藏為工作列圖示的Window appllication.

wpf C#編寫最小化時隱藏為工作列圖示的Window appllication.

設定窗體屬性showinTaskbar=false

WindowState ws;

        WindowState wsl;

        NotifyIcon notifyIcon;
        #region Constructionspublic MainWindow()

        {

          

            InitializeComponent();
//顯示托盤。            icon();

           
//保證窗體顯示在上方。            wsl = WindowState;

        }

       

#endregionprivatevoid icon()

        {

           
this.notifyIcon =new NotifyIcon();

           
this.notifyIcon.BalloonTipText ="Hello, 檔案監視器"; //設定程式啟動時顯示的文字this.notifyIcon.Text ="檔案監視器";//最小化到托盤時,滑鼠點選時顯示的文字this.notifyIcon.Icon =new System.Drawing.Icon("Downloads.ico");//程式圖示this.notifyIcon.Visible =true
;

            notifyIcon.MouseDoubleClick
+= OnNotifyIconDoubleClick;

           
this.notifyIcon.ShowBalloonTip(1000);

        }



       
privatevoid OnNotifyIconDoubleClick(object sender, EventArgs e)

        {

           
this.Show();

            WindowState
= wsl;

        }



       
privatevoid Window_StateChanged(
object sender, EventArgs e)

        {

            ws
= WindowState;

           
if (ws == WindowState.Minimized)

            {

               
this.Hide();

            }

        }

可以給notifyIcon新增右鍵選單:

主窗體中拖入一個ContextMenu控制元件contextMenu1,點中控制元件,在上下文選單中新增選單,notifyIcon1的ContextMenu行為中選中contextMenu1作為上下文選單。

(可以在子選單中新增行為)