在我的组件中使用 TImage
Using TImage inside of My Component
我正在尝试在我的组件内部使用 Timage,但我在第 " Image.Parent:=Self;" as 'incompatible type 'TWinControl' and 'TGasTurbine'
行收到编译器错误
所以'TGasTurbine 是我的组件名称
这是我的画图程序。我该怎么办?
Procedure TGasTurbine.Paint;
var
Image: TImage;
begin
Inherited;
Image := TImage.Create(self);
Image.Parent:=Self;
Image.Height := 100;
Image.Width := 100;
Image.Stretch:=True;
Image.Picture.LoadFromFile('H:\Component\Automation\Pump.Bmp');
end;
你的组件是什么类型的?您如何描述它的 class?
如果您的控件打算成为其他控件的父控件,则它必须是 TWinControl 的祖先。
编辑:
TGraphicControl 不能包含其他组件,也不能拥有它们。
你必须选择-或从TWindowControl继承你class,或拒绝使用内部TImage,并注意图片存储和绘制(最好)。
最好描述一下你的图片使用场景。
为什么要在 Paint 过程中创建内部组件?通常它会被调用很多次,在每次重绘下,一次又一次地创建新组件(顺便说一句,没有释放)——这不是一个明智的做法。
incompatible type 'TWinControl' and 'TGasTurbine'
这告诉您控件的父控件必须派生自 TWinControl
。而您的 TGasTurbine
class 并非源自 TWinControl
。
虽然您可以更改推导,但这将是错误的解决方案。您应该删除 TImage
控件。您选择了错误的方式在自定义控件中绘制图像。
而是在您的控件中声明一个 TBitmap
类型的字段。
FBitmap: TBitmap;
在构造函数中创建并填充它:
FBitmap := TBitmap.Create;
FBitmap.LoadFromFile(...);
然后用你的 Paint
方法绘制它:
Canvas.Draw(0, 0, FBitmap);
记得在析构函数中释放位图。
您最好将图像链接为资源而不是外部文件。
你为什么还要尝试创建 TImage 组件作为你自己组件的内部组件?
相反,您应该创建 TBitmap 来存储图像数据,然后使用 StretchDraw http://docwiki.embarcadero.com/Libraries/XE7/en/Vcl.Graphics.TCanvas.StretchDraw
在您的组件 canvas 上简单地绘制该位图
如果您需要对其他图像类型的支持,您也可以使用 TPicture 作为内部组件而不是 TBitmap,这样您就可以绘制 TImage 组件支持的所有支持的图像类型,该组件也将 TPicture 作为其内部组件组件。
我正在尝试在我的组件内部使用 Timage,但我在第 " Image.Parent:=Self;" as 'incompatible type 'TWinControl' and 'TGasTurbine'
所以'TGasTurbine 是我的组件名称
这是我的画图程序。我该怎么办?
Procedure TGasTurbine.Paint;
var
Image: TImage;
begin
Inherited;
Image := TImage.Create(self);
Image.Parent:=Self;
Image.Height := 100;
Image.Width := 100;
Image.Stretch:=True;
Image.Picture.LoadFromFile('H:\Component\Automation\Pump.Bmp');
end;
你的组件是什么类型的?您如何描述它的 class?
如果您的控件打算成为其他控件的父控件,则它必须是 TWinControl 的祖先。
编辑:
TGraphicControl 不能包含其他组件,也不能拥有它们。
你必须选择-或从TWindowControl继承你class,或拒绝使用内部TImage,并注意图片存储和绘制(最好)。
最好描述一下你的图片使用场景。
为什么要在 Paint 过程中创建内部组件?通常它会被调用很多次,在每次重绘下,一次又一次地创建新组件(顺便说一句,没有释放)——这不是一个明智的做法。
incompatible type 'TWinControl' and 'TGasTurbine'
这告诉您控件的父控件必须派生自 TWinControl
。而您的 TGasTurbine
class 并非源自 TWinControl
。
虽然您可以更改推导,但这将是错误的解决方案。您应该删除 TImage
控件。您选择了错误的方式在自定义控件中绘制图像。
而是在您的控件中声明一个 TBitmap
类型的字段。
FBitmap: TBitmap;
在构造函数中创建并填充它:
FBitmap := TBitmap.Create;
FBitmap.LoadFromFile(...);
然后用你的 Paint
方法绘制它:
Canvas.Draw(0, 0, FBitmap);
记得在析构函数中释放位图。
您最好将图像链接为资源而不是外部文件。
你为什么还要尝试创建 TImage 组件作为你自己组件的内部组件?
相反,您应该创建 TBitmap 来存储图像数据,然后使用 StretchDraw http://docwiki.embarcadero.com/Libraries/XE7/en/Vcl.Graphics.TCanvas.StretchDraw
在您的组件 canvas 上简单地绘制该位图如果您需要对其他图像类型的支持,您也可以使用 TPicture 作为内部组件而不是 TBitmap,这样您就可以绘制 TImage 组件支持的所有支持的图像类型,该组件也将 TPicture 作为其内部组件组件。