继承TGraphicControl类产生一个新画布,画面具有自绘保持功能。相当于画板控件。
.hpp文件内容如下:
#ifndef __MYGRIPHIC
#define __MYGRIPHIC
class TMyGraphic : public TGraphicControl
{
public:
__fastcall TMyGraphic(TComponent* AOwner);
void __fastcall Paint(void);//覆盖父类方法
__property OnMouseDown;//声明覆盖父类事件
};
__fastcall TMyGraphic::TMyGraphic(TComponent *AOwner) : TGraphicControl(AOwner)
{
this->Width = 400;
this->Height = 200;
this->Left = 80;
this->Top = 60;
}
void __fastcall TMyGraphic::Paint(void)
{
Canvas->Pen->Color = clYellow;
Canvas->Brush->Color = clRed;
Canvas->Brush->Style = bsClear;
Canvas->Font->Size = 50;
Canvas->Font->Color = clGreen;
Canvas->Font->Style = Canvas->Font->Style << fsBold;
Canvas->TextOutA(12, 12, "Hello world");
}
#endif
.cpp中调用方法如下:
void __fastcall TForm1::onMyMouseDown(TObject *Sender,TMouseButton button,
TShiftState Shift,int x,int y)//自定义函数,处理自己的事件
{
ShowMessage("ok");
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMyGraphic *l1 = new TMyGraphic(Form1);
l1->Parent = this;
l1->OnMouseDown = onMyMouseDown;//事件指向自己的处理函数
}