/
JvCaptionButton.pas
2831 lines (2490 loc) · 80.2 KB
/
JvCaptionButton.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvCaptionButton.PAS, released on 2002-05-26.
The Initial Developer of the Original Code is Peter Thörnqvist [peter3 at sourceforge dot net]
Portions created by Peter Thörnqvist are Copyright (C) 2002 Peter Thörnqvist.
This unit is a merging of the original TJvCaptionButton, TJvaCaptionButton.
Merging done 2003-06-12 by Remko Bonte [remkobonte at myrealbox dot com]
All Rights Reserved.
Contributor(s):
Andrei Prygounkov <a dot prygounkov at gmx dot de>, author of TJvaCaptionButton.
Remko Bonte [remkobonte at myrealbox dot com], theme support, actions
Olivier Sannier [obones att altern dott org], caption hints.
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.delphi-jedi.org
Modified 2003-06-13 (p3):
- Fixed MouseUp X,Y inconsistentcy (did not report the same values as MouseDown)
- Added MouseMove handler
- Added ShowHint, ParentShowHint
- Fixed drawing of disabled MinimizeToTray icon as well as incorrect Font.Color in text drawing
- Added Assign
- Tested on W2k
- Demo (examples\CaptionBtn) updated and extended
Known Issues:
* Msimg32.dll code should be moved to seperate import unit. Code is partly
copied from JwaWinGDI.pas.
* Button can disappear at design-time when switching themes.
* With more buttons, button can appear hot while mouse is over another caption
button.
* Still some flicker while resizing due to wrong FButtonRect, see comment
at HandleNCPaintBefore.
* Buttons on small caption (BorderStyle in [bsSizeToolWin, bsToolWin]) looks
ugly.
-----------------------------------------------------------------------------}
// $Id$
unit JvCaptionButton;
{$I jvcl.inc}
{$I windowsonly.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
Windows, Messages, Classes, Graphics, Controls, Forms, Types,
ActnList, ImgList,
{$IFDEF HAS_UNIT_SYSTEM_UITYPES}
System.UITypes,
{$ENDIF HAS_UNIT_SYSTEM_UITYPES}
JvComponentBase, JvTypes;
type
TJvStandardButton = (tsbNone, tsbClose, tsbHelp, tsbMax, tsbMin, tsbRestore,
tsbMinimizeToTray); // a la e-Mule
TJvCaptionButtonLayout = (cbImageLeft, cbImageRight);
TJvRedrawKind = (rkDirect, rkIndirect, rkTotalCaptionBar);
TJvCaptionButton = class;
TJvCaptionButtonActionLink = class(TActionLink)
protected
FClient: TJvCaptionButton;
procedure AssignClient(AClient: TObject); override;
function IsCaptionLinked: Boolean; override;
function IsEnabledLinked: Boolean; override;
function IsHintLinked: Boolean; override;
function IsImageIndexLinked: Boolean; override;
function IsVisibleLinked: Boolean; override;
function IsOnExecuteLinked: Boolean; override;
procedure SetCaption(const Value: string); override;
procedure SetEnabled(Value: Boolean); override;
procedure SetHint(const Value: string); override;
procedure SetImageIndex(Value: Integer); override;
procedure SetVisible(Value: Boolean); override;
procedure SetOnExecute(Value: TNotifyEvent); override;
end;
TJvCaptionButtonActionLinkClass = class of TJvCaptionButtonActionLink;
{$IFDEF RTL230_UP}
[ComponentPlatformsAttribute(pidWin32 or pidWin64)]
{$ENDIF RTL230_UP}
TJvCaptionButton = class(TJvComponent)
private
{ Properties }
FAlignment: TAlignment;
FHeight: Integer;
FLeft: Integer;
FTop: Integer;
FWidth: Integer;
FCaption: string;
FDown: Boolean;
FEnabled: Boolean;
FFont: TFont;
FHint: string;
FImageIndex: TImageIndex;
FImages: TCustomImageList;
FLayout: TJvCaptionButtonLayout;
FMargin: Integer;
FPosition: Integer;
FSpacing: Integer;
FStandard: TJvStandardButton;
FToggle: Boolean;
FVisible: Boolean;
FOnClick: TNotifyEvent;
FOnMouseUp: TMouseEvent;
FOnMouseDown: TMouseEvent;
FOnMouseMove: TMouseMoveEvent;
FDefaultButtonLeft: Integer;
FDefaultButtonTop: Integer;
FDefaultButtonWidth: Integer;
FDefaultButtonHeight: Integer;
FActionLink: TJvCaptionButtonActionLink;
FBuffer: TBitmap;
FButtonRect: TRect;
FCaptionHeight: Integer;
FClickRect: TRect; // Clickable area is a bit bigger than the button
FHasCaption: Boolean; // True, if the form has a caption
FHasSmallCaption: Boolean; // True, if the form has BorderStyle bsToolWindow, bsSizeToolWin
FImageChangeLink: TChangeLink;
FMouseButtonDown: Boolean;
FMouseInControl: Boolean;
FNeedRecalculate: Boolean;
FRgnChanged: Boolean;
FSaveRgn: HRGN;
FShowHint: Boolean;
FParentShowHint: Boolean;
{tool tip specific}
FToolTipHandle: THandle;
{tool tip specific end}
FCurrentWindowState: TWindowState;
{$IFDEF JVCLThemesEnabled}
FCaptionActive: Boolean;
FForceDrawSimple: Boolean;
FForceRedraw: Boolean;
function GetIsThemed: Boolean;
procedure SetForceDrawSimple(const Value: Boolean);
{$ENDIF JVCLThemesEnabled}
function GetAction: TBasicAction;
function GetIsImageVisible: Boolean;
function GetParentForm: TCustomForm;
function GetParentFormHandle: THandle;
function IsCaptionStored: Boolean;
function IsEnabledStored: Boolean;
function IsHintStored: Boolean;
function IsImageIndexStored: Boolean;
procedure SetAction(const Value: TBasicAction);
procedure SetAlignment(Value: TAlignment);
procedure SetCaption(Value: string);
procedure SetDown(const Value: Boolean);
procedure SetEnabled(const Value: Boolean);
procedure SetFont(Value: TFont);
procedure SetHeight(Value: Integer);
procedure SetImageIndex(const Value: TImageIndex);
procedure SetImages(const Value: TCustomImageList);
procedure SetLayout(const Value: TJvCaptionButtonLayout);
procedure SetLeft(Value: Integer);
procedure SetMargin(const Value: Integer);
procedure SetMouseInControl(const Value: Boolean);
procedure SetPosition(const Value: Integer);
procedure SetSpacing(const Value: Integer);
procedure SetStandard(Value: TJvStandardButton);
procedure SetToggle(const Value: Boolean);
procedure SetTop(Value: Integer);
procedure SetVisible(const Value: Boolean);
procedure SetWidth(Value: Integer);
{tool tip handling}
procedure CreateToolTip(Wnd: THandle);
procedure DestroyToolTip;
procedure HideToolTip;
procedure ForwardToToolTip(Msg: TMessage);
procedure Hook;
procedure UnHook;
function WndProcAfter(var Msg: TMessage): Boolean;
function WndProcBefore(var Msg: TMessage): Boolean;
procedure DrawButton(DC: HDC);
{$IFDEF JVCLThemesEnabled}
procedure DrawButtonBackground(ACanvas: TCanvas);
{$ENDIF JVCLThemesEnabled}
procedure DrawStandardButton(ACanvas: TCanvas);
procedure DrawNonStandardButton(ACanvas: TCanvas);
procedure DrawButtonImage(ACanvas: TCanvas; ImageBounds: TRect);
procedure DrawButtonText(ACanvas: TCanvas; TextBounds: TRect);
procedure Redraw(const AKind: TJvRedrawKind);
procedure CalcDefaultButtonRect(Wnd: THandle);
{Paint related messages}
procedure HandleNCActivate(var Msg: TWMNCActivate);
procedure HandleNCPaintAfter(Wnd: THandle; var Msg: TWMNCPaint);
procedure HandleNCPaintBefore(Wnd: THandle; var Msg: TWMNCPaint);
{Mouse down-related messages}
function HandleButtonDown(var Msg: TWMNCHitMessage): Boolean;
function HandleButtonUp(var Msg: TWMNCHitMessage): Boolean;
function HandleHitTest(var Msg: TWMNCHitTest): Boolean;
function HandleMouseMove(var Msg: TWMNCHitMessage): Boolean;
procedure HandleNCMouseMove(var Msg: TWMNCHitMessage);
{Other}
function HandleNotify(var Msg: TWMNotify): Boolean;
procedure ImageListChange(Sender: TObject);
procedure DoActionChange(Sender: TObject);
function MouseOnButton(X, Y: Integer; const TranslateToScreenCoord: Boolean): Boolean;
procedure SetParentShowHint(const Value: Boolean);
procedure SetShowHint(const Value: Boolean);
protected
procedure ActionChange(Sender: TObject; CheckDefaults: Boolean); dynamic;
procedure CalcButtonParts(ACanvas: TCanvas; ButtonRect: TRect; var RectText, RectImage: TRect);
function GetActionLinkClass: TJvCaptionButtonActionLinkClass; dynamic;
procedure Loaded; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); virtual;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); virtual;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); virtual;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure UpdateButtonRect(Wnd: THandle);
property ActionLink: TJvCaptionButtonActionLink read FActionLink write FActionLink;
property IsImageVisible: Boolean read GetIsImageVisible;
{$IFDEF JVCLThemesEnabled}
// The value of IsThemed stays the same until a WM_THEMECHANGED is received.
property IsThemed: Boolean read GetIsThemed;
{$ENDIF JVCLThemesEnabled}
property MouseInControl: Boolean read FMouseInControl;
property ParentFormHandle: THandle read GetParentFormHandle;
property ParentForm: TCustomForm read GetParentForm;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure InitiateAction; virtual;
procedure ResetButton;
procedure Click; dynamic;
property DefaultButtonWidth: Integer read FDefaultButtonWidth;
published
property Action: TBasicAction read GetAction write SetAction;
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
property ButtonHeight: Integer read FHeight write SetHeight default 0;
property ButtonLeft: Integer read FLeft write SetLeft default 0;
property ButtonTop: Integer read FTop write SetTop default 0;
property ButtonWidth: Integer read FWidth write SetWidth default 0;
property Caption: string read FCaption write SetCaption stored IsCaptionStored;
property Down: Boolean read FDown write SetDown default False;
{$IFDEF JVCLThemesEnabled}
property ForceDrawSimple: Boolean read FForceDrawSimple write SetForceDrawSimple default False;
{$ENDIF JVCLThemesEnabled}
property ShowHint: Boolean read FShowHint write SetShowHint default False;
property ParentShowHint: Boolean read FParentShowHint write SetParentShowHint default True;
property Enabled: Boolean read FEnabled write SetEnabled stored IsEnabledStored default True;
property Font: TFont read FFont write SetFont;
property Hint: string read FHint write FHint stored IsHintStored;
property ImageIndex: TImageIndex read FImageIndex write SetImageIndex stored IsImageIndexStored default -1;
property Images: TCustomImageList read FImages write SetImages;
property Layout: TJvCaptionButtonLayout read FLayout write SetLayout default cbImageLeft;
property Margin: Integer read FMargin write SetMargin default -1;
property Position: Integer read FPosition write SetPosition default 0;
property Spacing: Integer read FSpacing write SetSpacing default 4;
property Standard: TJvStandardButton read FStandard write SetStandard default tsbNone;
property Toggle: Boolean read FToggle write SetToggle default False;
property Visible: Boolean read FVisible write SetVisible default True;
property OnClick: TNotifyEvent read FOnClick write FOnClick;
property OnMouseUp: TMouseEvent read FOnMouseUp write FOnMouseUp;
property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown;
property OnMouseMove: TMouseMoveEvent read FOnMouseMove write FOnMouseMove;
end;
function TransparentBlt(hdcDest: HDC; nXOriginDest, nYOriginDest, nWidthDest, hHeightDest: Integer;
hdcSrc: HDC; nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc: Integer;
crTransparent: UINT): BOOL; stdcall;
function AlphaBlend(hdcDest: HDC; nXOriginDest, nYOriginDest, nWidthDest,
nHeightDest: Integer; hdcSrc: HDC; nXOriginSrc, nYOriginSrc, nWidthSrc,
nHeightSrc: Integer; BlendFunction: BLENDFUNCTION): BOOL; stdcall;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$URL$';
Revision: '$Revision$';
Date: '$Date$';
LogPath: 'JVCL\run'
);
{$ENDIF UNITVERSIONING}
implementation
uses
CommCtrl, Buttons, SysUtils,
JclSysInfo,
JvThemes,
{$IFDEF JVCLThemesEnabled}
UxTheme,
{$IFNDEF COMPILER7_UP}
TmSchema,
{$ENDIF !COMPILER7_UP}
{$ENDIF JVCLThemesEnabled}
JvDsgnIntf, JvJCLUtils, JvResources, JvWndProcHook, JvJVCLUtils;
const
{ Msimg32.dll is included in Windows 98 and later }
Msimg32DLLName = 'Msimg32.dll';
TransparentBltName = 'TransparentBlt';
AlphaBlendName = 'AlphaBlend';
htCaptionButton = HTSIZELAST + 1;
Alignments: array [TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
var
GMsimg32Handle: THandle = 0;
GTriedLoadMsimg32Dll: Boolean = False;
_AlphaBlend: function(hdcDest: HDC; nXOriginDest, nYOriginDest, nWidthDest,
nHeightDest: Integer; hdcSrc: HDC; nXOriginSrc, nYOriginSrc, nWidthSrc,
nHeightSrc: Integer; BlendFunction: BLENDFUNCTION): BOOL; stdcall;
_TransparentBlt: function(hdcDest: HDC; nXOriginDest, nYOriginDest, nWidthDest, hHeightDest: Integer;
hdcSrc: HDC; nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc: Integer;
crTransparent: UINT): BOOL; stdcall;
{$IFDEF JVCLThemesEnabled}
type
{ (rb) I couldn't get the alpha channel to work with the normal TBitmap so
introduced TAlphaBitmap. TBitmapAdapter hides the implementation details
of the TBitmap/TAlphaBitmap }
TAlphaBitmap = class(TObject)
private
FHandle: HDC;
FBitmapInfo: TBitmapInfo;
FDIBHandle: HBitmap;
FOldBitmap: HBitmap;
FBitsMem: Pointer;
FBitCount: Byte;
FHasAlphaChannel: Boolean;
function GetWidth: Integer;
function GetHeight: Integer;
protected
procedure CreateHandle(AWidth, AHeight: Integer);
function CreateDIB(ADC: HDC; AWidth, AHeight: Integer): HBitmap;
procedure Duplicate(Src: HBitmap);
procedure FreeHandle;
procedure InitAlpha;
public
destructor Destroy; override;
procedure Assign(Source: TPersistent);
procedure LoadFromResourceID(Instance: THandle; ResID: Integer);
procedure LoadFromResourceName(Instance: THandle; const ResName: string);
property Handle: HDC read FHandle;
property Width: Integer read GetWidth;
property Height: Integer read GetHeight;
property Data: Pointer read FBitsMem;
property BitCount: Byte read FBitCount;
property HasAlphaChannel: Boolean read FHasAlphaChannel;
end;
TBitmapAdapter = class(TObject)
private
FBitmap: TObject;
FMargins: TMargins;
FTransparentColor: TColorRef;
function GetHeight: Integer;
function GetWidth: Integer;
function GetIsValid: Boolean;
public
constructor Create; virtual;
destructor Destroy; override;
procedure Clear;
procedure Assign(Source: TPersistent);
procedure LoadFromResourceName(Instance: THandle; const ResName: string);
procedure LoadFromResourceID(Instance: THandle; ResID: Integer);
function Draw(ACanvas: TCanvas; const Rect: TRect; AMargins: PMargins): Boolean;
function DrawFixed(ACanvas: TCanvas; const X, Y: Integer): Boolean;
function DrawFixedPart(ACanvas: TCanvas; const DestRect: TRect; const SrcX, SrcY: Integer): Boolean;
function DrawPart(ACanvas: TCanvas; const SrcRect, DestRect: TRect; AMargins: PMargins): Boolean;
property Margins: TMargins read FMargins write FMargins;
property Width: Integer read GetWidth;
property Height: Integer read GetHeight;
property IsValid: Boolean read GetIsValid;
property TransparentColor: TColorRef read FTransparentColor write FTransparentColor;
end;
TGlobalXPData = class(TObject)
private
FCaptionButtonHeight: Integer;
FCaptionButtonCount: Integer;
FCaptionButtons: TBitmapAdapter;
FIsThemed: Boolean;
FBitmapValid: Boolean;
FClientCount: Integer;
public
constructor Create; virtual;
destructor Destroy; override;
procedure AddClient;
procedure RemoveClient;
procedure Update;
procedure DrawSimple(ACanvas: TCanvas; State: Integer; const DrawRect: TRect);
function Draw(ACanvas: TCanvas; State: Integer; const DrawRect: TRect): Boolean;
property IsThemed: Boolean read FIsThemed;
end;
var
GGlobalXPData: TGlobalXPData;
//=== Local procedures =======================================================
function IsVistaOrNewer: Boolean;
begin
Result := (Win32Platform = VER_PLATFORM_WIN32_NT) and JclCheckWinVersion(6, 0);
end;
function GlobalXPData: TGlobalXPData;
begin
if not Assigned(GGlobalXPData) then
GGlobalXPData := TGlobalXPData.Create;
Result := GGlobalXPData;
end;
function TranslateBitmapFileName(const S: string): string;
var
I: Integer;
begin
Result := S;
for I := 1 to Length(S) do
case S[I] of
'A'..'Z', '0'..'9':
{do nothing};
'a'..'z':
Result[I] := UpCase(S[I]);
else
Result[I] := '_';
end;
end;
procedure DupBits(Src, Dst: HBitmap; Size: TPoint);
var
MemDC: HDC;
DesktopDC: HDC;
OldBitmap: HBitmap;
begin
OldBitmap := 0;
DesktopDC := GetDC(GetDesktopWindow);
MemDC := CreateCompatibleDC(DesktopDC);
try
OldBitmap := SelectObject(MemDC, Src);
BitBlt(Dst, 0, 0, Size.X, Size.Y, MemDC, 0, 0, SRCCOPY);
finally
SelectObject(MemDC, OldBitmap);
ReleaseDC(GetDesktopWindow, DesktopDC);
DeleteDC(MemDC);
end;
end;
function GetHasAlphaChannel(Data: PRGBQuad; Count: Integer): Boolean;
begin
Result := False;
while Count > 0 do
begin
Result := Data.rgbReserved <> 0;
if Result then
Break;
Inc(Data);
Dec(Count);
end;
end;
procedure PreMultiplyAlphaChannel(Data: PRGBQuad; Count: Integer);
begin
while Count > 0 do
begin
with Data^ do
begin
rgbBlue := (rgbBlue * rgbReserved + 128) div 255;
rgbGreen := (rgbGreen * rgbReserved + 128) div 255;
rgbRed := (rgbRed * rgbReserved + 128) div 255;
end;
Inc(Data);
Dec(Count);
end;
end;
{$ENDIF JVCLThemesEnabled}
procedure UnloadMsimg32Dll;
begin
@_TransparentBlt := nil;
@_AlphaBlend := nil;
if GMsimg32Handle > 0 then
FreeLibrary(GMsimg32Handle);
GMsimg32Handle := 0;
end;
procedure LoadMsimg32Dll;
begin
GTriedLoadMsimg32Dll := True;
GMsimg32Handle := SafeLoadLibrary(Msimg32DLLName);
if GMsimg32Handle <> 0 then
begin
@_TransparentBlt := GetProcAddress(GMsimg32Handle, TransparentBltName);
@_AlphaBlend := GetProcAddress(GMsimg32Handle, AlphaBlendName);
end;
end;
{$IFDEF JVCLThemesEnabled}
function TransparentBltStretch(DestDC: HDC; const DestRect: TRect;
SourceDC: HDC; const SourceRect: TRect; const SizingMargins: TMargins;
const TransparentColor: TColor): Boolean;
var
ESourceWidth, ESourceHeight: Integer;
EDestWidth, EDestHeight: Integer;
LastOriginSource: TPoint;
LastOriginDest: TPoint;
begin
{ Source Dest
|--------------| |--------------------|
| A | B | C | | A | B | C |
|-- |------|---| |-- |------------|---|
| | | | | | | |
| D | E | F | | | | |
| | | | => | D | E | F |
|---|------|---| | | | |
| G | H | I | | | | |
|--------------| |--------------------|
| G | H | I |
|-- |------------|---|
}
ESourceWidth := SourceRect.Right - SourceRect.Left - SizingMargins.cxLeftWidth - SizingMargins.cxRightWidth;
ESourceHeight := SourceRect.Bottom - SourceRect.Top - SizingMargins.cyTopHeight - SizingMargins.cyBottomHeight;
EDestWidth := DestRect.Right - DestRect.Left - SizingMargins.cxLeftWidth - SizingMargins.cxRightWidth;
EDestHeight := DestRect.Bottom - DestRect.Top - SizingMargins.cyTopHeight - SizingMargins.cyBottomHeight;
GetWindowOrgEx(SourceDC, LastOriginSource);
SetWindowOrgEx(SourceDC, LastOriginSource.X - SourceRect.Left, LastOriginSource.Y - SourceRect.Top, nil);
GetWindowOrgEx(DestDC, LastOriginDest);
SetWindowOrgEx(DestDC, LastOriginDest.X - DestRect.Left, LastOriginDest.Y - DestRect.Top, nil);
Result :=
{ A }
TransparentBlt(
DestDC,
0, 0, SizingMargins.cxLeftWidth, SizingMargins.cyTopHeight,
SourceDC,
0, 0, SizingMargins.cxLeftWidth, SizingMargins.cyTopHeight,
TransparentColor
) and
{ B }
TransparentBlt(
DestDC,
SizingMargins.cxLeftWidth, 0, EDestWidth, SizingMargins.cyTopHeight,
SourceDC,
SizingMargins.cxLeftWidth, 0, ESourceWidth, SizingMargins.cyTopHeight,
TransparentColor
) and
{ C }
TransparentBlt(
DestDC,
EDestWidth + SizingMargins.cxLeftWidth, 0, SizingMargins.cxRightWidth, SizingMargins.cyTopHeight,
SourceDC,
ESourceWidth + SizingMargins.cxLeftWidth, 0, SizingMargins.cxRightWidth, SizingMargins.cyTopHeight,
TransparentColor
) and
{ D }
TransparentBlt(
DestDC,
0, SizingMargins.cyTopHeight, SizingMargins.cxLeftWidth, EDestHeight,
SourceDC,
0, SizingMargins.cyTopHeight, SizingMargins.cxLeftWidth, ESourceHeight,
TransparentColor
) and
{ E }
TransparentBlt(
DestDC,
SizingMargins.cxLeftWidth, SizingMargins.cyTopHeight, EDestWidth, EDestHeight,
SourceDC,
SizingMargins.cxLeftWidth, SizingMargins.cyTopHeight, ESourceWidth, ESourceHeight,
TransparentColor
) and
{ F }
TransparentBlt(
DestDC,
EDestWidth + SizingMargins.cxLeftWidth, SizingMargins.cyTopHeight, SizingMargins.cxRightWidth, EDestHeight,
SourceDC,
ESourceWidth + SizingMargins.cxLeftWidth, SizingMargins.cyTopHeight, SizingMargins.cxRightWidth, ESourceHeight,
TransparentColor
) and
{ G }
TransparentBlt(
DestDC,
0, EDestHeight + SizingMargins.cyTopHeight, SizingMargins.cxLeftWidth, SizingMargins.cyBottomHeight,
SourceDC,
0, ESourceHeight + SizingMargins.cyTopHeight, SizingMargins.cxLeftWidth, SizingMargins.cyBottomHeight,
TransparentColor
) and
{ H }
TransparentBlt(
DestDC,
SizingMargins.cxLeftWidth, EDestHeight + SizingMargins.cyTopHeight, EDestWidth, SizingMargins.cyBottomHeight,
SourceDC,
SizingMargins.cxLeftWidth, ESourceHeight + SizingMargins.cyTopHeight, ESourceWidth, SizingMargins.cyBottomHeight,
TransparentColor
) and
{ I }
TransparentBlt(
DestDC,
EDestWidth + SizingMargins.cxLeftWidth, EDestHeight + SizingMargins.cyTopHeight,
SizingMargins.cxRightWidth, SizingMargins.cyBottomHeight,
SourceDC,
ESourceWidth + SizingMargins.cxLeftWidth, ESourceHeight + SizingMargins.cyTopHeight,
SizingMargins.cxRightWidth, SizingMargins.cyBottomHeight,
TransparentColor
);
SetWindowOrgEx(SourceDC, LastOriginSource.X, LastOriginSource.Y, nil);
SetWindowOrgEx(DestDC, LastOriginDest.X, LastOriginDest.Y, nil);
end;
function GetXPCaptionButtonBitmap(ABitmap: TBitmapAdapter; out BitmapCount: Integer): Boolean;
var
Handle: THandle;
ThemeFileNameW, BitmapFileNameW: array [0..MAX_PATH] of WideChar;
Details: TThemedElementDetails;
Margins: TMargins;
begin
ThemeFileNameW[MAX_PATH] := #0;
BitmapFileNameW[MAX_PATH] := #0;
Result := UxTheme.GetCurrentThemeName(ThemeFileNameW, MAX_PATH, nil, 0, nil, 0) = S_OK;
if not Result then
Exit;
Details := StyleServices.GetElementDetails(twMinButtonNormal);
with Details do
Result := GetThemeFilename(StyleServices.Theme[Element], Part, State,
TMT_IMAGEFILE, BitmapFileNameW, MAX_PATH) = S_OK;
if not Result then
Exit;
with Details do
Result := GetThemeInt(StyleServices.Theme[Element], Part, State,
TMT_IMAGECOUNT, BitmapCount) = S_OK;
if not Result then
Exit;
Result := BitmapCount > 0;
if not Result then
Exit;
with Details do
if GetThemeMargins(StyleServices.Theme[Element], 0, Part, State,
TMT_SIZINGMARGINS, nil, Margins) <> S_OK then
FillChar(Margins, SizeOf(Margins), 0);
ABitmap.Margins := Margins;
Handle := SafeLoadLibrary(ThemeFileNameW, SEM_NOOPENFILEERRORBOX or SEM_FAILCRITICALERRORS);
if Handle <> 0 then
try
ABitmap.Assign(nil); // fixes GDI resource leak
ABitmap.LoadFromResourceName(Handle, TranslateBitmapFileName(BitmapFileNameW));
{ (rb) can't determine actual transparent color? }
ABitmap.TransparentColor := clFuchsia;
Result := (ABitmap.Width > 0) and (ABitmap.Height > 0);
finally
FreeLibrary(Handle);
end;
end;
{$ENDIF JVCLThemesEnabled}
//=== Global procedures ======================================================
function AlphaBlend(hdcDest: HDC; nXOriginDest, nYOriginDest, nWidthDest,
nHeightDest: Integer; hdcSrc: HDC; nXOriginSrc, nYOriginSrc, nWidthSrc,
nHeightSrc: Integer; BlendFunction: BLENDFUNCTION): BOOL; stdcall;
begin
if not GTriedLoadMsimg32Dll then
LoadMsimg32Dll;
Result := Assigned(_AlphaBlend) and
_AlphaBlend(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc,
nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, BlendFunction);
end;
function TransparentBlt(hdcDest: HDC; nXOriginDest, nYOriginDest, nWidthDest, hHeightDest: Integer;
hdcSrc: HDC; nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc: Integer;
crTransparent: UINT): BOOL; stdcall;
begin
if not GTriedLoadMsimg32Dll then
LoadMsimg32Dll;
Result := Assigned(_TransparentBlt) and
_TransparentBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, hHeightDest, hdcSrc,
nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, crTransparent);
end;
{$IFDEF JVCLThemesEnabled}
//=== { TAlphaBitmap } =======================================================
destructor TAlphaBitmap.Destroy;
begin
FreeHandle;
inherited Destroy;
end;
procedure TAlphaBitmap.Assign(Source: TPersistent);
begin
// What to do here when Source is not nil???
if not Assigned(Source) then
FreeHandle
else
;
end;
function TAlphaBitmap.CreateDIB(ADC: HDC; AWidth, AHeight: Integer): HBitmap;
begin
with FBitmapInfo.bmiHeader do
begin
biSize := SizeOf(FBitmapInfo.bmiHeader);
biWidth := AWidth;
biHeight := AHeight;
biPlanes := 1;
biBitCount := 32;
biCompression := BI_RGB;
biSizeImage := AWidth * AHeight * 4;
end;
// Create the DIB
Result := CreateDIBSection(ADC, FBitmapInfo, DIB_RGB_COLORS, FBitsMem, 0, 0);
end;
procedure TAlphaBitmap.CreateHandle(AWidth, AHeight: Integer);
var
H: HBitmap;
begin
FreeHandle;
H := CreateScreenCompatibleDC;
FDIBHandle := CreateDIB(H, AWidth, AHeight);
if FDIBHandle <> 0 then
FOldBitmap := SelectObject(H, FDIBHandle)
else
FOldBitmap := 0;
FHandle := H;
end;
procedure TAlphaBitmap.Duplicate(Src: HBitmap);
var
Bitmap: Windows.TBitmap;
begin
GetObject(Src, SizeOf(Bitmap), @Bitmap);
CreateHandle(Bitmap.bmWidth, Bitmap.bmHeight);
DupBits(Src, FHandle, Point(Bitmap.bmWidth, Bitmap.bmHeight));
end;
procedure TAlphaBitmap.FreeHandle;
begin
if FHandle <> 0 then
begin
if FDIBHandle <> 0 then
begin
if FOldBitmap <> 0 then
SelectObject(FHandle, FOldBitmap);
DeleteObject(FDIBHandle);
end;
DeleteDC(FHandle);
end;
end;
function TAlphaBitmap.GetHeight: Integer;
begin
Result := FBitmapInfo.bmiHeader.biHeight;
end;
function TAlphaBitmap.GetWidth: Integer;
begin
Result := FBitmapInfo.bmiHeader.biWidth;
end;
procedure TAlphaBitmap.InitAlpha;
var
Count: Integer;
begin
Count := Width * Height;
if BitCount < 32 then
FHasAlphaChannel := False
else
begin
FHasAlphaChannel := GetHasAlphaChannel(Data, Count);
if HasAlphaChannel then
PreMultiplyAlphaChannel(Data, Count);
end;
end;
procedure TAlphaBitmap.LoadFromResourceID(Instance: THandle; ResID: Integer);
var
Stream: TCustomMemoryStream;
BitmapInfoHeader: TBitmapInfoHeader;
BitmapHandle: HBitmap;
begin
Stream := TResourceStream.CreateFromID(Instance, ResID, RT_BITMAP);
try
Stream.Read(BitmapInfoHeader, SizeOf(TBitmapInfoHeader));
FBitCount := BitmapInfoHeader.biBitCount;
finally
Stream.Free;
end;
if FBitCount = 32 then
begin
BitmapHandle := LoadImage(Instance, PChar(ResID), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
if BitmapHandle = 0 then
Exit;
Duplicate(BitmapHandle);
DeleteObject(BitmapHandle);
InitAlpha;
end;
end;
procedure TAlphaBitmap.LoadFromResourceName(Instance: THandle; const ResName: string);
var
Stream: TCustomMemoryStream;
BitmapInfoHeader: TBitmapInfoHeader;
BitmapHandle: HBitmap;
begin
Stream := TResourceStream.Create(Instance, ResName, RT_BITMAP);
try
Stream.Read(BitmapInfoHeader, SizeOf(TBitmapInfoHeader));
FBitCount := BitmapInfoHeader.biBitCount;
finally
Stream.Free;
end;
if FBitCount = 32 then
begin
BitmapHandle := LoadImage(Instance, PChar(ResName), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
if BitmapHandle = 0 then
Exit;
Duplicate(BitmapHandle);
DeleteObject(BitmapHandle);
InitAlpha;
end;
end;
//=== { TBitmapAdapter } =====================================================
constructor TBitmapAdapter.Create;
begin
inherited Create;
FTransparentColor := clFuchsia;
end;
destructor TBitmapAdapter.Destroy;
begin
FBitmap.Free;
inherited Destroy;
end;
procedure TBitmapAdapter.Assign(Source: TPersistent);
begin
if FBitmap is TBitmap then
(FBitmap as TBitmap).Assign(Source)
else
if FBitmap is TAlphaBitmap then
(FBitmap as TAlphaBitmap).Assign(Source);
end;
procedure TBitmapAdapter.Clear;
begin
FreeAndNil(FBitmap);
end;
function TBitmapAdapter.Draw(ACanvas: TCanvas; const Rect: TRect;
AMargins: PMargins): Boolean;
begin
if (Rect.Right - Rect.Left = Width) and (Rect.Bottom - Rect.Top = Height) then
Result := DrawFixedPart(ACanvas, Rect, 0, 0)
else
begin
if AMargins = nil then
AMargins := @FMargins;
if FBitmap is TAlphaBitmap then
with TAlphaBitmap(FBitmap) do
Result := TransparentBltStretch(ACanvas.Handle, Rect, Handle,
Bounds(0, 0, Width, Height), AMargins^, FTransparentColor)
else
if FBitmap is TBitmap then
with TBitmap(FBitmap) do
Result := TransparentBltStretch(ACanvas.Handle, Rect, Canvas.Handle,
Bounds(0, 0, Width, Height), AMargins^, FTransparentColor)
else
Result := False;
end;
end;
function TBitmapAdapter.DrawFixed(ACanvas: TCanvas; const X, Y: Integer): Boolean;
begin
Result := DrawFixedPart(ACanvas, Bounds(X, Y, Width, Height), 0, 0);
end;
function TBitmapAdapter.DrawFixedPart(ACanvas: TCanvas;
const DestRect: TRect; const SrcX, SrcY: Integer): Boolean;
var
BlendFunction: TBlendFunction;
W, H: Integer;
begin
W := DestRect.Right - DestRect.Left;
H := DestRect.Bottom - DestRect.Top;
if FBitmap is TAlphaBitmap then
begin
with TAlphaBitmap(FBitmap) do
begin
BlendFunction.BlendOp := AC_SRC_OVER;
BlendFunction.BlendFlags := 0;
BlendFunction.SourceConstantAlpha := $FF;
BlendFunction.AlphaFormat := AC_SRC_ALPHA;
Result := AlphaBlend(ACanvas.Handle, DestRect.Left, DestRect.Top, W, H,
Handle, SrcX, SrcY, W, H, BlendFunction);
end;
end
else
if FBitmap is TBitmap then
with TBitmap(FBitmap) do
Result := TransparentBlt(ACanvas.Handle, DestRect.Left, DestRect.Top, W, H,
Canvas.Handle, SrcX, SrcY, W, H, Self.TransparentColor)
else
Result := False;
end;
function TBitmapAdapter.DrawPart(ACanvas: TCanvas; const SrcRect,
DestRect: TRect; AMargins: PMargins): Boolean;
begin
// Same width/height?
if (SrcRect.Right - SrcRect.Left = DestRect.Right - DestRect.Left) and
(SrcRect.Bottom - SrcRect.Top = DestRect.Bottom - DestRect.Top) then
Result := DrawFixedPart(ACanvas, DestRect, SrcRect.Left, SrcRect.Top)
else
begin
if AMargins = nil then
AMargins := @FMargins;
if FBitmap is TAlphaBitmap then
with TAlphaBitmap(FBitmap) do
Result := TransparentBltStretch(ACanvas.Handle, DestRect, Handle, SrcRect,
AMargins^, Self.TransparentColor)
else
if FBitmap is TBitmap then
with TBitmap(FBitmap) do