WPF自定義控制元件-托盤控制元件和事件
阿新 • • 發佈:2019-02-16
WPF中是沒有托盤控制元件的,可是,有時候就得用到托盤,這時候怎麼辦,這時候,就要自定義控制元件,這裡就看看自定義控制元件,這裡是我整理出來的程式碼,
public NotifyIcon notifyIcon = null; //定義個NotifyIcon類,這是托盤類,需要自己寫屬性和事件,有些麻煩,咳咳
//設定托盤的各個屬性
notifyIcon = new NotifyIcon();notifyIcon.BalloonTipText = "V_John播放器放到這裡啦!";//這是氣球提示的文字
notifyIcon.BalloonTipTitle = "小小提示";//這是氣球提示的標題
notifyIcon.Icon = new System.Drawing.Icon(@"../../Images/Music.ico");//設定托盤的圖示
notifyIcon.Visible = true;//設定是否顯示
notifyIcon.ShowBalloonTip(2000);//氣泡顯示時間
notifyIcon.Text = "V_John播發器base6.0.2 \n by: 503115254";
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);//托盤滑鼠單擊事件
//設定選單項,就是右鍵的時候,出現的選擇列表
System.Windows.Forms.MenuItem ShowMain = new System.Windows.Forms.MenuItem("顯示");
//MenuItem Setting = new MenuItem("Setting", new MenuItem[] { ShowMain });//這樣,就把ShowMain變成是Setting是子選項;
System.Windows.Forms.MenuItem playPtevMusic = new System.Windows.Forms.MenuItem("上一曲");
System.Windows.Forms.MenuItem playPause = new System.Windows.Forms.MenuItem("暫停");
System.Windows.Forms.MenuItem playNextMusic = new System.Windows.Forms.MenuItem("下一曲");
System.Windows.Forms.MenuItem aboutMe = new System.Windows.Forms.MenuItem("關於我");
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("退出");
exit.Click += new EventHandler(exit_Click);//這些事件,不是我一個一個字打出來的,有提示的,很好辨認,我要點選事件,當然是Click事件咯,然後輸入+= 就自動提示出來了,就不要管了,自動給你來個方法,有必要的話,自己到給你好的方法裡面去修改程式碼
ShowMain.Click += new EventHandler(ShowMain_Click);
playPtevMusic.Click += new EventHandler(playPtevMusic_Click);
playPause.Click += new EventHandler(playPause_Click);
playNextMusic.Click += new EventHandler(playNextMusic_Click);
aboutMe.Click += new EventHandler(aboutMe_Click);
//關聯托盤控制元件
System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { ShowMain, playPtevMusic, playPause, playNextMusic, aboutMe, exit };
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
嗯嗯,這就差不多了,如果需要別的,就自己新增吧。。。