C#實現(xiàn)打造氣泡屏幕保護(hù)效果
本文主要是介紹C#實現(xiàn)打造氣泡屏幕保護(hù)效果,首先說一下制作要點:1 窗口要全屏置頂 2 模擬氣泡的滾動和粘滯效果 3 支持快捷鍵ESC退出
大致就是這3個要點了,其他還有一些細(xì)節(jié)我們在程序中根據(jù)需要再看,OK,開工!
首先是全屏置頂,因為是屏幕保護(hù)嘛,這個簡單,在窗體的屬性設(shè)置里把FormBorderStyle設(shè)置為none表示無邊框,把ShowInTaskbar設(shè)置為false表示不在任務(wù)欄出現(xiàn),最后一個把WindowState設(shè)置為Maximized表示最大化即可,當(dāng)然可以設(shè)置TopMost為true讓窗口置頂,不過這個不是絕對的,如果有其他窗口也使用TopMost的話會讓我們失去焦點,所以我們要注冊一個快捷鍵讓程序可以退出!
模擬氣泡我們可以用Graphics類中的DrawEllipse方法來畫一個圓,當(dāng)然這個圓我們可以指定不同的顏色和大小,這里重點講一下怎么模擬粘滯效果!
所謂粘滯效果相信大家到知道,膠體大家都見過吧?就是類似膠體那種有彈性并且可以在改變形狀后回復(fù)原型的那種效果,當(dāng)然這里要想模擬這個效果只能說是稍微類似,DrawEllipse方法中最后兩個參數(shù)表示圓的大小,我們可以在這里做文章,由于循環(huán)的速度很快,我們只要動態(tài)改變圓的大小就可以產(chǎn)生類似粘滯的效果,當(dāng)然這個改變大小的參數(shù)不能太大,否則就無效了!
我們在onpaint事件中寫入如下代碼來繪制一些圓:
Random ra = new Random(); //初始化隨機(jī)數(shù)
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),//根據(jù)事先定義好的顏色繪制不同顏色的圓
ballarray[i, 1], ballarray[i, 2], 70+ra.Next(1, 10), 70+ra.Next(1, 10));
//注意上面的最后兩個參數(shù)利用隨機(jī)數(shù)產(chǎn)生粘滯效果
}
e.Graphics.DrawImageUnscaled(bmp, 0, 0);
bmpGraphics.Dispose();
bmp.Dispose();//這里是非托管的垃圾回收機(jī)制,避免產(chǎn)生內(nèi)存溢出
這樣,通過以上代碼就可以繪制出一些不同顏色的具有粘滯效果的圓來模擬氣泡
下面是注冊系統(tǒng)熱鍵,有個API函數(shù)RegisterHotKey可以完成系統(tǒng)快捷鍵的注冊,使用他之前我們要先引用一個系統(tǒng)的DLL文件:USER32.DLL,然后對這個RegisterHotKey函數(shù)進(jìn)行一下聲明:
[DllImport("user32.dll")]//引用USER32.DLL
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk); //聲明函數(shù)原型
由于引用了一個DLL文件,我們不要忘了在文件頭加入DLLImport的類聲明using System.Runtime.InteropServices;然后在Form1的構(gòu)造函數(shù)中來注冊一個系統(tǒng)熱鍵,這里我們注冊ESC:RegisterHotKey(this.Handle, 247696411, 0, (UInt32)Keys.Escape); 通過以上步驟,我們就可以注冊一個或多個系統(tǒng)熱鍵,但是,注冊系統(tǒng)熱鍵后我們還不能立即使用,因為我們在程序中還無法對這個消息進(jìn)行響應(yīng),我們重載一下默認(rèn)的WndProc過程來響應(yīng)我們的熱鍵消息:
protected override void WndProc(ref Message m)//注意是保護(hù)類型的過程
{
const int WM_HOTKEY = 0x0312;
}
if (m.Msg == WM_HOTKEY & & m.WParam.ToInt32() == 247696411) //判斷熱鍵消息是不是我們設(shè)置的
{
Application.Exit();//如果消息等于我們的熱鍵消息,程序退出
}
base.WndProc(ref m);//其他消息返回做默認(rèn)處理
好了,通過以上一些步驟,我們就基本完成了這個屏幕保護(hù)程序的要點設(shè)計,其他的詳細(xì)過程可以參考源碼,程序運行的時候背景是透明的,這個也不難實現(xiàn)
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)))));
屏幕保護(hù)程序代碼如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
/*
屏幕保護(hù)程序
使用技術(shù):系統(tǒng)熱鍵,隨機(jī)數(shù),Graphics類繪制圓形
編譯環(huán)境:VisualStudio 2005
運行要求:安裝.net framework 2.0 框架
其他:使用ESC退出
說明:由于使用了循環(huán)控制圖形位移,CPU占用在20%-30%左右
程序具有自動垃圾回收機(jī)制避免造成內(nèi)存溢出
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
//重寫消息循環(huán)
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 = "小焱屏幕保護(hù)";
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));//利用隨機(jī)數(shù)產(chǎn)生粘滯效果
}
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的顏色設(shè)置一致就可以了,這里我設(shè)置的是粉紅,注意最好設(shè)置的顏色是窗體所沒有的,否則一旦匹配將會以透明顯示!
效果如下:
上一篇:C# 后臺處理圖片的幾種方法
欄 目:C#教程
本文標(biāo)題:C#實現(xiàn)打造氣泡屏幕保護(hù)效果
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6208.html
您可能感興趣的文章
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)遠(yuǎn)程關(guān)閉計算機(jī)或重啟計算機(jī)的方法
- 01-10C#自定義簽名章實現(xiàn)方法
- 01-10C#文件斷點續(xù)傳實現(xiàn)方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什


