如何实现从 TPaintBox 创建的新组件的 OnClick、OnMouseDown、OnMouseUp 事件?
How to implement OnClick, OnMouseDown, OnMouseUp Events of a new component created from TPaintBox?
我正在尝试从 TPaintBox 创建一个新组件。新组件大小为 Rect(0,0,160,248)。我在新组件上画了两个矩形,我想为上面的每个矩形实现事件。
获取矩形位于 Rect(102,43,157,63),我想为此矩形区域实现 OnGetClick、OnGetMouseDown、OnGetMouseUp 等事件。
设置矩形位于Rect(102,69,157,89),我想为这个矩形区域实现OnSetClick、OnSetMouseDown、OnSetMouseUp 等事件。
新组件区域的其余部分将显示与 VGauge 相关的值,我不包含在下面的代码中。
class PACKAGE TVGauge : public TPaintBox
{
public:
virtual void __fastcall Paint(void) ;
};
void __fastcall TVGauge::Paint(void)
{
int ind1, ind2 ;
TRect tempR ;
String str;
Canvas->Font->Size = 8 ;
//whole rect
Canvas->Pen->Color = clSilver ;
Canvas->Brush->Color = clBtnFace ;
Canvas->Rectangle(ClientRect) ; //Rect(0,0,160,248)
//----------
Canvas->Pen->Color = clMedGray ;
Canvas->Font->Color = clWindowText ;
Canvas->Brush->Color = clWhite ;
//Get Button ; draw a rectangle and it should act like a button
Canvas->Rectangle(102 , 43 , 157 , 63 ) ;
Canvas->TextOut(112, 48 ,L"Get");
//Set Button ; draw a rectangle and it should act like a button
Canvas->Rectangle(102 , 69 , 157 , 89 ) ;
Canvas->TextOut(112, 74 ,L"Set");
//display values related to the VGauge
//..
//..
if(OnPaint != NULL) OnPaint(this) ;
}
我不知道如何为新组件实现上述事件。因此,我希望获得实施此组件的建议。
覆盖虚拟MouseDown()
和MouseUp()
方法,例如:
enum TVGaugeButton { tvgGetBtn, tvgSetBtn };
typedef void __fastcall (__closure *TVGaugeMouseEvent)(TObject *Sender, TVGaugeButton GaugeButton, TMouseButton MouseButton, TShiftState Shift, int X, int Y);
typedef void __fastcall (__closure *TVGaugeClickEvent)(TObject *Sender, TVGaugeButton GaugeButton);
class PACKAGE TVGauge : public TPaintBox
{
typedef TPaintBox inherited;
private:
bool FMouseDown[2];
TVGaugeMouseEvent FOnGaugeButtonMouseDown;
TVGaugeMouseEvent FOnGaugeButtonMouseUp;
TVGaugeClickEvent FOnGaugeButtonClick;
TRect __fastcall GetButtonRect(TVGaugeButton WhichButton);
protected:
DYNAMIC void __fastcall MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y);
DYNAMIC void __fastcall MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y);
virtual void __fastcall Paint();
public:
__fastcall TVGauge(TComponent *Owner);
virtual void __fastcall SetBounds(int ALeft, int ATop, int AWidth, int AHeight);
__published:
__property TVGaugeClickEvent OnGaugeButtonClick = {read=FOnGaugeButtonClick, write=FOnGaugeButtonClick};
__property TVGaugeMouseEvent OnGaugeButtonMouseDown ={read=FOnGaugeButtonMouseDown, write=FOnGaugeButtonMouseDown};
__property TVGaugeMouseEvent OnGaugeButtonMouseUp = {read=FOnGaugeButtonMouseUp, write=FOnGaugeButtonMouseUp};
};
__fastcall TVGauge::TVGauge(TComponent *Owner)
: TPaintBox(Owner)
{
}
void __fastcall TVGauge::SetBounds(int ALeft, int ATop, int AWidth, int AHeight)
{
inherited::SetBounds(ALeft, ATop, 160, 248);
}
TRect __fastcall TVGauge::GetButtonRect(TVGaugeButton WhichButton)
{
switch (WhichButton)
{
case tvgGetBtn:
return Rect(102, 43, 157, 63);
case tvgSetBtn:
return Rect(102, 69, 157, 89);
}
return Rect(0, 0, 0, 0);
}
void __fastcall TVGauge::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y)
{
inherited::MouseDown(Button, Shift, X, Y);
for (int i = tvgGetBtn; i <= tvgSetBtn; ++i)
{
TVGaugeButton myBtn = (TVGaugeButton) i;
TRect r = GetButtonRect(myBtn);
if (PtInRect(&r, Point(X, Y)))
{
if (Button == mbLeft)
FMouseDown[i] = true;
if (FOnGaugeButtonMouseDown)
FOnGaugeButtonMouseDown(this, myBtn, Button, Shift, X-r.Left, Y-r.Top);
break;
}
}
}
void __fastcall TVGauge::MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y)
{
inherited::MouseDown(Button, Shift, X, Y);
for (int i = tvgGetBtn; i <= tvgSetBtn; ++i)
{
TVGaugeButton myBtn = (TVGaugeButton) i;
TRect r = GetButtonRect(myBtn);
if (PtInRect(&r, Point(X, Y)))
{
bool bClicked = false;
if (Button == mbLeft)
{
FMouseDown[i] = false;
bClicked = true;
}
if (FOnGaugeButtonMouseUp)
FOnGaugeButtonMouseUp(this, myBtn, Button, Shift, X-r.Left, Y-r.Top);
if ((bClicked) && (FOnGaugeButtonClick))
FOnGaugeButtonClick(this, myBtn);
break;
}
}
}
void __fastcall TVGauge::Paint()
{
TRect tempR;
Canvas->Font->Size = 8;
//whole rect
Canvas->Pen->Color = clSilver;
Canvas->Brush->Color = clBtnFace;
Canvas->Rectangle(ClientRect);
//----------
Canvas->Pen->Color = clMedGray;
Canvas->Font->Color = clWindowText;
Canvas->Brush->Color = clWhite;
//Get Button ; draw a rectangle and it should act like a button
tempR = GetButtonRect(tvgGetBtn);
Canvas->Rectangle(tempR);
Canvas->TextRect(tempR, tempR.Left + 10, tempR.Top + 5, _D("Get"));
//Set Button ; draw a rectangle and it should act like a button
tempR = GetButtonRect(tvgSetBtn);
Canvas->Rectangle(tempR);
Canvas->TextRect(tempR, tempR.Left + 10, tempR.Top + 5, _D("Set"));
//display values related to the VGauge
//..
//..
if (OnPaint)
OnPaint(this);
}
附带说明一下,您确实应该直接从 TGraphicControl
派生组件,而不是从 TPaintBox
派生组件。 TPaintBox
和 TGraphicControl
之间的 仅 区别在于 TPaintBox
公开了一个 OnPaint
事件,它覆盖了 Paint()
到触发器 OnPaint
。你根本不需要组件中的 OnPaint
事件,因为你正在做所有你自己的绘画(除非你真的希望用户在我们的绘画之上绘画)。
我正在尝试从 TPaintBox 创建一个新组件。新组件大小为 Rect(0,0,160,248)。我在新组件上画了两个矩形,我想为上面的每个矩形实现事件。
获取矩形位于 Rect(102,43,157,63),我想为此矩形区域实现 OnGetClick、OnGetMouseDown、OnGetMouseUp 等事件。
设置矩形位于Rect(102,69,157,89),我想为这个矩形区域实现OnSetClick、OnSetMouseDown、OnSetMouseUp 等事件。
新组件区域的其余部分将显示与 VGauge 相关的值,我不包含在下面的代码中。
class PACKAGE TVGauge : public TPaintBox
{
public:
virtual void __fastcall Paint(void) ;
};
void __fastcall TVGauge::Paint(void)
{
int ind1, ind2 ;
TRect tempR ;
String str;
Canvas->Font->Size = 8 ;
//whole rect
Canvas->Pen->Color = clSilver ;
Canvas->Brush->Color = clBtnFace ;
Canvas->Rectangle(ClientRect) ; //Rect(0,0,160,248)
//----------
Canvas->Pen->Color = clMedGray ;
Canvas->Font->Color = clWindowText ;
Canvas->Brush->Color = clWhite ;
//Get Button ; draw a rectangle and it should act like a button
Canvas->Rectangle(102 , 43 , 157 , 63 ) ;
Canvas->TextOut(112, 48 ,L"Get");
//Set Button ; draw a rectangle and it should act like a button
Canvas->Rectangle(102 , 69 , 157 , 89 ) ;
Canvas->TextOut(112, 74 ,L"Set");
//display values related to the VGauge
//..
//..
if(OnPaint != NULL) OnPaint(this) ;
}
我不知道如何为新组件实现上述事件。因此,我希望获得实施此组件的建议。
覆盖虚拟MouseDown()
和MouseUp()
方法,例如:
enum TVGaugeButton { tvgGetBtn, tvgSetBtn };
typedef void __fastcall (__closure *TVGaugeMouseEvent)(TObject *Sender, TVGaugeButton GaugeButton, TMouseButton MouseButton, TShiftState Shift, int X, int Y);
typedef void __fastcall (__closure *TVGaugeClickEvent)(TObject *Sender, TVGaugeButton GaugeButton);
class PACKAGE TVGauge : public TPaintBox
{
typedef TPaintBox inherited;
private:
bool FMouseDown[2];
TVGaugeMouseEvent FOnGaugeButtonMouseDown;
TVGaugeMouseEvent FOnGaugeButtonMouseUp;
TVGaugeClickEvent FOnGaugeButtonClick;
TRect __fastcall GetButtonRect(TVGaugeButton WhichButton);
protected:
DYNAMIC void __fastcall MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y);
DYNAMIC void __fastcall MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y);
virtual void __fastcall Paint();
public:
__fastcall TVGauge(TComponent *Owner);
virtual void __fastcall SetBounds(int ALeft, int ATop, int AWidth, int AHeight);
__published:
__property TVGaugeClickEvent OnGaugeButtonClick = {read=FOnGaugeButtonClick, write=FOnGaugeButtonClick};
__property TVGaugeMouseEvent OnGaugeButtonMouseDown ={read=FOnGaugeButtonMouseDown, write=FOnGaugeButtonMouseDown};
__property TVGaugeMouseEvent OnGaugeButtonMouseUp = {read=FOnGaugeButtonMouseUp, write=FOnGaugeButtonMouseUp};
};
__fastcall TVGauge::TVGauge(TComponent *Owner)
: TPaintBox(Owner)
{
}
void __fastcall TVGauge::SetBounds(int ALeft, int ATop, int AWidth, int AHeight)
{
inherited::SetBounds(ALeft, ATop, 160, 248);
}
TRect __fastcall TVGauge::GetButtonRect(TVGaugeButton WhichButton)
{
switch (WhichButton)
{
case tvgGetBtn:
return Rect(102, 43, 157, 63);
case tvgSetBtn:
return Rect(102, 69, 157, 89);
}
return Rect(0, 0, 0, 0);
}
void __fastcall TVGauge::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y)
{
inherited::MouseDown(Button, Shift, X, Y);
for (int i = tvgGetBtn; i <= tvgSetBtn; ++i)
{
TVGaugeButton myBtn = (TVGaugeButton) i;
TRect r = GetButtonRect(myBtn);
if (PtInRect(&r, Point(X, Y)))
{
if (Button == mbLeft)
FMouseDown[i] = true;
if (FOnGaugeButtonMouseDown)
FOnGaugeButtonMouseDown(this, myBtn, Button, Shift, X-r.Left, Y-r.Top);
break;
}
}
}
void __fastcall TVGauge::MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y)
{
inherited::MouseDown(Button, Shift, X, Y);
for (int i = tvgGetBtn; i <= tvgSetBtn; ++i)
{
TVGaugeButton myBtn = (TVGaugeButton) i;
TRect r = GetButtonRect(myBtn);
if (PtInRect(&r, Point(X, Y)))
{
bool bClicked = false;
if (Button == mbLeft)
{
FMouseDown[i] = false;
bClicked = true;
}
if (FOnGaugeButtonMouseUp)
FOnGaugeButtonMouseUp(this, myBtn, Button, Shift, X-r.Left, Y-r.Top);
if ((bClicked) && (FOnGaugeButtonClick))
FOnGaugeButtonClick(this, myBtn);
break;
}
}
}
void __fastcall TVGauge::Paint()
{
TRect tempR;
Canvas->Font->Size = 8;
//whole rect
Canvas->Pen->Color = clSilver;
Canvas->Brush->Color = clBtnFace;
Canvas->Rectangle(ClientRect);
//----------
Canvas->Pen->Color = clMedGray;
Canvas->Font->Color = clWindowText;
Canvas->Brush->Color = clWhite;
//Get Button ; draw a rectangle and it should act like a button
tempR = GetButtonRect(tvgGetBtn);
Canvas->Rectangle(tempR);
Canvas->TextRect(tempR, tempR.Left + 10, tempR.Top + 5, _D("Get"));
//Set Button ; draw a rectangle and it should act like a button
tempR = GetButtonRect(tvgSetBtn);
Canvas->Rectangle(tempR);
Canvas->TextRect(tempR, tempR.Left + 10, tempR.Top + 5, _D("Set"));
//display values related to the VGauge
//..
//..
if (OnPaint)
OnPaint(this);
}
附带说明一下,您确实应该直接从 TGraphicControl
派生组件,而不是从 TPaintBox
派生组件。 TPaintBox
和 TGraphicControl
之间的 仅 区别在于 TPaintBox
公开了一个 OnPaint
事件,它覆盖了 Paint()
到触发器 OnPaint
。你根本不需要组件中的 OnPaint
事件,因为你正在做所有你自己的绘画(除非你真的希望用户在我们的绘画之上绘画)。