如何在沒有畫布的控件上進行繪畫呢?比如說Tpanel,他不具備畫布的屬性在上面寫字畫畫都有困難。自己再把它包裝一下嗎?麻煩點吧!如何才能方便的進行處理呢?
看看這段代碼
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
PanelCanvas: TCanvas;
begin
PanelCanvas := TControlCanvas.Create;
TControlCanvas(PanelCanvas).Control := Panel1;
PanelCanvas.Brush.Style := bsClear;
PanelCanvas.Pen.Color := clBlue;
PanelCanvas.Rectangle(2, 2, Panel1.Width - 2, Panel1.Height - 2);
PanelCanvas.Font.Name:='楷體_GB2312';
PanelCanvas.Font.Size:=24;
PanelCanvas.TextOut(Panel1.Width div 2,Panel1.Height div 2,'OK');
PanelCanvas.Free;
end;
end.
當少量應用時可以這樣處理。當然如果要當量應用在沒有畫布的控件上繪畫就用以上的方法把原來的控件包裝一下開發一個新的控件,會更方便一些。