由于Delphi6没有提供在画布上画渐变图的函数,对于很多程序员来
说还得自己编写代码(很不方便),1、画渐变背景;
2、写文字;
3、画圆角边框;
4、把矩形按钮变成圆角;
下面启动你的Delphi,新建一个组件,我们就要在这个新的组件中编
写代码,
信息如下:
单元名:JHLButton
继承自:TButton
类名:TJHLButton
组件夹:DelphiDemos
该文件内容如下:
unit JHLButton;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls;
type
TJHLButton = class(TButton)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('DelphiDemos', [TJHLButton]);
end;
end.
如果把组件安装以后,就可以在“DelphiDemos”组件夹下找到按钮
组件TJHLButton 。
每个按钮都有“Invalidate;”过程,当调用按钮的这个过程后,系
统自动调用“WMPaint();”,我们就要在“WMPaint();”
中编写代码,从而达到在按钮表面绘图的目的。
由于要在按钮表面绘图,所以需要用到Graphics.pas,这就决定着要
把“Graphics”添加到uses部分,修改后的uses部分如下:
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics;
接着要在Private部分添加函数:WMPaint(); 并且在implementation
部分编写这个函数的内容,如下所示:
……
private
{ Private declarations }
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
……
implementation
procedure TJHLButton.WMPaint(var Message: TWMPaint);
var b:TBitmap; c:TControlCanvas;
begin
inherited;
//0.准备一张要画到按钮表面的位图
b:=TBitmap.Create;
b.Width:=width; b.Height:=height;//这张图片的尺寸要和按钮的尺寸一样
//1.在b上画按钮表面的第一部分:背景图
//2.在b上画按钮表面的第二部分:文字
//3.在b上画按钮表面的第三部分:边框
//4.在b上画按钮表面的第四部分:表示按钮拥有焦点的矩形虚线框
//5.把b画到按钮的表面
c:=TControlCanvas.Create;
c.Control:=self;
c.Draw(0,0,b);
c.Free;
//6.释放b所占用的内存空间
b.Free;
end;
……
如果对函数“WMPaint()”比较陌生,只要通过google搜索就能得到
很多相关资料,由于“WMPaint()”属于入门级的内容,所以本文不
再重述。
上面“WMPaint()”中代码的大概意思是说:在内存中制作按钮表面
图片,然后画到按钮表面。
“0-4”步是制作按钮表面图片,第5步把制作好的图片画到按钮表
面。
注意观察“1-4”步,其中有空白的行,下面我们就用代码填充这些
空白。
这篇文章节选自:Delphi高级组件开发指南 第一篇
URL:http://www.docin.com/p-116784103.html
DelphiDemos.zip 下载:http://bigengineer.blog.ccidnet.com/blog-htm-do-showone-uid-369088-type-blog-itemid-20070074.html
文档Flash:http://www.docin.com/DocinViewer-116784103-144.swf
按钮TJHLButton组件下载:DelphiDemos.zip 
http://www.filefactory.com/file/b4he896/n/DelphiDemos.zip