*本文大部分都参考了MSDN的文章,非原创

在创建自行绘制的控件时,应从 System.Windows.Forms.Control 中派生,并覆盖 OnPaintOnPaintBackground 事件。 下面是一个最简单的例子:
 1创建ImageButton自定义控件using System;
 2创建ImageButton自定义控件using System.Collections.Generic;
 3创建ImageButton自定义控件using System.ComponentModel;
 4创建ImageButton自定义控件using System.Data;
 5创建ImageButton自定义控件using System.Drawing;
 6创建ImageButton自定义控件using System.Text;
 7创建ImageButton自定义控件using System.Windows.Forms;
 8创建ImageButton自定义控件
 9创建ImageButton自定义控件namespace Animation
10

当然,现在这个ImageButton除了能显示一张图片以外什么也不能做,我将在后面一步一步完善它。
OnPaint 事件的特性是它可以根据操作系统的要求无限次调用。在实例化和销毁这些对象上浪费时间将会影响绘图性能。 出现所谓的“闪烁”现象,通常采用“双缓冲”来减小这一因素的影响:
创建ImageButton自定义控件private Bitmap imageBuffer;
创建ImageButton自定义控件
创建ImageButton自定义控件
protected override void OnPaint(PaintEventArgs e)
}
在上面的代码中,我们通过调用 Graphics 类的静态 FromImage 方法在与我们的控件大小相同的空位图中创建了一个 Graphics 对象。我们在内存中的 Graphics 对象上进行所有的绘图,完成后,将整个准备好的位图覆盖到控件的图形上即可。源代码如下:
创建ImageButton自定义控件using System;
创建ImageButton自定义控件
using System.Collections.Generic;
创建ImageButton自定义控件
using System.ComponentModel;
创建ImageButton自定义控件
using System.Data;
创建ImageButton自定义控件
using System.Drawing;
创建ImageButton自定义控件
using System.Text;
创建ImageButton自定义控件
using System.Windows.Forms;
创建ImageButton自定义控件
创建ImageButton自定义控件
namespace Animation

然而,陈现在控件上的一张图片,我们如何让其显示透明图像,为了使图像透明,我们使用下面这个函数来获取图片的背景颜色:
创建ImageButton自定义控件private Color ImageBackColor(Image image)
}
然后使用 创建ImageButton自定义控件using System.Drawing.Imaging;

相关文章: