任務(wù)欄中的圖標(biāo)是一些進(jìn)程的快捷方式,這些進(jìn)程在計(jì)算機(jī)后臺(tái)運(yùn)行,如防病毒程序或音量控制。平時(shí)我們?cè)谑褂肣Q的時(shí)候,QQ主界面隱藏到系統(tǒng)托盤(pán)里,需要調(diào)用的時(shí)候在點(diǎn)擊出來(lái),很好很強(qiáng)大。
那么我們?cè)贑#中如何實(shí)現(xiàn)呢。本節(jié)將來(lái)一起探討一下。其實(shí)就是NotifyICon控件的作用。那么您也會(huì)問(wèn)那么,我在系統(tǒng)托盤(pán)中右鍵單擊該Icon,會(huì)彈出菜單又是怎么實(shí)現(xiàn)的呢?
其實(shí)這個(gè)是contextMenuStrip控件的作用。那么當(dāng)好友傳來(lái)信息,托盤(pán)中好友頭像Icon不斷閃爍便是,該圖片隱藏與顯示的交叉實(shí)現(xiàn)了。使用Time控件控制顯示的時(shí)間。這的確是個(gè)有趣的技巧。NotifyIcon控件提供了編寫(xiě)此功能的方法。Icon屬性定義顯示在通知區(qū)域中的圖標(biāo)。圖標(biāo)的彈出菜單由 ContextMenu屬性確定。Text屬性分配工具提示文本。要在通知區(qū)域中顯示圖標(biāo),必須將 Visible屬性設(shè)置為 true。
1.創(chuàng)建一個(gè)項(xiàng)目,向窗體中添加NotifyIcon控件和ContextMenuStrip控件;
2.為ContextMenuStrip控件添加子項(xiàng);
3.選擇NotifyIcon控件,在其屬性窗口中將ContextMenuStrip屬性設(shè)置為添加到窗體上的ContextMenuStrip控件,并為Icon屬性設(shè)置圖片。
注:必須為NotifyIcon控件的Icon屬性設(shè)置圖標(biāo),否則是看不到的
代碼:
private void 顯示ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true;
}
private void 設(shè)置ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = false;
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
效果圖:
2.圖標(biāo)的閃爍效果
在圖標(biāo)閃爍中提到,給NotifyIcon賦予一個(gè)ICON可以控制使其在任務(wù)欄顯示,閃爍的效果是加上一個(gè)空白的圖標(biāo),正常圖標(biāo)與空白圖標(biāo)的切換進(jìn)而實(shí)現(xiàn)閃爍的效果。
注:不能使用清除icon的方法,否則圖標(biāo)是在該位置清除,會(huì)引起其他圖標(biāo)的移動(dòng),空白圖標(biāo)起到占位的作用
代碼如下:
說(shuō)明:property是vs的一個(gè)資源管理功能,可以存儲(chǔ)系統(tǒng)圖標(biāo)及一些常量
private Icon blank = Properties.Resources.blank;
private Icon normal = Properties.Resources.normal;
private bool _status = true;
private bool _isBlink = false;
右鍵菜單控制圖標(biāo)是不是顯示
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
if (_isBlink == false)
{
_isBlink = true;
timer1.Enabled = true;
timer1.Start();
}
else
{
_isBlink = false;
timer1.Stop();
notifyIcon1.ShowBalloonTip(5000, "提示", "關(guān)閉閃爍效果!", ToolTipIcon.Info);
}
}
定時(shí)器中修改圖標(biāo)的狀態(tài),定時(shí)反轉(zhuǎn)圖標(biāo)
private void timer1_Tick(object sender, EventArgs e)
{
if (_status)
notifyIcon1.Icon = normal;
else
notifyIcon1.Icon = blank;
_status = !_status;
}
氣泡提示:
notifyIcon1.ShowBalloonTip(5000, "提示", "關(guān)閉閃爍效果!", ToolTipIcon.Info);
實(shí)例實(shí)踐:在文本框中輸入文字,在系統(tǒng)托盤(pán)中顯示提示氣泡信息
效果為:
具體代碼分析如下:
public partial class Main : Form{public Main(){InitializeComponent();}private void btnShow_Click(object sender, EventArgs e){string tipText = this.txtText.Text.Trim() == "" ? "No text!":this.txtText.Text.Trim();this.notifyIcon.ShowBalloonTip(1000, "Tip", tipText, ToolTipIcon.Info);//顯示時(shí)間,標(biāo)題Tip,顯示文本,顯示標(biāo)號(hào)}private void notifyIcon_MouseMove(object sender, MouseEventArgs e){// 獲得屏幕的寬Screen screen = Screen.PrimaryScreen;int screenWidth = screen.Bounds.Width;// 獲得工作區(qū)域的高int workAreaHeight = Screen.PrimaryScreen.WorkingArea.Height;// 獲得提示窗體的寬和高int toolTipWidth = Tools.GetInstance().Width;int toolTipHeight = Tools.GetInstance().Height;// 那么提示窗體的左上角坐標(biāo)就是:屏幕的寬 - 提示窗體的寬, 工作區(qū)域的高 - 提示窗體的高Tools.GetInstance().Location = new Point(screenWidth - toolTipWidth, workAreaHeight - toolTipHeight);// 顯示提示窗體Tools.GetInstance().Show();// 開(kāi)啟Timerthis.timer1.Enabled = true;}private void timer1_Tick(object sender, EventArgs e){// 關(guān)閉Timerthis.timer1.Enabled = false;// 隱藏提示窗體Tools.GetInstance().Hide();}}
下面就這個(gè)栗子在添加一個(gè)仿QQ提示框
這個(gè)窗口的代碼是:
private static Tools Instance = null;
public static Tools GetInstance()
{
if (Instance == null)
Instance = new Tools();return Instance;
}
最終效果是:
如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答!! 點(diǎn)擊進(jìn)入論壇