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->Line