栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

本文主要是介绍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;

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

TransparencyKey可以让窗体的某个颜色透明显示,我们只要把窗体的颜色和TransparencyKey的颜色设置一致就可以了,这里我设置的是粉红,注意最好设置的颜色是窗体所没有的,否则一旦匹配将会以透明显示!

效果如下:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号