C# MSN Messenger的窗口的實(shí)現(xiàn)淺析
C# MSN Messenger的窗口的實(shí)現(xiàn)是指什么呢?大家一定都用過MSN Messager了吧?每當(dāng)有新郵件或者是新消息到來的時(shí)候,MSN Messager便會(huì)從右下角升起一個(gè)小窗口提醒您,然后又降下去。當(dāng)你在聚精會(huì)神的在電腦上做一件事的時(shí)候,一定不會(huì)喜歡突然被"咚"一下出現(xiàn)在屏幕中心的對(duì)話框打擾,它的這種設(shè)計(jì)不但非常體貼用戶,而且效果還很酷。如果您寫了一個(gè)程序駐留在后臺(tái)并要求在需要的時(shí)候會(huì)提醒用戶,并且希望也能實(shí)現(xiàn)這種效果,那么請(qǐng)跟我一步一步來做下圖所示的這個(gè)仿MSN Messager的滾動(dòng)提示窗口。
C# MSN Messenger的窗口的實(shí)現(xiàn)方法詳細(xì):
效果示例圖
C# MSN Messenger的窗口的實(shí)現(xiàn)***步,建立一個(gè)Windows Application,然后在主form中放置一個(gè)Button,如下圖所示:
C# MSN Messenger的窗口的實(shí)現(xiàn)第二步,給這個(gè)Application添加一個(gè)窗體(Form2),把窗體的FormBorderStyle屬性設(shè)置為None(無邊框模式),然后把TopMost屬性(總在最上方)屬性設(shè)置為True,把ShowInTaskbar屬性(是否在 Windows 任務(wù)欄中顯示窗體)設(shè)置為False,并在窗體上加上你打算要顯示的文字(實(shí)際應(yīng)用中一般是在程序中動(dòng)態(tài)加載),將窗體的背景設(shè)置為你想要的圖片和合適的大小。***再放上三個(gè)Timer控件,其中,timer1控制窗體滾出的動(dòng)畫,timer2控制窗體停留時(shí)間,timer3控制窗體的滾入動(dòng)畫,將它們的Interval屬性設(shè)置為10。參見下圖
#p#
C# MSN Messenger的窗口的實(shí)現(xiàn)第三步,編寫代碼,在Form2中添加兩個(gè)屬性用來設(shè)置窗體的顯示大小:
- private int heightMax, widthMax;
- public int HeightMax
- {
- set
- {
- heightMax = value;
- }
- get
- {
- return heightMax;
- }
- }
- public int WidthMax
- {
- set
- {
- widthMax = value;
- }
- get
- {
- return widthMax;
- }
- }
添加一個(gè)ScrollShow的公共方法:
- public void ScrollShow()
- {
- this.Width = widthMax;
- this.Height = 0;
- this.Show();
- this.timer1.Enabled = true;
- }
添加一個(gè)StayTime屬性設(shè)置窗體停留時(shí)間(默認(rèn)為5秒):
- public int StayTime = 5000;
添加ScrollUp和ScrollDown方法來編寫窗體如何滾出和滾入:
- private void ScrollUp()
- {
- if(Height < heightMax)
- {
- this.Height += 3;
- this.Location = new Point(this.Location.X, this.Location.Y - 3);
- }
- else
- {
- this.timer1.Enabled = false;
- this.timer2.Enabled = true;
- }
- }
- private void ScrollDown()
- {
- if(Height > 3)
- {
- this.Height -= 3;
- this.Location = new Point(this.Location.X, this.Location.Y + 3);
- }
- else
- {
- this.timer3.Enabled = false;
- this.Close();
- }
- }
在三個(gè)Timer的Tick方法中分別寫入:
- private void timer1_Tick(object sender, System.EventArgs e)
- {
- ScrollUp();
- }
- private void timer2_Tick(object sender, System.EventArgs e)
- {
- timer2.Enabled = false;
- timer3.Enabled = true;
- }
- private void timer3_Tick(object sender, System.EventArgs e)
- {
- ScrollDown();
- }
在Form2的Load事件中初始化窗體變量:
- private void Form2_Load(object sender, System.EventArgs e)
- {
- Screen[] screens = Screen.AllScreens;
- Screen screen = screens[0];//獲取屏幕變量
- this.Location = new Point(
- screen.WorkingArea.Width - widthMax - 20,
- screen.WorkingArea.Height - 34);//WorkingArea為Windows桌面的工作區(qū)
- this.timer2.Interval = StayTime;
- }
好了,滾動(dòng)窗體的代碼編寫到這里就完成了,當(dāng)然,它本身只實(shí)現(xiàn)了一個(gè)比較簡(jiǎn)單的窗體滾動(dòng)滾出效果,具體如何去應(yīng)用還應(yīng)該配合你的程序來完成。當(dāng)然,你還可以為它添加更多的功能,比如從窗體的任意位置顯示(這里只是從右下角顯示),淡入淡出效果,加上聲音等等。最常用的就是寫一個(gè)托盤程序,然后采用這種提醒效果。
***,我們?cè)倩氐紽orm1,在Button的Click事件中寫如下代碼來測(cè)試一下效果:
- private void button1_Click(object sender, System.EventArgs e)
- {
- Form2 form = new Form2();
- form.HeightMax = 120;//窗體滾動(dòng)的高度
- form.WidthMax = 148;//窗體滾動(dòng)的寬度
- form.ScrollShow();
- }
編譯并運(yùn)行程序,點(diǎn)擊按紐,怎么樣?是不是跟MSN Messager的效果一樣,很酷吧?:)
C# MSN Messenger的窗口的實(shí)現(xiàn)的基本內(nèi)容就向你介紹到這里,希望對(duì)你了解C# MSN Messenger的窗口的實(shí)現(xiàn)有所幫助。
【編輯推薦】