C#实现打造气泡屏幕保护效果

  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 description for Form1.

  ///

  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

  //

  }

  ///

  /// Clean up any resources being used.

  ///

  protected override void Dispose( bool disposing )

  {

  if( disposing )

  {

  if (components != null)

  {

  components.Dispose();

  }

  }

  base.Dispose( disposing );

  }

  #region Windows Form Designer generated code

  ///

  /// Required method for Designer support - do not modify

  /// the contents of this method with the code editor.

  ///

  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

  ///

  /// The main entry point for the application.

  ///

  [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();

  }

  }

  }