本文主要是介紹C#實現打造氣泡屏幕保護效果,首先說一下制作要點:1 窗口要全屏置頂 2 模擬氣泡的滾動和粘滯效果 3 支持快捷鍵ESC退出
大致就是這3個要點了,其他還有一些細節我們在程序中根據需要再看,OK,開工!
首先是全屏置頂,因為是屏幕保護嘛,這個簡單,在窗體的屬性設置里把FormBorderStyle設置為none表示無邊框,把ShowInTaskbar設置為false表示不在任務欄出現,最后一個把WindowState設置為Maximized表示最大化即可,當然可以設置TopMost為true讓窗口置頂,不過這個不是絕對的,如果有其他窗口也使用TopMost的話會讓我們失去焦點,所以我們要注冊一個快捷鍵讓程序可以退出!
模擬氣泡我們可以用Graphics類中的DrawEllipse方法來畫一個圓,當然這個圓我們可以指定不同的顏色和大小,這里重點講一下怎么模擬粘滯效果!
所謂粘滯效果相信大家到知道,膠體大家都見過吧?就是類似膠體那種有彈性并且可以在改變形狀后回復原型的那種效果,當然這里要想模擬這個效果只能說是稍微類似,DrawEllipse方法中最后兩個參數表示圓的大小,我們可以在這里做文章,由于循環的速度很快,我們只要動態改變圓的大小就可以產生類似粘滯的效果,當然這個改變大小的參數不能太大,否則就無效了!
我們在onpaint事件中寫入如下代碼來繪制一些圓:
Random ra = new Random(); //初始化隨機數 bmp = new Bitmap(ClientSize.Width,ClientSize.Height, e.Graphics); Graphics bmpGraphics = Graphics.FromImage(bmp); // 繪制圓形 for (int i=1;i<=13;i++)//這里繪制13個圓形 { bmpGraphics.DrawEllipse(new Pen(Color.FromName(colours[i]),2),//根據事先定義好的顏色繪制不同顏色的圓 ballarray[i, 1], ballarray[i, 2], 70+ra.Next(1, 10), 70+ra.Next(1, 10)); //注意上面的最后兩個參數利用隨機數產生粘滯效果 } e.Graphics.DrawImageUnscaled(bmp, 0, 0); bmpGraphics.Dispose(); bmp.Dispose();//這里是非托管的垃圾回收機制,避免產生內存溢出
這樣,通過以上代碼就可以繪制出一些不同顏色的具有粘滯效果的圓來模擬氣泡
下面是注冊系統熱鍵,有個API函數RegisterHotKey可以完成系統快捷鍵的注冊,使用他之前我們要先引用一個系統的DLL文件:USER32.DLL,然后對這個RegisterHotKey函數進行一下聲明:
[DllImport("user32.dll")]//引用USER32.DLL public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk); //聲明函數原型
由于引用了一個DLL文件,我們不要忘了在文件頭加入DLLImport的類聲明using System.Runtime.InteropServices;然后在Form1的構造函數中來注冊一個系統熱鍵,這里我們注冊ESC:RegisterHotKey(this.Handle, 247696411, 0, (UInt32)Keys.Escape); 通過以上步驟,我們就可以注冊一個或多個系統熱鍵,但是,注冊系統熱鍵后我們還不能立即使用,因為我們在程序中還無法對這個消息進行響應,我們重載一下默認的WndProc過程來響應我們的熱鍵消息:
protected override void WndProc(ref Message m)//注意是保護類型的過程 { const int WM_HOTKEY = 0x0312; } if (m.Msg == WM_HOTKEY & & m.WParam.ToInt32() == 247696411) //判斷熱鍵消息是不是我們設置的 { Application.Exit();//如果消息等于我們的熱鍵消息,程序退出 } base.WndProc(ref m);//其他消息返回做默認處理
好了,通過以上一些步驟,我們就基本完成了這個屏幕保護程序的要點設計,其他的詳細過程可以參考源碼,程序運行的時候背景是透明的,這個也不難實現
1.this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
2.this.TransparencyKey = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
屏幕保護程序代碼如下:
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Runtime.InteropServices;/* 屏幕保護程序 使用技術:系統熱鍵,隨機數,Graphics類繪制圓形 編譯環境:VisualStudio 2005 運行要求:安裝.net framework 2.0 框架 其他:使用ESC退出 說明:由于使用了循環控制圖形位移,CPU占用在20%-30%左右 程序具有自動垃圾回收機制避免造成內存溢出 2009年3月15日 */namespace AnimatBall{ /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { public int[,] ballarray = new int[20,20]; public string[] colours = new string[16]; public Bitmap bmp; private System.Windows.Forms.Timer timer1; private System.ComponentModel.IContainer components; [DllImport("user32.dll")] public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk); //API //重寫消息循環 protected override void WndProc(ref Message m) { const int WM_HOTKEY = 0x0312; if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == 247696411) //判斷熱鍵 { Application.Exit(); } base.WndProc(ref m); } public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); //colours[0]="Red"; colours[1]="Red"; colours[2]="Blue"; colours[3]="Black"; colours[4]="Yellow"; colours[5]="Crimson"; colours[6]="Gold"; colours[7]="Green"; colours[8]="Magenta"; colours[9]="Aquamarine"; colours[10]="Brown"; colours[11]="Red"; colours[12]="DarkBlue"; colours[13]="Brown"; colours[14]="Red"; colours[15]="DarkBlue"; InitializeComponent(); RegisterHotKey(this.Handle, 247696411, 0, (UInt32)Keys.Escape); //注冊熱鍵 // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.SuspendLayout(); // // timer1 // this.timer1.Interval = 25; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192))))); this.ClientSize = new System.Drawing.Size(373, 294); this.DoubleBuffered = true; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "Form1"; this.ShowInTaskbar = false; this.Text = "小焱屏幕保護"; this.TopMost = true; this.TransparencyKey = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192))))); this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void timer1_Tick(object sender, System.EventArgs e) { for (int i=1;i<=13;i++) { //add direction vectors to coordinates ballarray[i,1] = ballarray[i,1] + ballarray[i,3]; ballarray[i,2] = ballarray[i,2] + ballarray[i,4]; //if ball goes of to right if ((ballarray[i,1]+50)>=ClientSize.Width) { ballarray[i,1]=ballarray[i,1]-ballarray[i,3]; ballarray[i,3]=-ballarray[i,3]; } //if ball goes off bottom else if ((ballarray[i,2]+50)>=ClientSize.Height) { ballarray[i,2]=ballarray[i,2]-ballarray[i,4]; ballarray[i,4]=-ballarray[i,4]; } //if ball goes off to left else if (ballarray[i,1]<=1) { ballarray[i,1]=ballarray[i,1]-ballarray[i,3]; ballarray[i,3]=-ballarray[i,3]; } //if ball goes over top else if (ballarray[i,2]<=1) { ballarray[i,2]=ballarray[i,2]-ballarray[i,4]; ballarray[i,4]=-ballarray[i,4]; } } this.Refresh(); //force repaint of window } //Called from timer event when window needs redrawing protected override void OnPaint(PaintEventArgs e) { Random ra = new Random(); bmp = new Bitmap(ClientSize.Width,ClientSize.Height, e.Graphics); Graphics bmpGraphics = Graphics.FromImage(bmp); // draw here for (int i=1;i<=13;i++) { bmpGraphics.DrawEllipse(new Pen(Color.FromName(colours[i]),2), ballarray[i, 1], ballarray[i, 2], 70+ra.Next(1, 10), 70+ra.Next(1, 10));//利用隨機數產生粘滯效果 } e.Graphics.DrawImageUnscaled(bmp, 0, 0); //Draw ellipse acording to mouse coords. bmpGraphics.Dispose(); bmp.Dispose(); } private void Form1_Load(object sender, EventArgs e) { Random r = new Random(); //set ball coords and vectors x,y,xv,yv for (int i = 1; i <= 13; i++) { ballarray[i, 1] = +r.Next(10) + 1; //+1 means i lose zero values ballarray[i, 2] = +r.Next(10) + 1; ballarray[i, 3] = +r.Next(10) + 1; ballarray[i, 4] = +r.Next(10) + 1; } timer1.Start(); } }}
TransparencyKey可以讓窗體的某個顏色透明顯示,我們只要把窗體的顏色和TransparencyKey的顏色設置一致就可以了,這里我設置的是粉紅,注意最好設置的顏色是窗體所沒有的,否則一旦匹配將會以透明顯示!
效果如下:
新聞熱點
疑難解答