1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "cpn_button.h"
#pragma package(smart_init)
using Gr32_image::TCustomPaintBox32;
using Graphics::TColor;
using System::UnicodeString;
using Types::TSize;
//---------------------------------------------------------------------------
#define INNER_HSPACE 12 // Should be even
#define INNER_VSPACE 6
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(BButton*)
{
new BButton(NULL);
}
//---------------------------------------------------------------------------
namespace Cpn_button
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = { __classid(BButton) };
RegisterComponents(L"MYCPN", classes, 0);
// With multiple components in one file
// TComponentClass classes[3];
// classes[0] = __classid(BButton);
// classes[1] = __classid(BFixedButton);
// RegisterComponents(L"MYCPN", classes, 1);
}
}
//---------------------------------------------------------------------------
__fastcall BButton::BButton(TComponent* owner) : TCustomPaintBox32(owner), attr_caption(L""), attr_state(STATE_NORMAL)
{
// OutputDebugStringW(L"BButton::BButton");
Ctl3D = false;
DoubleBuffered = true
TabStop = true;
Font->Name = L"Segoe UI";
Font->Color = clBlack;
Font->Height = -13;
// Font->Size = 10;
OnClick = NULL;
OnEnter = &button_enter;
OnExit = &button_exit;
OnKeyPress = &button_keypress;
OnMouseEnter = &action_mouse_enter;
OnMouseLeave = &action_mouse_leave;
OnMouseUp = NULL;
Width = INNER_HSPACE;
Height = 30;
}
//---------------------------------------------------------------------------
void __fastcall BButton::action_mouse_enter(TObject*)
{
if (state == STATE_NORMAL) {
state = STATE_MOUSE_OVER;
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall BButton::action_mouse_leave(TObject*)
{
if (state == STATE_MOUSE_OVER) {
state = STATE_NORMAL;
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall BButton::button_enter(TObject*)
{
if (state == STATE_NORMAL) {
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall BButton::button_exit(TObject*)
{
if (state == STATE_NORMAL) {
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall BButton::button_keypress(TObject*, WideChar& key)
{
if (key == VK_RETURN) {
if (OnClick != NULL) {
OnClick(NULL);
} else if (OnMouseUp != NULL) {
OnMouseUp(NULL, mbLeft, TShiftState(), 0, 0);
}
}
}
//---------------------------------------------------------------------------
void __fastcall BButton::DoPaintBuffer()
{
// OutputDebugStringW(L"BButton::DoPaintBuffer");
draw_button();
Buffer->TextoutW(INNER_HSPACE, INNER_VSPACE, attr_caption);
TCustomPaintBox32::DoPaintBuffer();
}
//---------------------------------------------------------------------------
void BButton::draw_button()
{
// OutputDebugStringW(L"BButton::draw_button");
int color;
unsigned char red, green, blue;
unsigned short line, nb_lines = ((Height - 2) - 1);
Buffer->Font = Font;
Canvas->Font = Font;
Buffer->Canvas->Font = Font;
Buffer->BeginUpdate();
if (state == STATE_NORMAL) {
if ( Focused() ) {
// Buffer->Line(0, 0, (Width - 1), 0, 0x003399FF);
// Buffer->Line((Width - 1), 0, (Width - 1), (Height - 1), 0x003399FF);
// Buffer->Line((Width - 1), (Height - 1), 0, (Height - 1), 0x003399FF);
// Buffer->Line(0, (Height - 1), 0, 0, 0x003399FF);
Buffer->FrameRectS(0, 0, Width, Height, 0x003399FF);
} else {
// Buffer->Line(0, 0, (Width - 1), 0, 0x00ACACAC);
// Buffer->Line((Width - 1), 0, (Width - 1), (Height - 1), 0x00ACACAC);
// Buffer->Line((Width - 1), (Height - 1), 0, (Height - 1), 0x00ACACAC);
// Buffer->Line(0, (Height - 1), 0, 0, 0x00ACACAC);
Buffer->FrameRectS(0, 0, Width, Height, 0x00ACACAC);
}
Buffer->Line(1, 1, (Width - 1), 1, 0x00F0F0F0);
for(line = 1; line <= nb_lines; ++line) {
red = (unsigned char) (0xF0 + line * ((float) ((float) (0xE5 - 0xF0)) / ((float) nb_lines)));
green = red;
blue = red;
color = (((red & 0xFF) << 16) + ((green & 0xFF) << 8) + (blue & 0xFF));
Buffer->Line(1, (line + 1), (Width - 1), (line + 1), color);
}
Buffer->Line(1, (Height - 2), (Width - 1), (Height - 2), 0x00E5E5E5);
Buffer->Font->Color = clBlack;
} else if (state == STATE_MOUSE_OVER) {
// Buffer->Line(0, 0, (Width - 1), 0, 0x007EB4EA);
// Buffer->Line((Width - 1), 0, (Width - 1), (Height - 1), 0x007EB4EA);
// Buffer->Line((Width - 1), (Height - 1), 0, (Height - 1), 0x007EB4EA);
// Buffer->Line(0, (Height - 1), 0, 0, 0x007EB4EA);
Buffer->FrameRectS(0, 0, Width, Height, 0x007EB4EA);
Buffer->Line(1, 1, (Width - 1), 1, 0x00ECF4FC);
for(line = 1; line <= nb_lines; ++line) {
red = (unsigned char) (0xEC + line * ((float) ((float) (0xDC - 0xEC)) / ((float) nb_lines)));
green = (unsigned char) (0xF4 + line * ((float) ((float) (0xEC - 0xF4)) / ((float) nb_lines)));
blue = 0xFC;
color = (((red & 0xFF) << 16) + ((green & 0xFF) << 8) + (blue & 0xFF));
Buffer->Line(1, (line + 1), (Width - 1), (line + 1), color);
}
Buffer->Line(1, (Height - 2), (Width - 1), (Height - 2), 0x00DCECFC);
Buffer->Font->Color = clBlack;
} else if (state == STATE_DISABLED) {
Buffer->FillRect(1, 1, (Width - 1), (Height - 1), 0x00EFEFEF);
// Buffer->Line(0, 0, (Width - 1), 0, 0x00D9D9D9);
// Buffer->Line((Width - 1), 0, (Width - 1), (Height - 1), 0x00D9D9D9);
// Buffer->Line((Width - 1), (Height - 1), 0, (Height - 1), 0x00D9D9D9);
// Buffer->Line(0, (Height - 1), 0, 0, 0x00D9D9D9);
Buffer->FrameRectS(0, 0, Width, Height, 0x00D9D9D9);
Buffer->Font->Color = (TColor) 0x00A0A0A0;
}
Buffer->EndUpdate();
}
//--------------------------------------------------------------------------
void BButton::reinit_events() {
OnEnter = &button_enter;
OnExit = &button_exit;
OnKeyPress = &button_keypress;
OnMouseEnter = &action_mouse_enter;
OnMouseLeave = &action_mouse_leave;
}
//--------------------------------------------------------------------------
void __fastcall BButton::set_caption(const UnicodeString new_caption)
{
// OutputDebugStringW(L"BButton::set_caption");
attr_caption = new_caption;
Buffer->Font = Font;
Canvas->Font = Font;
Buffer->Canvas->Font = Font;
TSize size;
if ((attr_caption.Length() != 0) && (!attr_caption.IsEmpty())) {
size = Buffer->TextExtent(attr_caption);
} else {
size = Buffer->TextExtent(L"AZwpq02!!");
size.cx = 0;
}
if ((size.cx % 2) != 0) { ++size.cx; }
if ((size.cy % 2) != 0) { ++size.cy; }
Width = (size.cx + (2 * INNER_HSPACE));
Height = (size.cy + (2 * INNER_VSPACE));
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall BButton::set_state(const BButton::STATE new_state)
{
// OutputDebugStringW(L"BButton::set_state 01");
if (attr_state == new_state) { return; }
// OutputDebugStringW(L"BButton::set_state 02");
attr_state = new_state;
Invalidate();
}
//--------------------------------------------------------------------------- |
Partager