65.9K
CodeProject 正在变化。 阅读更多。
Home

渐变动画等待条控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.86/5 (31投票s)

2004年9月30日

1分钟阅读

viewsIcon

105515

downloadIcon

1359

当处理过程耗时较长时,此控件非常有用,可以告知用户。

Sample image - GradientAnimation-WaitBar.jpg

引言

本文演示了一个用 C# 编写的渐变等待条控件。该控件无限地动画显示由 FradientColor1GradientColor2 组合而成的渐变图像。当任何处理(工作)耗时较长时,此控件非常有用,可以告知用户当前线程正在处理工作。此控件不使用 Win32 API,仅使用 C# 函数。

代码列表

以下代码是 GradientWaitingBar

此控件的头部

//

// This control is Gradient Waiting Bar.

//

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;

属性

此控件具有以下属性

  • Speed 控件的滚动速度。
  • GradientColor1 渐变的起始颜色。
  • GradientColor2 渐变的结束颜色。
  • ScrollWay 渐变滚动的方向。
  • Interval 渐变滚动的计时器间隔。
#region "Properties"
// property of timer

[System.ComponentModel.Description("Gets or sets " + 
 "the Interval of timer tick count for Speed of GradientWaitingBar"), 
 System.ComponentModel.Category("Behavior")]
public int Interval
{
    get    {
        return m_timer.Interval;
    }
    set    {
        m_timer.Interval = value;
    }
}

// property of speed in scrolling.

[System.ComponentModel.Description("Gets or sets the " + 
 "speed of gradient for GradientWaitingBar"), 
 System.ComponentModel.Category("Behavior")]
public int Speed
{
    get
    {
        return m_nSpeed;
    }
    set
    {
        m_nSpeed = value;
    }
}
// property of begin color in linear gradient

[System.ComponentModel.Description("Gets or sets the starting" + 
 " color of the gradient for GradientWaitingBar"), 
 System.ComponentModel.Category("Appearance")]
public Color GradientColor1
{
    get
    {
        return m_col1;
    }
    set
    {
        m_col1 = value;
        MakeBitmapForWaitingBar();
    }
}

// property of end color in linear gradient

[System.ComponentModel.Description("Gets or sets the ending " + 
 "color of the gradient for GradientWaitingBar"), 
 System.ComponentModel.Category("Appearance")]
public Color GradientColor2
{
    get
    {
        return m_col2;
    }
    set
    {
        m_col2 = value;
        MakeBitmapForWaitingBar();
    }
}

// property of scrolling direction in control

[System.ComponentModel.Description("Gets or sets" + 
  " the direction of scrolling the gradient"), 
  System.ComponentModel.Category("Appearance")]
public SCROLLGRADIENTALIGN ScrollWAY
{
    get
    {
        return m_nScrollway;
    }
    set
    {
        m_nScrollway = value;
        MakeBitmapForWaitingBar();
    }
}        

#endregion

此控件为了性能而创建一个渐变位图。为了创建渐变位图,此代码使用 C# 代码,而不是 Win32 API。

//

// Make a Gradient Bitmap

// For performance, on sizing and changed Properties

// first makes a suitable bitmap image of gradient

// and then On paint this control uses it.

//

private void MakeBitmapForWaitingBar()
{
  //check the windows size

  if(this.ClientRectangle.Width == 0 || this.ClientRectangle.Height == 0)
      return;
  if(m_bitmap != null)
      m_bitmap.Dispose();
  
  Graphics gimage = null, gWnd = null;
  Brush br1, br2;
  Rectangle rt1, rt2;
  
  // Get temporary DC and make a compatible bitmap with current Windows.

  gWnd = Graphics.FromHwnd(this.Handle);
  m_bitmap = new Bitmap(this.ClientRectangle.Width, 
                 this.ClientRectangle.Height, gWnd);
  gWnd.Dispose();
  
  if(m_nScrollway == SCROLLGRADIENTALIGN.HORIZONTAL)
  {
      rt1 = this.ClientRectangle;
      rt1.Width = this.ClientRectangle.Width/2+1;
  
      rt2 = rt1;
      rt2.X = this.ClientRectangle.Width/2;
      rt2.Width = this.ClientRectangle.Width/2+1;
      br1 = new LinearGradientBrush(rt1, m_col1, m_col2, 
                          LinearGradientMode.Horizontal);
      br2 = new LinearGradientBrush(rt2, m_col2, m_col1, 
                          LinearGradientMode.Horizontal);
  }
  else
  {
      rt1 = this.ClientRectangle;
      rt1.Height = this.ClientRectangle.Height/2 + 1;
  
      rt2 = rt1;
      rt2.Y = this.ClientRectangle.Height / 2;
      rt2.Height = this.ClientRectangle.Height/2 + 1;
      br1 = new LinearGradientBrush(rt1, m_col1, m_col2, 
                               LinearGradientMode.Vertical);
      br2 = new LinearGradientBrush(rt2, m_col2, m_col1, 
                               LinearGradientMode.Vertical);
  }
  
  // make a new bitmap

  gimage = Graphics.FromImage(m_bitmap);
  gimage.FillRectangle(br2, rt2);
  gimage.FillRectangle(br1, rt1);
  gimage.Dispose();
}

如何使用

您可以根据您的想法进行开发。您也可以重写上面的代码,例如编写对齐方式、边框样式等。如果您想使用此控件,非常简单。代码如下所示

......
using KDHLib.Controls.GradientWaitingBar;

public class Form1 : System.Windows.Forms.Form
{
    private System.ComponentModel.IContainer components;
    private KDHLib.Controls.GradientWaitingBar m_bar = null;
    
    public Form1()
    {
        InitializeComponent();
        this.m_bar = new KDHLib.Controls.GradientWaitingBar();
        
        this.m_bar.BackColor = System.Drawing.Color.White;
        // set the gradient colors

        this.m_bar.GradientColor1 = System.Drawing.Color.LightPink;
        this.m_bar.GradientColor2 = System.Drawing.Color.GhostWhite;
        this.m_bar.Interval = 100;
        this.m_bar.Location = new System.Drawing.Point(384, 16);
        // Make a vertical Gradient scrolling bar

        this.m_bar.ScrollWAY = 
          KDHLib.Controls.GradientWaitingBar.SCROLLGRADIENTALIGN.VERTICAL;
        this.m_bar.Size = new System.Drawing.Size(16, 144);
        
        // set the speed

        this.m_bar.Speed = 5;
        this.m_bar.TabIndex = 0;
        this.Controls.Add(this.m_bar);
    }
    ......
}

结论

这是一个非常简单的控件,但我认为它非常有用。非常感谢您的阅读。

© . All rights reserved.