-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
XCombobox.pas
433 lines (375 loc) · 12.4 KB
/
XCombobox.pas
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
{
XCheckbox, mimics the Windows 10's checkbox capable of
adding over an aero glass form on Delphi
Author: vhanla
Changelog:
TODO:
Add touch support
Add items support using maybe XPopupMenu
- 16-08-05
Change rendering text method for gdiplus to make it look better
https://theartofdev.com/2014/03/02/blurryfuzzy-gdi-text-rendering-using-antialias-and-floating-point-y-coordinates/
http://stackoverflow.com/questions/11307509/drawing-text-on-glass-background-becomes-blurred-as-alpha-is-lowered
- 15-10-14
Looks like Windows'
Twetaked to use it as a label when disabled, temporary solution :P
Fixed OnClick property in order to programmatically assign a function
to handle it, just removed the extra read an write events
}
unit XCombobox;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, PNGimage, functions, GDIPApi, GDIPobj;
type
TXCombobox = class(TGraphicControl)
private
_caption: string;
_disabledColor: TColor;
_enabledColor: TColor;
_pressedColor: TColor;
_chkstate: Boolean;
_mousehover: Boolean;
_mousepressed: Boolean;
_enabled: Boolean;
MDown: TMouseEvent;
MUp: TMouseEvent;
MLeave: TNotifyEvent;
BtnClick: TNotifyEvent;
FOnclick: TNotifyEvent;
_font: TFont;
procedure SetDisabledColor(Value: TColor);
procedure SetEnabledColor(Value: TColor);
procedure SetEnabled(Value: Boolean);
procedure SetPressedColor(Value: TColor);
procedure SetCaption(Value: string);
procedure SetCheckState(Value: Boolean);
procedure SetFont(Value: TFont);
protected
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure MouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure Click; override;
procedure SetParent(Value: TWinControl); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Caption: string read _caption write SetCaption;
property Color: TColor read _enabledColor write SetEnabledColor;
property DisabledColor: TColor read _disabledColor write SetDisabledColor;
property PressedColor: TColor read _pressedColor write SetPressedColor;
property Checked: Boolean read _chkstate write SetCheckState;
property OnMouseDown: TMouseEvent read MDown write MDown;
property OnMouseUp: TMouseEvent read MUp write MUp;
property OnMouseLeave: TNotifyEvent read MLeave write MLeave;
property ShowHint;
property ParentShowHint;
property OnMouseMove;
// property Font;
property Font: TFont read _font write SetFont;
property Enabled: Boolean read _enabled write SetEnabled;
property OnClick; //: TNotifyEvent read FOnClick write FOnClick;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('codigobit', [TXCombobox]);
end;
{ TXCombobox }
procedure TXCombobox.Click;
begin
inherited;
// ShowMessage(inttostr(Width));
// _chkstate := not _chkstate;
//Paint;
end;
constructor TXCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Width := MulDiv(167, Screen.PixelsPerInch, 96); //win10
Width := MulDiv(151, Screen.PixelsPerInch, 96);
Height := MulDiv(31, Screen.PixelsPerInch, 96);
_enabledcolor := clOlive;
_disabledColor := clBlack;
_enabled := True; // this will show as label if disabled
_pressedColor := $666666;
_mousehover := False;
_mousepressed := False;
Canvas.Brush.Color := clBlack;
_font := TFont.Create;
_font.Color := clWhite;
_font.Size := 14; //12 win10
FOnclick := nil;
ShowHint := False;
end;
destructor TXCombobox.Destroy;
begin
_font.Free;
inherited;
end;
procedure TXCombobox.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
_mousepressed := True;
Paint;
end;
procedure TXCombobox.MouseEnter(var Message: TMessage);
begin
_mousehover := True;
Paint;
end;
procedure TXCombobox.MouseLeave(var Message: TMessage);
begin
_mousehover := False;
Paint;
end;
procedure TXCombobox.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
_mousepressed := False;
Paint;
end;
procedure TXCombobox.Paint;
function MakeGDIPColor(C: TColor; A: Integer = 255): Cardinal;
var
tmpRGB: TColorRef;
begin
tmpRGB := ColorToRGB(C);
Result := ((DWORD(GetBValue(tmpRGB)) shl BlueShift) or
(DWORD(GetGValue(tmpRGB)) shl GreenShift) or
(DWORD(GetRValue(tmpRGB)) shl RedShift) or
(DWORD(A) shl AlphaShift));
end;
function hidpi(value: Integer): Integer;
begin
Result := MulDiv(value, Screen.PixelsPerInch, 96);
end;
var
bmp: TBitmap;
xfont: TGPFont;
style: Integer;
graph: TGPGraphics;
pen: TGPPen;
brush: TGPSolidBrush;
stringFormat: TGPStringFormat;
l, t, w, h, d, s, radio: Integer;
txt: WideString;
DPI: Integer;
ScaleFactor: Single;
path: TGPGraphicsPath;
begin
inherited;
DPI := Screen.PixelsPerInch;
ScaleFactor := 1; //DPI / 96; // Assuming 96 DPI as the baseline
radio := 42;
d := radio div 2;
s := d div 2; // guide for lines width
bmp := TBitmap.Create;
try
bmp.PixelFormat := pf32bit;
bmp.SetSize(Width, Height);
// Clear the canvas with appropriate color
if TaskbarAccented then
bmp.Canvas.Brush.Handle := CreateSolidBrushWithAlpha(BlendColors(clBlack, GetAccentColor, 50), 200)
else if SystemUsesLightTheme then
bmp.Canvas.Brush.Handle := CreateSolidBrushWithAlpha($DDDDDD, 200)
else
begin
if isWindows11 then
bmp.Canvas.Brush.Handle := CreateSolidBrushWithAlpha(BlendColors($2d2d2d, clBlack,25), 200)
else
bmp.Canvas.Brush.Handle := CreateSolidBrushWithAlpha($222222, 200);
end;
bmp.Canvas.FillRect(Rect(0, 0, Width, Height));
graph := TGPGraphics.Create(bmp.Canvas.Handle);
try
graph.SetSmoothingMode(SmoothingModeAntiAlias);
l := 0; t := 0; w := Width - 1; h := Height - 1;
style := FontStyleRegular;
xfont := TGPFont.Create(_font.Name, hidpi(_font.Size), style, UnitPixel);
try
brush := TGPSolidBrush.Create(MakeGDIPColor(_disabledColor));
try
if SystemUsesLightTheme then
pen := TGPPen.Create(MakeGDIPColor(clBlack, 100), 2 * ScaleFactor)
else
pen := TGPPen.Create(MakeGDIPColor(_disabledColor, 100), 2 * ScaleFactor);
try
if _enabled then
begin
if _mousepressed then
begin
if isWindows11 then
begin
end
else // win10
begin
brush.SetColor(MakeGDIPColor(_pressedColor));
graph.FillRectangle(brush, MakeRect(0, 0, Width - 1, Height - 1));
brush.SetColor(MakeGDIPColor(_disabledColor));
end;
end
else if _mousehover then
begin
if SystemUsesLightTheme then
pen.SetColor(MakeGDIPColor($333333))
else
pen.SetColor(MakeGDIPColor(_disabledColor));
end;
//draw button
d := HighDpi(8); //radio
if isWindows11 then
begin
path := TGPGraphicsPath.Create();
try
path.AddArc(l + 1, t + 1, d, d, 180, 90);
path.AddArc(l + w - d - 1, t + 1, d, d, 270, 90);
path.AddArc(l + w - d - 1, t + h - 1 - d, d, d, 0, 90);
path.AddArc(l + 1, t + h - d - 1, d, d, 90, 90);
path.CloseFigure;
if _mousepressed then
brush.SetColor(MakeGDIPColor($303030))
else
begin
if SystemUsesLightTheme then
brush.SetColor(MakeGDIPColor($cccccc))
else
brush.SetColor($FF353535);
end;
graph.FillPath(brush, path);
finally
path.Free;
end;
path := TGPGraphicsPath.Create();
try
path.AddArc(l + 2, t + 2, d, d, 180, 90);
path.AddArc(l + w - d - 2, t + 2, d, d, 270, 90);
path.AddArc(l + w - d - 2, t + h - 2 - d, d, d, 0, 90);
path.AddArc(l + 2, t + h - d - 2, d, d, 90, 90);
path.CloseFigure;
if _mousehover then
begin
if SystemUsesLightTheme then
brush.SetColor($FFF5F5F5)
else
brush.SetColor($FF323232)
end
else
if SystemUsesLightTheme then
brush.SetColor($effbfbfb)
else
brush.SetColor($FF2D2D2D);
if _mousepressed then
begin
if SystemUsesLightTheme then
brush.SetColor($FFf5f5f5)
else
brush.SetColor($FF272727);
end;
graph.FillPath(brush, path);
finally
path.Free;
end;
end
else //win10
begin
graph.DrawLine(pen, l, t, l + w, t);
graph.DrawLine(pen, l, t, l, t + h);
graph.DrawLine(pen, l, t + h, l + w, t + h);
graph.DrawLine(pen, l + w, t, l + w, t + h);
end;
// Draw knob
pen.SetWidth(1);
if SystemUsesLightTheme then
pen.SetColor($FF333333)
else
pen.SetColor(MakeGDIPColor(_disabledColor, 100));
graph.DrawLine(pen, l + w - hidpi(22), t + hidpi(13), l + w - hidpi(22) + 6, t + hidpi(13) + 6);
graph.DrawLine(pen, l + w - hidpi(22) + 6 + 1, t + hidpi(13) + 6, l + w - hidpi(22) + 6 + 1 + 6, t + hidpi(13));
pen.SetWidth(2);
end;
// Draw the caption
stringFormat := TGPStringFormat.Create;
try
stringFormat.SetAlignment(StringAlignmentNear);
stringFormat.SetLineAlignment(StringAlignmentCenter);
stringFormat.SetTrimming(StringTrimmingEllipsisCharacter);
stringFormat.SetFormatFlags(StringFormatFlagsNoWrap);
brush.SetColor(MakeGDIPColor($BBBBBB));
graph.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
txt := _caption;
graph.DrawString(txt, Length(txt), xfont, MakePoint(15.0 * ScaleFactor, (Height / 2 - hidpi(_font.Size)) * ScaleFactor + hidpi(_font.size div 3)), nil, brush);
graph.SetTextRenderingHint(TextRenderingHintSingleBitPerPixelGridFit);
if SystemUsesLightTheme then
brush.SetColor(MakeGDIPColor($333333))
else
brush.SetColor(MakeGDIPColor($FFFFFF));
if SystemUsesLightTheme then
graph.DrawString(txt, Length(txt), xfont, MakePoint(15.0 * ScaleFactor, (Height / 2 - _font.Size) * ScaleFactor), nil, brush);
finally
stringFormat.Free;
end;
finally
pen.Free;
end;
finally
brush.Free;
end;
finally
xfont.Free;
end;
finally
graph.Free;
end;
Canvas.Draw(0, 0, bmp);
finally
bmp.Free;
end;
end;
procedure TXCombobox.SetCaption(Value: string);
begin
_caption := Value;
Paint;
end;
procedure TXCombobox.SetCheckState(Value: Boolean);
begin
_chkstate := Value;
Paint;
end;
procedure TXCombobox.SetDisabledColor(Value: TColor);
begin
_disabledColor := Value;
Paint;
end;
procedure TXCombobox.SetEnabled(Value: Boolean);
begin
_enabled := Value;
Paint;
end;
procedure TXCombobox.SetEnabledColor(Value: TColor);
begin
_enabledColor := Value;
Paint;
end;
procedure TXCombobox.SetFont(Value: TFont);
begin
_font.Assign(Value);
Invalidate;
end;
procedure TXCombobox.SetParent(Value: TWinControl);
begin
inherited;
if Value <> nil then _caption := Name;
end;
procedure TXCombobox.SetPressedColor(Value: TColor);
begin
_pressedColor := Value;
Paint;
end;
end.