支持自绘画的属性编辑器(2)
2008-04-09 04:21:03来源:互联网 阅读 ()
string;var
Font: TFont;
begin
Font := TFont(GetOrdValue);
if Font = nil then
Result := inherited GetValue
else
Result := Format(''''%s, %d'''', [Font.Name, Font.Size]);
end;
我们可以画任何东西到画布上,比如图标和位图的属性编辑器是TgraphicProperty。它显示把图标属性显示为一个乏味的字符串”TIcon”。 我们可以把图标属性显示为对应的图标,这样的界面更加友好。这里我们继承一个TvisualGraphicProperty对象重载PropDrawValue来实现这一功能。
Tpicture属性的情况也是类似的,所以我们用一个公用的过程DrawGraphic来实现,DraGraphic缩放图形对象使之符合对象编辑器可用空间的大小,同时它维持原来的宽高比,缩放图像为最小的可能的尺寸。对于图标来说,由于Windows不能缩放图标,所以DrawGraphic调用StretchIcon过程把图标画到位图上,然后缩放位图。下面是过程代码:
// Windows不能缩放图标,所以如果图标大小不匹配的话,
//把它画到一个临时的位图上,然后缩放位图。
procedure StretchIcon(Canvas: TCanvas;
const Rect: TRect; Icon: TIcon);
var
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
try
Bitmap.Height := Icon.Height;
Bitmap.Width := Icon.Width;
Bitmap.Canvas.Brush.Color := clBtnFace;
Bitmap.Canvas.FillRect(Rect);
Bitmap.Canvas.Draw(0, 0, Icon);
Canvas.StretchDraw(Rect, Bitmap);
finally
Bitmap.Free;
end;
end;
procedure DrawGraphic(Canvas: TCanvas; const Rect: TRect;
Graphic: TGraphic; const Value: string);
var
R: TRect;
HeightRatio, WidthRatio: Single;
begin
Canvas.FillRect(Rect);
//缩放图像使其符合给定空间大小,
//同时保持图像宽高比不变
HeightRatio := (Rect.Bottom - Rect.Top) / Graphic.Height;
WidthRatio := (Rect.Right - Rect.Left) / Graphic.Width;
R := Rect;
if HeightRatio < WidthRatio then
R.Right := R.Left Trunc(Graphic.Width * HeightRatio)
else
R.Bottom := R.Top Trunc(Graphic.Height * WidthRatio);
if (Graphic is TIcon) and
((HeightRatio > 1) or (WidthRatio > 1)) then
StretchIcon(Canvas, R, TIcon(Graphic))
else
Canvas.StretchDraw(R, Graphic);
// 在图像的右边,让继承的编辑器画缺省的文本,比如“Ticon“
R.Left := R.Right;
R.Right := Rect.Right;
R.Top := Rect.Top;
R.Bottom := Rect.Bottom;
Canvas.TextRect(R, R.Left 1, R.Top 1, Value);
end;
我们在DrawGraphic过程中写了主要的代码,所以PropDrawValue就显得简单多了,主要的作用是确保属性有一个有效的图形对象,如果没有就调用继承的方法来处理。代码如下:
procedure TVisualGraphicProperty.PropDrawValue(
Canvas: TCanvas; const Rect: TRect; Selected: Boolean);
var
Graphic: TGraphic;
begin
Graphic := TGraphic(GetOrdValue);
if (Graphic = nil) or Graphic.Empty or
(Graphic.Height = 0) or (Graphic.Width = 0) then
inherited
else
DrawGraphic(Canvas, Rect, Graphic, GetVisualValue);
end;
自绘画的名字
我们可以重载PropDrawName方法,它同PropDrawValue方法工作方式类似,只不过一个是画值,一个是画名称。大多数属性的名字不需要任何特殊的处理,对于BoldFace属性的名字来说,把名字加粗可以便于用户了解BoldFace属性的情况。下面的代码显示了TboldComponentNameProperty类的如何实现PropDrawName方法的。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
