向导制作不完全手册 二(0分)

L

lqy

Unregistered / Unconfirmed
GUEST, unregistred user!
向导制作不完全手册 二
作者:李清一
为Caption属性创建一个下拉框,用于选择国家名。
一:先定义一个控件
Tproperty_Button = class(TButton)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;
可见我们只是继承了 Tbutton没有任何改变。
二:定义一个属性编辑器
Tproperty_button_capion=class(TPropertyEditor)
public
function GetAttributes: TPropertyAttributes;
override;
procedure GetValues(Proc: TGetStrProc);
override;
function GetValue: string;
override;
procedure SetValue(const Value: string);
override;
end;
由于我们不需要弹出对话框,所以不需要重载 EDIT方法。
其它方法定义如下:
function Tproperty_button_capion.GetAttributes: TPropertyAttributes;
begin
result:=[paValueList,paMultiSelect,paSortList,paAutoUpdate];

end;
定义该属性编辑器为一个下拉式的,自动排序,允许多选,自动更新(caption属性
本来就是自动更新的,详细说明在手册一中。
function Tproperty_button_capion.GetValue: string;
begin
result:=GetStrValue;
end;
在Object Inspector需要显示属性字符串值时会调用这个方法,这里
我们调用GetStrValue返回属性值(就是Caption的值),如果我们不重载这
个方法,它会在Object Inspector中显示Caption为(unknown);
procedure Tproperty_button_capion.GetValues(Proc: TGetStrProc);
begin
Proc('中国');
Proc('美国');
Proc('英国');
Proc('法国');
end;
由于我们的属性编辑器是下拉式的,所以IDE会调用GetValues来提取
下拉列表中的值。这里我们设置为一些国家名。
procedure Tproperty_button_capion.SetValue(const Value: string);
begin
inherited;
SetStrValue(value);
end;
当IDE设置属性字符串值时,会触发这个方法(我们下拉选择后IDE就会设置
属性字符串值为我们所选择的值),在这里我们要用SetStrValue
把属性值设置为字符串值,不然的话caption不会改变。
三:注册属性编辑器
procedure Register;
begin
RegisterComponents('editpanel', [Tproperty_Button]);
RegisterPropertyEditor(typeinfo(Tcaption),Tproperty_Button,'caption',Tproperty_button_capion);
end;
RegisterPropertyEditor的参数说明见手册一
现在注册控件和属性编辑器,查看Caption的变化。
四,定制属性的下拉框
现在我们定制属性的下拉框,让其在文字的左边出现国家的国旗。
其实这个功能在delphi常用,如TColor,Tcursor等属性。
相关的方法
procedure ListMeasureWidth(const Value: string;
ACanvas: TCanvas;
var AWidth: Integer);
override;
procedure ListMeasureHeight(const Value: string;
ACanvas: TCanvas;
var AHeight: Integer);
override;
procedure ListDrawValue(const Value: string;
ACanvas: TCanvas;
const ARect: TRect;
ASelected: Boolean);
override;
重载这三个方法就可以随意控制下拉框的显示了,其中ListMeasureWidth比较特别
如果你设置Awidth为 100,那当属性值输入框的宽度大于 100时,下拉框的宽度等于
输入框的宽度。如果小于100时,下拉框的宽度等于100(如果Object Inspector的宽度
小于100,就等于Object Inspector的宽度)。
下面先建立一个资源文件,定义几个高为 30 宽为 60 的Bitmap分别命名为 bmp1 bmp2
bmp3 bmp4,然后在控件单元导入这个资源文件。
重载 ListDrawValue 从而重画下拉框,
procedure Tproperty_button_capion.ListDrawValue(const Value: string;
ACanvas: TCanvas;
const ARect: TRect;
ASelected: Boolean);
var
FImage1:TBitmap;
imageindex:string;
oldbrushcolor:Tcolor;
begin

if value='中国' then
imageindex:='1'
else
if value='美国' then
imageindex:='2'
else
if value='英国' then
imageindex:='3'
else
imageindex:='4';
oldbrushcolor:=acanvas.Brush.Color;
imageindex:='bmp'+imageindex;
FImage1:=tbitmap.Create;
FImage1.handle:=loadbitmap(hInstance,pchar(imageindex));
acanvas.Brush.Bitmap:=Fimage1;
acanvas.FillRect(rect(arect.Left,arect.top,arect.left+60,arect.Bottom));
fimage1.Free;
acanvas.Brush.Color:=oldbrushcolor;
inherited ListDrawValue(Value, ACanvas,
rect(arect.Left+60,arect.top,arect.Right,arect.Bottom),
ASelected);
end;

由于图例高为30比默认为的下拉框项高大。所以我们重定项高。
procedure Tproperty_button_capion.ListMeasureHeight(const Value: string;
ACanvas: TCanvas;
var AHeight: Integer);
begin
inherited;
AHeight:=30;
end;

最后把项宽为 150
procedure Tproperty_button_capion.ListMeasureWidth(const Value: string;
ACanvas: TCanvas;
var AWidth: Integer);
begin
awidth:=150;
end;

完成上面三个方法的重载,下拉框己经完全不同了。
五:定制属性栏的值域(我已不知道这个叫什么好,只好这样叫了)
我们知道Tcolor的值中显示在object inspector中是,字符串值的左边还有一个小颜色块
现在我们让我们的Caption属性也有这个功能,在左边显示国旗图案。
实现的方法主要是重载 PropDrawValue 方法
procedure PropDrawValue(ACanvas: TCanvas;
const ARect: TRect;
ASelected: Boolean);
override;

procedure Tproperty_button_capion.PropDrawValue(ACanvas: TCanvas;
const ARect: TRect;
ASelected: Boolean);
var
FImage1:TBitmap;
imageindex:string;
oldbrushcolor:Tcolor;
begin
if value='中国' then
imageindex:='1'
else
if value='美国' then
imageindex:='2'
else
if value='英国' then
imageindex:='3'
else
imageindex:='4';
oldbrushcolor:=acanvas.Brush.Color;
imageindex:='bmp'+imageindex;
FImage1:=tbitmap.Create;
FImage1.handle:=loadbitmap(hInstance,pchar(imageindex));
acanvas.Brush.Bitmap:=Fimage1;
acanvas.StretchDraw(rect(arect.Left,arect.top,arect.left+16,arect.Bottom),FImage1);
fimage1.Free;
acanvas.Brush.Color:=oldbrushcolor;
inherited PropDrawValue(ACanvas,
rect(arect.Left+17,arect.top,arect.Right,arect.Bottom), ASelected)
end;

这就完成该功能了
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
449
import
I
I
回复
0
查看
518
import
I
顶部