欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)打造氣泡屏幕保護(hù)效果

 更新時(shí)間:2016年10月19日 10:52:14   作者:李sir  
本文是介給大家介紹一個(gè)很好玩的小程序:氣泡屏幕保護(hù)!類似于windows的屏保功能,有需要的朋友可以參考一下。

本文主要是介紹C#實(shí)現(xiàn)打造氣泡屏幕保護(hù)效果,首先說一下制作要點(diǎn):1 窗口要全屏置頂 2 模擬氣泡的滾動(dòng)和粘滯效果 3 支持快捷鍵ESC退出

大致就是這3個(gè)要點(diǎn)了,其他還有一些細(xì)節(jié)我們?cè)诔绦蛑懈鶕?jù)需要再看,OK,開工!

首先是全屏置頂,因?yàn)槭瞧聊槐Wo(hù)嘛,這個(gè)簡(jiǎn)單,在窗體的屬性設(shè)置里把FormBorderStyle設(shè)置為none表示無邊框,把ShowInTaskbar設(shè)置為false表示不在任務(wù)欄出現(xiàn),最后一個(gè)把WindowState設(shè)置為Maximized表示最大化即可,當(dāng)然可以設(shè)置TopMost為true讓窗口置頂,不過這個(gè)不是絕對(duì)的,如果有其他窗口也使用TopMost的話會(huì)讓我們失去焦點(diǎn),所以我們要注冊(cè)一個(gè)快捷鍵讓程序可以退出!

模擬氣泡我們可以用Graphics類中的DrawEllipse方法來畫一個(gè)圓,當(dāng)然這個(gè)圓我們可以指定不同的顏色和大小,這里重點(diǎn)講一下怎么模擬粘滯效果!

所謂粘滯效果相信大家到知道,膠體大家都見過吧?就是類似膠體那種有彈性并且可以在改變形狀后回復(fù)原型的那種效果,當(dāng)然這里要想模擬這個(gè)效果只能說是稍微類似,DrawEllipse方法中最后兩個(gè)參數(shù)表示圓的大小,我們可以在這里做文章,由于循環(huán)的速度很快,我們只要?jiǎng)討B(tài)改變圓的大小就可以產(chǎn)生類似粘滯的效果,當(dāng)然這個(gè)改變大小的參數(shù)不能太大,否則就無效了!

我們?cè)趏npaint事件中寫入如下代碼來繪制一些圓:

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個(gè)圓形 
   { 
     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)); 
      //注意上面的最后兩個(gè)參數(shù)利用隨機(jī)數(shù)產(chǎn)生粘滯效果 
   } 
   e.Graphics.DrawImageUnscaled(bmp, 0, 0); 
   bmpGraphics.Dispose(); 
   bmp.Dispose();//這里是非托管的垃圾回收機(jī)制,避免產(chǎn)生內(nèi)存溢出

這樣,通過以上代碼就可以繪制出一些不同顏色的具有粘滯效果的圓來模擬氣泡

下面是注冊(cè)系統(tǒng)熱鍵,有個(gè)API函數(shù)RegisterHotKey可以完成系統(tǒng)快捷鍵的注冊(cè),使用他之前我們要先引用一個(gè)系統(tǒng)的DLL文件:USER32.DLL,然后對(duì)這個(gè)RegisterHotKey函數(shù)進(jìn)行一下聲明:

[DllImport("user32.dll")]//引用USER32.DLL 
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk); //聲明函數(shù)原型

由于引用了一個(gè)DLL文件,我們不要忘了在文件頭加入DLLImport的類聲明using System.Runtime.InteropServices;然后在Form1的構(gòu)造函數(shù)中來注冊(cè)一個(gè)系統(tǒng)熱鍵,這里我們注冊(cè)ESC:RegisterHotKey(this.Handle, 247696411, 0, (UInt32)Keys.Escape); 通過以上步驟,我們就可以注冊(cè)一個(gè)或多個(gè)系統(tǒng)熱鍵,但是,注冊(cè)系統(tǒng)熱鍵后我們還不能立即使用,因?yàn)槲覀冊(cè)诔绦蛑羞€無法對(duì)這個(gè)消息進(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) //判斷熱鍵消息是不是我們?cè)O(shè)置的 
       { 
        Application.Exit();//如果消息等于我們的熱鍵消息,程序退出 
      } 
    base.WndProc(ref m);//其他消息返回做默認(rèn)處理

好了,通過以上一些步驟,我們就基本完成了這個(gè)屏幕保護(hù)程序的要點(diǎn)設(shè)計(jì),其他的詳細(xì)過程可以參考源碼,程序運(yùn)行的時(shí)候背景是透明的,這個(gè)也不難實(shí)現(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
 運(yùn)行要求:安裝.net framework 2.0 框架
 其他:使用ESC退出
 
 說明:由于使用了循環(huán)控制圖形位移,CPU占用在20%-30%左右
 程序具有自動(dòng)垃圾回收機(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); //注冊(cè)熱鍵
      //
      // 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可以讓窗體的某個(gè)顏色透明顯示,我們只要把窗體的顏色和TransparencyKey的顏色設(shè)置一致就可以了,這里我設(shè)置的是粉紅,注意最好設(shè)置的顏色是窗體所沒有的,否則一旦匹配將會(huì)以透明顯示!

效果如下:

相關(guān)文章

最新評(píng)論