VCL中的TCheckBox控件在選中時顯示的是一個“√”,但有的時候需要顯示為“×”,下面這段代碼是cker寫的,實現了這個效果。
// 通過繼承實現不同的CheckBox風格
// 代碼作者:cker
//---------------------------------------------------------------------------
class TXCheckBox : public TCustomControl
{
private:
bool FDown;
TCheckBoxState FState;
bool FFocused;
TColor FCheckColor;
public:
__fastcall TXCheckBox(TComponent* AOwner);
protected:
void __fastcall Paint();
DYNAMIC void __fastcall MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y);
DYNAMIC void __fastcall MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y);
DYNAMIC void __fastcall MouseMove(TShiftState Shift, int X, int Y);
DYNAMIC void __fastcall KeyDown(Word &Key, TShiftState Shift);
DYNAMIC void __fastcall KeyUp(Word &Key, TShiftState Shift);
void __fastcall SetDown( bool Value);
void __fastcall SetState(TCheckBoxState Value);
void __fastcall SetChecked( bool Value);
bool __fastcall GetChecked();
void __fastcall SetCheckColor(TColor Value);
void __fastcall SetCaption( const TCaption& Value);
TCaption __fastcall GetCaption();
DYNAMIC void __fastcall DoEnter( void);
DYNAMIC void __fastcall DoExit( void);
__published:
__property TCaption Caption = {read=GetCaption, write=SetCaption};
__property TColor CheckColor = {read=FCheckColor, write=SetCheckColor, default=clBlack};
__property bool Checked = {read=GetChecked, write=SetChecked, default= false};
__property bool Down = {read=FDown, write=SetDown, default= false};
__property DragCursor;
__property DragMode;
__property Enabled;
__property Font;
__property ParentFont;
__property PopupMenu;
__property ShowHint;
__property TCheckBoxState State = {read=FState, write=SetState, default=cbUnchecked};
__property TabOrder;
__property TabStop;
__property OnClick;
__property OnDragDrop;
__property OnDragOver;
__property OnEndDrag;
__property OnKeyDown;
__property OnKeyPress;
__property OnKeyUp;
__property OnMouseDown;
__property OnMouseMove;
__property OnMouseUp;
};
__fastcall TXCheckBox::TXCheckBox(TComponent* AOwner):TCustomControl(AOwner)
{
Width = 200;
Height = 20;
ParentColor = false;
Color = clBtnFace;
}
void __fastcall TXCheckBox::Paint()
{
const int BW = 12;
int BL, BT, BR, BB;
int TX, TY, TW, TH;
TRect Rect;
Canvas->Font = Font;
BT = (Height / 2)-(BW / 2);
BB = BT + BW;
BL = 1;
BR = BW + 1;
Canvas->Brush->Color = FDown ? clBtnFace : clWhite;
Canvas->Pen->Width = 1;
Canvas->Pen->Color = clBtnFace;
Canvas->Rectangle(BL, BT, BR, BB);
Canvas->Pen->Color = clBtnShadow;
Canvas->MoveTo(BL, BB);
Canvas->LineTo(BL, BT);
Canvas->LineTo(BR, BT);
Canvas->Pen->Color = clBtnHighlight;
Canvas->LineTo(BR, BB);
Canvas->LineTo(BL, BB);
Canvas->Pen->Color = cl3DDkShadow;
Canvas->MoveTo(BL + 1, BB - 1);
Canvas->LineTo(BL + 1, BT + 1);
Canvas->LineTo(BR - 1, BT + 1);
Canvas->Pen->Color = cl3DLight;
Canvas->LineTo(BR - 1, BB - 1);
Canvas->LineTo(BL + 1, BB - 1);
Canvas->Brush->Color = clBtnFace;
TX = BR + 5;
TY = (Height / 2) + (Canvas->Font->Height / 2) + 1;
TW = Canvas->TextWidth(Caption);
TH = Canvas->TextHeight(Caption);
Canvas->TextOut(TX, TY, Caption);
switch(State)
{
case cbChecked:
{
Canvas->Pen->Color = FCheckColor;
Canvas->Pen->Width = 2;
BT--;
BB--;
Canvas->MoveTo(BL + 3, BT + 4);
Canvas->LineTo(BR - 3, BB - 2);
Canvas->MoveTo(BR - 3, BT + 4);
Canvas->LineTo(BL + 3, BB - 2);
}
break;
case cbGrayed:
{
if(Down)
{
Canvas->Pen->Color = clBtnFace;
Canvas->Brush->Color = clBtnFace;
Canvas->Rectangle(BL + 2, BT + 2, BR - 1, BB - 1);
}
Canvas->Brush->Color = clBtnShadow;
Canvas->Rectangle(BL + 2, BT + 2, BR - 1, BB - 1);
}
break;
}
Canvas->Brush->Color = clBtnFace;
Rect = Bounds(TX - 1, TY, TW + 3, TH + 1);
Canvas->FrameRect(Rect);
if(FFocused)
Canvas->DrawFocusRect(Rect);
}
//----------------------------------------------------------------------------
// 抱歉,這個提示又來了,為了防止不負責任的轉載者,只好在此留些信息。
// 作者:cker
// 本文轉自 C++Builder 研究 - http://www.ccrun.com/article/go.asp?i=631&d=qh555w
//----------------------------------------------------------------------------
TCaption __fastcall TXCheckBox::GetCaption()
{
return Text;
}
void __fastcall TXCheckBox::SetCaption( const TCaption& Value)
{
if (Text != Value) Text = Value;
Invalidate();
}
void __fastcall TXCheckBox::SetDown( bool Value)
{
if (FDown != Value)
{
FDown = Value;
Paint();
}
}
void __fastcall TXCheckBox::SetState(TCheckBoxState Value)
{
if (FState!= Value)
{
FState = Value;
Paint();
Click();
}
}
bool __fastcall TXCheckBox::GetChecked()
{
return State == cbChecked;
}
void __fastcall TXCheckBox::SetChecked( bool Value)
{
if (Value) State = cbChecked;
else State = cbUnchecked;
}
void __fastcall TXCheckBox::SetCheckColor( TColor Value)
{
FCheckColor = Value;
Paint();
}
void __fastcall TXCheckBox::DoEnter()
{
TCustomControl::DoEnter();
FFocused = true;
Paint();
}
void __fastcall TXCheckBox::DoExit()
{
TCustomControl::DoExit();
FFocused = false;
Paint();
}
void __fastcall TXCheckBox::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y)
{
SetFocus();
FFocused = true;
TCustomControl::MouseDown(Button, Shift, X, Y);
MouseCapture = true;
Down = true;
}
void __fastcall TXCheckBox::MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y)
{
MouseCapture = false;
Down = false;
if ((X>=0) && (X<=Width) && (Y>=0) && (Y<=Height))
Checked = !Checked;
TCustomControl::MouseUp(Button, Shift, X, Y);
}
void __fastcall TXCheckBox::MouseMove(TShiftState Shift, int X, int Y)
{
if (MouseCapture)
Down = (X>=0) && (X<=Width) && (Y>=0) && (Y<=Height);
TCustomControl::MouseMove(Shift, X, Y);
}
void __fastcall TXCheckBox::KeyDown(Word &Key, TShiftState Shift)
{
if (Key == VK_SPACE) Down = true;
TCustomControl::KeyDown(Key, Shift);
}
void __fastcall TXCheckBox::KeyUp(Word &Key, TShiftState Shift)
{
if (Key == VK_SPACE)
{
Down = false;
Checked = !Checked;
}
}
測試代碼:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TXCheckBox* x = new TXCheckBox( this);
x->Parent = this;
x->Caption = "中文測試english";
x->CheckColor = clRed;
x->Left = 100;
x->Top = 100;
}