C# MSN Messenger的窗口的實現淺析
C# MSN Messenger的窗口的實現是指什么呢?大家一定都用過MSN Messager了吧?每當有新郵件或者是新消息到來的時候,MSN Messager便會從右下角升起一個小窗口提醒您,然后又降下去。當你在聚精會神的在電腦上做一件事的時候,一定不會喜歡突然被"咚"一下出現在屏幕中心的對話框打擾,它的這種設計不但非常體貼用戶,而且效果還很酷。如果您寫了一個程序駐留在后臺并要求在需要的時候會提醒用戶,并且希望也能實現這種效果,那么請跟我一步一步來做下圖所示的這個仿MSN Messager的滾動提示窗口。
C# MSN Messenger的窗口的實現方法詳細:
效果示例圖
C# MSN Messenger的窗口的實現***步,建立一個Windows Application,然后在主form中放置一個Button,如下圖所示:
C# MSN Messenger的窗口的實現第二步,給這個Application添加一個窗體(Form2),把窗體的FormBorderStyle屬性設置為None(無邊框模式),然后把TopMost屬性(總在最上方)屬性設置為True,把ShowInTaskbar屬性(是否在 Windows 任務欄中顯示窗體)設置為False,并在窗體上加上你打算要顯示的文字(實際應用中一般是在程序中動態加載),將窗體的背景設置為你想要的圖片和合適的大小。***再放上三個Timer控件,其中,timer1控制窗體滾出的動畫,timer2控制窗體停留時間,timer3控制窗體的滾入動畫,將它們的Interval屬性設置為10。參見下圖
#p#
C# MSN Messenger的窗口的實現第三步,編寫代碼,在Form2中添加兩個屬性用來設置窗體的顯示大小:
- private int heightMax, widthMax;
- public int HeightMax
- {
- set
- {
- heightMax = value;
- }
- get
- {
- return heightMax;
- }
- }
- public int WidthMax
- {
- set
- {
- widthMax = value;
- }
- get
- {
- return widthMax;
- }
- }
添加一個ScrollShow的公共方法:
- public void ScrollShow()
- {
- this.Width = widthMax;
- this.Height = 0;
- this.Show();
- this.timer1.Enabled = true;
- }
添加一個StayTime屬性設置窗體停留時間(默認為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();
- }
- }
在三個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桌面的工作區
- this.timer2.Interval = StayTime;
- }
好了,滾動窗體的代碼編寫到這里就完成了,當然,它本身只實現了一個比較簡單的窗體滾動滾出效果,具體如何去應用還應該配合你的程序來完成。當然,你還可以為它添加更多的功能,比如從窗體的任意位置顯示(這里只是從右下角顯示),淡入淡出效果,加上聲音等等。最常用的就是寫一個托盤程序,然后采用這種提醒效果。
***,我們再回到Form1,在Button的Click事件中寫如下代碼來測試一下效果:
- private void button1_Click(object sender, System.EventArgs e)
- {
- Form2 form = new Form2();
- form.HeightMax = 120;//窗體滾動的高度
- form.WidthMax = 148;//窗體滾動的寬度
- form.ScrollShow();
- }
編譯并運行程序,點擊按紐,怎么樣?是不是跟MSN Messager的效果一樣,很酷吧?:)
C# MSN Messenger的窗口的實現的基本內容就向你介紹到這里,希望對你了解C# MSN Messenger的窗口的實現有所幫助。
【編輯推薦】