📄 colorfield.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "ColorField.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TColorField *)
{
new TColorField(NULL);
}
//---------------------------------------------------------------------------
__fastcall TColorField::TColorField(TComponent* Owner)
: TGraphicControl(Owner)
{
FForeground = clBlack;
FBackground = clBlack;
FPalette[0] = clBlack;
FPalette[1] = clMaroon;
FPalette[2] = clGreen;
FPalette[3] = clOlive;
FPalette[4] = clNavy;
FPalette[5] = clPurple;
FPalette[6] = clTeal;
FPalette[7] = clGray;
FPalette[8] = clSilver;
FPalette[9] = clRed;
FPalette[10] = clLime;
FPalette[11] = clYellow;
FPalette[12] = clBlue;
FPalette[13] = clFuchsia;
FPalette[14] = clAqua;
FPalette[15] = clWhite;
}
//---------------------------------------------------------------------------
namespace Colorfield
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TColorField)};
RegisterComponents("Beispiele", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TColorField::Paint (void)
{
int r, c;
for (r = 0;r < 4;r++)
{
for (c = 0;c < 4;c++)
{
DrawCell(r * 4 + c);
}
}
c = ColorToCell (FForeground);
if (FForeground == FBackground)
{
if (FForeground != clNone) PutText ("FB", c);
return;
}
if (FForeground != clNone) PutText ("FG", c);
c = ColorToCell (FBackground);
if (FBackground != clNone) PutText ("BG", c);
}
//---------------------------------------------------------------------------
void __fastcall TColorField::DrawCell (int fieldnum)
{
TRect Cell;
if (fieldnum < 0 || fieldnum > 15) return;
Cell = Bounds((fieldnum % 4) * (Width / 4), (fieldnum / 4) * (Height / 4),
Width / 4, Height / 4);
Cell.Top++;
Cell.Left++;
Cell.Bottom--;
Cell.Right--;
Frame3D(Canvas, Cell, TColor(clBtnShadow), TColor(clBtnHighlight), 2);
Canvas->Brush->Color = FPalette[fieldnum];
Canvas->Pen->Color = FPalette[fieldnum];
Canvas->Rectangle(Cell.Left, Cell.Top, Cell.Right, Cell.Bottom);
//InflateRect(&RECT(Cell), -1, -1);
//DrawFocusRect((HDC)Canvas->Handle, &RECT(CellRect));
}
//---------------------------------------------------------------------------
TColor __fastcall TColorField::BuildTextColor (TColor back)
{
TColor res = (TColor) 0;
if ((back & 0x80) < 0x80) res = (TColor)0xff;
if ((back & 0x8000) < 0x8000) (int)res |= 0xff00;
if ((back & 0x800000) < 0x800000) (int)res |= 0xff0000;
return (res);
}
//---------------------------------------------------------------------------
int __fastcall TColorField::ColorToCell (TColor col)
{
int i;
for (i = 0;i < 16;i++)
{
if (FPalette[i] == col) return i;
}
return 0;
}
//---------------------------------------------------------------------------
void __fastcall TColorField::PutText (AnsiString text, int fieldnum)
{
int x, y;
Canvas->Font->Color = BuildTextColor(FPalette[fieldnum]);
Canvas->Brush->Color = FPalette[fieldnum];
x = (fieldnum % 4) * (Width / 4);
y = (fieldnum / 4) * (Height / 4);
y += ((Height / 4) - Canvas->TextHeight (text)) / 2;
x += ((Width / 4) - Canvas->TextWidth (text)) / 2;
Canvas->TextOut (x, y, text);
return;
}
//---------------------------------------------------------------------------
void __fastcall TColorField::SetForeground (TColor col)
{
FForeground = col;
Paint ();
}
//---------------------------------------------------------------------------
void __fastcall TColorField::SetBackground (TColor col)
{
FBackground = col;
Paint ();
}
//---------------------------------------------------------------------------
void __fastcall TColorField::MouseDown(TMouseButton but, TShiftState shift, int x, int y)
{
int col, row, i;
bool doPaint = false;
TGraphicControl::MouseDown(but, shift, x, y);
row = y / (Height / 4);
col = x / (Width / 4);
i = (row * 4) + col;
if (i < 0 || i > 15) i = 0;
if (but == mbLeft)
{
if (FForeground != FPalette[i])
{
FForeground = FPalette[i];
doPaint = true;
Change ();
}
}
if (but == mbRight)
{
if (FBackground != FPalette[i])
{
FBackground = FPalette[i];
doPaint = true;
Change ();
}
}
if (doPaint) Paint();
}
//---------------------------------------------------------------------------
TColor __fastcall TColorField::GetForeground (void)
{
return (FForeground);
}
//---------------------------------------------------------------------------
TColor __fastcall TColorField::GetBackground (void)
{
return (FBackground);
}
//---------------------------------------------------------------------------
void __fastcall TColorField::Change(void)
{
if (FOnChange) FOnChange (this);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -