How to draw on a control without canvas? For example, Tpanel does not have the properties of a canvas, so it is difficult to write or draw on it. Would you like to repackage it yourself? Please trouble me! How can we handle it conveniently?
Take a look at this code
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.
This can be done when applying in small quantities. Of course, if you want to apply it to a control without a canvas, it will be more convenient to use the above method to wrap the original control and develop a new control.