当前位置:七道奇文章资讯编程技术VC/C++编程
日期:2011-03-22 13:55:00  来源:本站整理

<b>通过担当实现差别的CheckBox气势</b>[VC/C++编程]

赞助商链接



  本文“<b>通过担当实现差别的CheckBox气势</b>[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

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;
}

  以上是“<b>通过担当实现差别的CheckBox气势</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .