• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#实现打造气泡屏幕保护效果

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

作者:李sir 字体:[增加 减小] 来源:互联网 时间:2017-05-28

李sir 通过本文主要向大家介绍了c#气泡,c#气泡提示,c#获取屏幕大小,c#获取屏幕分辨率,c#屏幕录像等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文主要是介绍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();//这里是非托管的垃圾回收机制,避免产生内存溢出
</div>

这样,通过以上代码就可以绘制出一些不同颜色的具有粘滞效果的圆来模拟气泡

下面是注册系统热键,有个API函数RegisterHotKey可以完成系统快捷键的注册,使用他之前我们要先引用一个系统的DLL文件:USER32.DLL,然后对这个RegisterHotKey函数进行一下声明:

[DllImport("user32.dll")]//引用USER32.DLL 
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk); //声明函数原型
</div>

由于引用了一个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);//其他消息返回做默认处理
</div>

好了,通过以上一些步骤,我们就基本完成了这个屏幕保护程序的要点设计,其他的详细过程可以参考源码,程序运行的时候背景是透明的,这个也不难实现

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] = ballar



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 2017-05-28C#创建线程带参数的方法
  • 2017-05-28C#利用控件拖拽技术制作拼图游戏
  • 2017-05-28C#读写config配置文件的方法
  • 2017-05-28深入理解StringBuilder的使用方法
  • 2017-05-28C#常用GDI+文字操作汇总
  • 2017-05-28C#自定义序列化ISerializable的实现方法
  • 2017-05-28c#模拟js escape方法的简单实例
  • 2017-05-28C#实现单件模式的三种常用方法
  • 2017-05-28C# 复制指定节点的所有子孙节点到新建的节点下
  • 2017-05-28C#获取数组中最大最小值的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#获取网页源代码的方法
    • c#初学简单程序实例代码介绍
    • C#字符串的常用操作工具类代码分享
    • C#获取并修改文件扩展名的方法
    • C#遍历文件夹及子目录下所有图片
    • C#实现文件上传以及多文件上传功能
    • C#中进程的挂起与恢复
    • C#中倒序输出字符串的方法示例
    • c#文件助手类分享(读取文件内容 操作日志文件)
    • C#图像处理之图像目标质心检测的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有