[英]Delphi 7 - how to create a component with Text on the center of Image
我在創建組件時遇到問題。 我想在該圖像的中心有一個圖像和簡單標簽。 它必須是一個組件,因為我將通過代碼動態創建它。 這個怎么做? 我不知道如何將兩個組件合並為一個。
如果要將其實現為自己的組件,最快的方法可能是從TImage繼承,該方法將提供圖像所需的所有屬性,並重寫Paint方法,訪問祖先的畫布,這不會在位圖上留下任何機會。 簡短的示例不是處理Stretch,您必須自己實現。
unit CaptionImage;
interface
uses Windows, Classes, Controls, ExtCtrls, Graphics, PNGIMage, jpeg;
type
// maybe we want to do some more action on the Canvas without manipulation the Bitmap
TOnAfterpaintEvent = Procedure(Sender: TObject; Canvas: TCanvas) of object;
TGraphicControl = Class(Controls.TGraphicControl) // make canvas accessable
public
Property Canvas;
End;
TCaptionImage = Class(ExtCtrls.TImage)
private
ICanvas: TCanvas;
FOnAfterPaint: TOnAfterpaintEvent;
function GetFont: TFont;
published
public
procedure Paint; override;
published
Property OnAfterPaint: TOnAfterpaintEvent Read FOnAfterPaint Write FOnAfterPaint;
Property Caption;
Property Font: TFont read GetFont;
End;
implementation
function TCaptionImage.GetFont: TFont;
begin
Result := TGraphicControl(Self).Canvas.Font;
end;
procedure TCaptionImage.Paint;
var
s: String;
r: TRect;
begin
inherited;
r := ClientRect;
s := Caption;
ICanvas := TGraphicControl(Self).Canvas;
ICanvas.Brush.Style := bsClear;
ICanvas.Textrect(r, s, [tfVerticalCenter, tfCenter, tfSingleLine]);
if Assigned(FOnAfterPaint) then
FOnAfterPaint(Self, ICanvas);
end;
end.
一個示例用法是:
procedure TForm5.Button1Click(Sender: TObject);
begin
With TCaptionImage.Create(self) do
begin
Parent := self;
AutoSize := true;
Font.Color := clBlue;
Font.Size := 20;
Picture.LoadFromFile('C:\temp\Bild 1.png');
Caption := 'Test';
end;
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.