Implementing different checkbox styles through inheritance

Source: Internet
Author: User
Tags bool inheritance

The Tcheckbox control in VCL displays a "√" when it is selected, but sometimes it needs to be displayed as "X", and the following code is written by Cker to achieve this effect.

Implementing different checkbox styles through inheritance

Code Author: 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 = {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::P aint ()
{
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);
}
//----------------------------------------------------------------------------
Sorry, this hint again, in order to prevent irresponsible reprint, had to leave some information here.
Author: cker
This paper studies from 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::D oenter ()
{
Tcustomcontrol::D oenter ();
Ffocused = true;
Paint ();
}
void __fastcall Txcheckbox::D oexit ()
{
Tcustomcontrol::D oexit ();
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;
}
}
Test code:
__fastcall Tform1::tform1 (tcomponent* Owner)
: Tform (Owner)
{
txcheckbox* x = new Txcheckbox (this);
X->parent = this;
x->caption = "Chinese test 中文版";
X->checkcolor = clred;
X->left = 100;
X->top = 100;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.