📄 bitbox.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "BitBox.h"
#pragma package(smart_init)
#define BOX_SIZE 16
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TBitBox *)
{
new TBitBox(NULL);
}
//---------------------------------------------------------------------------
__fastcall TBitBox::TBitBox(TComponent* Owner)
: TCustomControl(Owner)
{
TabStop = true;
FOffColor = clRed;
FOnColor = clLime;
}
//---------------------------------------------------------------------------
namespace Bitbox
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TBitBox)};
RegisterComponents("Beispiele", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::SetState (bool st)
{
FState = st;
Invalidate ();
if (FOnChange) FOnChange (this);
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::Click(void)
{
if (FState) FState = false;
else FState = true;
SetFocus ();
Invalidate ();
TCustomControl::Click ();
if (FOnChange) FOnChange (this);
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::DblClick(void)
{
if (FState) FState = false;
else FState = true;
SetFocus ();
Invalidate ();
TCustomControl::Click ();
if (FOnChange) FOnChange (this);
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::KeyPress(char &Key)
{
TCustomControl::KeyPress (Key);
if (Key == ' ')
{
if (FState) FState = false;
else FState = true;
Invalidate ();
if (FOnChange) FOnChange (this);
}
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::Paint (void)
{
int mid, t1, t2;
TCustomControl::Paint ();
mid = (Height - 1) / 2;
// Draw Background
if (Enabled) Canvas->Brush->Color = clBlack;
else Canvas->Brush->Color = (TColor)0x00111111;
Canvas->FillRect (Rect (0, mid - (BOX_SIZE / 2), BOX_SIZE, mid + (BOX_SIZE / 2)));
// Draw Bevel
Canvas->Pen->Color = clGray;
Canvas->MoveTo (0, mid + (BOX_SIZE / 2));
Canvas->LineTo (0, mid - (BOX_SIZE / 2));
Canvas->LineTo (BOX_SIZE, mid - (BOX_SIZE / 2));
Canvas->Pen->Color = (TColor)0x00555555;
Canvas->MoveTo (1, mid + (BOX_SIZE / 2) - 1);
Canvas->LineTo (1, mid - (BOX_SIZE / 2) + 1);
Canvas->LineTo (BOX_SIZE - 1, mid - (BOX_SIZE / 2) + 1);
Canvas->Pen->Color = clWhite;
Canvas->MoveTo (BOX_SIZE, mid - (BOX_SIZE / 2));
Canvas->LineTo (BOX_SIZE, mid + (BOX_SIZE / 2));
Canvas->LineTo (0, mid + (BOX_SIZE / 2));
Canvas->Pen->Color = clSilver;
Canvas->MoveTo (BOX_SIZE - 1, mid - (BOX_SIZE / 2) + 1);
Canvas->LineTo (BOX_SIZE - 1, mid + (BOX_SIZE / 2) - 1);
Canvas->LineTo (1, mid + (BOX_SIZE / 2) - 1);
// Draw Number
Canvas->Brush->Style = bsClear;
if (FState)
{
Canvas->Brush->Style = bsClear;
Canvas->Font->Name = "Courier";
if (Enabled) Canvas->Font->Color = FOnColor;
else Canvas->Font->Color = clGrayText;
Canvas->Font->Style = TFontStyles () << fsBold;
Canvas->Font->Size = 10;
t1 = mid - (Canvas->TextHeight ("1") / 2);
t2 = (BOX_SIZE - Canvas->TextWidth ("1")) / 2;
Canvas->TextOut (t2, t1, "1");
}
else
{
Canvas->Brush->Style = bsClear;
Canvas->Font->Name = "Courier";
if (Enabled) Canvas->Font->Color = FOffColor;
else Canvas->Font->Color = clGrayText;
Canvas->Font->Style = TFontStyles () << fsBold;
Canvas->Font->Size = 10;
t1 = mid - (Canvas->TextHeight ("0") / 2);
t2 = (BOX_SIZE - Canvas->TextWidth ("0")) / 2;
Canvas->TextOut (t2, t1, "0");
}
// Draw Caption
Canvas->Font->Assign (Font);
t1 = mid - (Canvas->TextHeight (FCaption) / 2);
if (!Enabled)
{
Canvas->Font->Color = clHighlightText;
Canvas->TextOut (BOX_SIZE + 6, t1 + 1, FCaption);
Canvas->Font->Color = clGrayText;
}
Canvas->TextOut (BOX_SIZE + 5, t1, FCaption);
// Draw Focus Rect
if (Focused ())
{
Canvas->Brush->Style = bsSolid;
Canvas->Pen->Color = clBlack;
Canvas->DrawFocusRect (Rect (BOX_SIZE + 3, t1 - 2,
Canvas->TextWidth (FCaption) + BOX_SIZE + 7,
(Canvas->TextHeight (FCaption) / 2) + mid + 2));
}
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::WMSetFocus(TWMSetFocus &Message)
{
FHasFocus = true;
Repaint ();
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::WMKillFocus(TWMKillFocus &Message)
{
FHasFocus = false;
Repaint ();
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::SetEnabled(bool Value)
{
TCustomControl::SetEnabled (Value);
Invalidate ();
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::SetOnColor (TColor col)
{
FOnColor = col;
Invalidate ();
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::SetOffColor (TColor col)
{
FOffColor = col;
Invalidate ();
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::SetName(const AnsiString Value)
{
TCustomControl::SetName(Value);
if (FCaption == "") FCaption = Value;
Invalidate ();
}
//---------------------------------------------------------------------------
void __fastcall TBitBox::SetCaption (AnsiString cap)
{
FCaption = cap;
Invalidate ();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -