/
Vcl.Styles.Utils.Menus.pas
2268 lines (2063 loc) · 67.6 KB
/
Vcl.Styles.Utils.Menus.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
// **************************************************************************************************
//
// Unit Vcl.Styles.Utils.Menus
// unit for the VCL Styles Utils
// https://github.com/RRUZ/vcl-styles-utils/
//
// 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/
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
// ANY KIND, either express or implied. See the License for the specific language governing rights
// and limitations under the License.
//
//
// Portions created by Mahdi Safsafi [SMP3] e-mail SMP@LIVE.FR
// Portions created by Rodrigo Ruz V. are Copyright (C) 2013-2023 Rodrigo Ruz V.
// All Rights Reserved.
//
// **************************************************************************************************
// Contributors :
//
// gandf https://github.com/gandf
//
// **************************************************************************************************
unit Vcl.Styles.Utils.Menus;
interface
{$DEFINE UseVCLStyleUtilsMenu}
// {$IF CompilerVersion >= 27} // uncomment these lines if you want to use the VCL Styles Menus Hooks
// {$UNDEF UseVCLStyleUtilsMenu} // included on XE6-XE8 (Embarcadero Version)
// {$IFEND} //
uses
System.Classes,
System.Types,
System.SysUtils,
System.Math,
Winapi.Windows,
Winapi.Messages,
Winapi.UxTheme,
Vcl.Themes,
Vcl.Graphics,
Vcl.ImgList,
Vcl.GraphUtil,
Vcl.Controls,
Vcl.Menus,
Vcl.Styles.Utils.SysStyleHook;
const
{ The Undocumented Messages }
MN_SETHMENU = $01E0;
MN_GETHMENU = $01E1;
MN_SIZEWINDOW = $01E2;
MN_OPENHIERARCHY = $01E3;
MN_CLOSEHIERARCHY = $01E4;
MN_SELECTITEM = $01E5;
MN_CANCELMENUS = $01E6;
MN_SELECTFIRSTVALIDITEM = $01E7;
MN_GETPPOPUPMENU = $01EA;
MN_FINDMENUWINDOWFROMPOINT = $01EB;
MN_SHOWPOPUPWINDOW = $01EC;
MN_BUTTONDOWN = $01ED;
MN_MOUSEMOVE = $01EE;
MN_BUTTONUP = $01EF;
MN_SETTIMERTOOPENHIERARCHY = $01F0;
MN_DBLCLK = $001F1;
MN_BUTTONDOWN_UP = $FFFFFFFC;
MN_BUTTONDOWN_DOWN = $FFFFFFFD;
WM_UAHDESTROYWINDOW = $0090;
WM_UAHDRAWMENU = $0091;
WM_UAHDRAWMENUITEM = $0092;
WM_UAHINITMENU = $0093;
WM_UAHMEASUREMENUITEM = $0094;
WM_UAHNCPAINTMENUPOPUP = $0095;
{ MARLETT Font Char Const }
MARLETT_RESTORE_CHAR = Char(50);
MARLETT_MINIMIZE_CHAR = Char(48);
MARLETT_CLOSE_CHAR = Char(114);
MARLETT_MAXIMIZE_CHAR = Char(49);
MARLETT_LEFT_ARROW_SCROLL_CHAR = Char(51);
MARLETT_RIGHT_ARROW_SCROLL_CHAR = Char(52);
MARLETT_UP_ARROW_SCROLL_CHAR = Char(53);
MARLETT_DOWN_ARROW_SCROLL_CHAR = Char(54);
MARLETT_PAGE_DOWN_ARROW_CHAR = Char(55);
MARLETT_RIGHT_ARROW_SUBMENU_CHAR = Char(56);
MARLETT_UP_ARROW_CHAR = Char(116);
MARLETT_DOWN_ARROW_CHAR = Char(117);
type
TSysPopupStyleHook = class;
TSysPopupItemState = set of (isHot, isDisabled, isChecked, isDefault);
TSysPopupItemStyle = (isNormal, isSep, isDropDown);
TSysPopupStyleHook = class(TSysStyleHook)
private type
{$REGION 'TSysPopupItem'}
TSysPopupItem = class
private
FIndex: integer;
FMenu: HMENU;
FHandle: HWND;
FSysParent: TSysControl;
FSysPopupStyleHook: TSysPopupStyleHook;
function GetItemRect: TRect;
function IsItemDisabled: Boolean;
function IsItemContainsSubMenu: Boolean;
function IsItemSeparator: Boolean;
function IsItemChecked: Boolean;
function IsItemDefault: Boolean;
function GetItemText: String;
function GetVCLMenuItems: TMenuItem;
function GetVCLMenuItemsFast: TMenuItem;
function GetItemBitmap: HBITMAP;
function IsItemRadioCheck: Boolean;
// function isItemVisible: Boolean;
function IsItemOwnerDraw: Boolean;
function GetItemID: WORD;
function GetVCLRealItem: TMenuItem;
public
constructor Create(SysPopupStyleHook: TSysPopupStyleHook; SysParent: TSysControl; const Index: integer; const Menu: HMENU); virtual;
Destructor Destroy; override;
property ID: WORD read GetItemID;
property ItemRect: TRect read GetItemRect;
property Disabled: Boolean read IsItemDisabled;
property Separator: Boolean read IsItemSeparator;
property HasSubMenu: Boolean read IsItemContainsSubMenu;
property Checked: Boolean read IsItemChecked;
// property Visible: Boolean read isItemVisible;
property RadioCheck: Boolean read IsItemRadioCheck;
property DefaultItem: Boolean read IsItemDefault;
property Text: String read GetItemText;
property OwnerDraw: Boolean read IsItemOwnerDraw;
property VCLMenuItems: TMenuItem read GetVCLMenuItemsFast;
property VCLItem: TMenuItem read GetVCLRealItem;
property Bitmap: HBITMAP read GetItemBitmap;
end;
{$ENDREGION}
var
FOffset: Integer;
FOffsetCache: Integer;
FSeparatorHeightCache: Integer;
FItemHeightCache: Integer;
FItemsPainted: Boolean;
FParentSubItemPainted: Boolean;
FPreviousHotItemIndex: integer;
FPaintFirstItemFromMenu: Boolean;
FKeyIndex: integer;
FSysPopupItem: TSysPopupItem;
FCount: integer;
FMenu: HMENU;
FVCLMenuItems: TMenuItem;
FNCRect: TRect;
FEnterWithKeyboard: Boolean;
FPersistentHotKeys: Boolean;
FMenuBarHook: TObject;
function GetMenuFromHandle(AHandle: HWND): HMENU;
function GetItemsCount: integer;
procedure MNSELECTITEM(var Message: TMessage); message MN_SELECTITEM;
procedure WMPRINT(var Message: TMessage); message WM_PRINT;
function GetSysPopupItem(Index: integer): TSysPopupItem;
function GetRightToLeft: Boolean;
protected
procedure EraseItem(Canvas: TCanvas; const Index: integer; const ItemRect: TRect); virtual;
procedure DoDrawItem(Canvas: TCanvas; const Index: integer; const ForceIsNotHot: Boolean = False);
procedure DrawItem(Canvas: TCanvas; const Index: integer; const ItemRect: TRect; const ItemText: String; const State: TSysPopupItemState;
const Style: TSysPopupItemStyle); Virtual;
procedure PaintBackground(Canvas: TCanvas); override;
procedure GetNbSeparator(var PNbSeparator: Integer; const PIndex: Integer);
function GetMenuItemHeight(const Index: Integer): Integer;
function GetOffset(UseCache: Boolean): Integer;
function ItemIsVisible(const Index: Integer): Boolean;
procedure SetMaxOffset();
procedure RefreshMenu();
procedure GetItemHeight();
procedure GetSeparatorHeight();
function GetClientRectHeight(var PValue: TRect): Integer;
function GetItemClicked(var PButton: Byte; PInitPos: TPoint): Integer;
function GetMousePos(): TPoint;
procedure WndProc(var Message: TMessage); override;
procedure UpdateColors; override;
procedure SetOffset(PValue: Integer);
function GetBottom(const Index: Integer): Integer;
public
constructor Create(AHandle: THandle); override;
Destructor Destroy; override;
property Menu: HMENU read FMenu;
property Items[Index: integer]: TSysPopupItem read GetSysPopupItem;
property Count: integer read FCount;
property RightToLeft: Boolean read GetRightToLeft;
property Offset: Integer read FOffset write SetOffset;
end;
implementation
{
RANGE CHECKS OFF .
IMPLICIT_STRING_CAST_LOSS OFF .
}
{$R-,WARN IMPLICIT_STRING_CAST_LOSS OFF}
uses
Vcl.Forms,
Vcl.Styles.Utils.Misc,
Vcl.Styles.Utils.SysControls,
Vcl.Styles.Utils.Graphics;
type
TControlClass = Class(TControl);
function GetBmpInfo(hBmp: HBITMAP): Bitmap;
begin
ZeroMemory(@Result, sizeof(Bitmap));
GetObject(hBmp, sizeof(Result), @Result);
end;
function GetBitmapHeight(hBmp: HBITMAP): integer;
begin
Result := GetBmpInfo(hBmp).bmHeight;
end;
function GetBitmapWidth(hBmp: HBITMAP): integer;
begin
Result := GetBmpInfo(hBmp).bmWidth;
end;
function BmpToIcon(hBmp: HBITMAP): HICON;
var
Bmp: Bitmap;
hbmMask: HBITMAP;
DC: HDC;
piconinfo: TIconInfo;
Icon: HICON;
begin
Icon := 0;
FillChar(Bmp, sizeof(Bitmap), Char(0));
if GetObject(hBmp, sizeof(Bitmap), @Bmp) > 0 then
begin
DC := GetDC(0);
if DC <> 0 then
try
hbmMask := CreateCompatibleBitmap(DC, Bmp.bmWidth, Bmp.bmHeight);
if hbmMask <> 0 then
try
ZeroMemory(@piconinfo, sizeof(piconinfo));
piconinfo.fIcon := True;
piconinfo.hbmColor := hBmp;
piconinfo.hbmMask := hbmMask;
Icon := CreateIconIndirect(piconinfo);
finally
DeleteObject(hbmMask);
end;
finally
ReleaseDC(0, DC);
end;
end;
Result := Icon;
end;
function GetMenuItemPos(Menu: HMENU; ID: integer): integer;
var
i: integer;
pMenuItemInfo: TMenuItemInfo;
begin
Result := -1;
if Menu = 0 then
Exit;
for i := 0 to GetMenuItemCount(Menu) do
begin
FillChar(pMenuItemInfo, sizeof(pMenuItemInfo), Char(0));
pMenuItemInfo.cbSize := sizeof(pMenuItemInfo);
pMenuItemInfo.fMask := MIIM_ID;
if (GetMenuItemInfo(Menu, i, True, pMenuItemInfo)) then
if pMenuItemInfo.wID = Cardinal(ID) then
Exit(i);
end;
end;
function IsItemHILITE(Menu: HMENU; const ItemIndex: integer): Boolean;
var
pMenuItemInfo: TMenuItemInfo;
begin
Result := False;
FillChar(pMenuItemInfo, sizeof(pMenuItemInfo), Char(0));
pMenuItemInfo.cbSize := sizeof(TMenuItemInfo);
pMenuItemInfo.fMask := MIIM_STATE;
if GetMenuItemInfo(Menu, ItemIndex, True, pMenuItemInfo) then
Result := (pMenuItemInfo.fState and MFS_HILITE) = MFS_HILITE;
end;
{ TSysPopupStyleHook }
constructor TSysPopupStyleHook.Create(AHandle: THandle);
begin
inherited;
{$IF CompilerVersion > 23}
StyleElements := [seFont, seClient, seBorder];
{$ELSE}
OverridePaint := True;
OverridePaintNC := True;
OverrideFont := True;
{$IFEND}
FPreviousHotItemIndex := -1;
FKeyIndex := -1;
FItemsPainted := False;
FSysPopupItem := nil;
FVCLMenuItems := nil;
Offset := 0;
FSeparatorHeightCache := 1;
FItemHeightCache := 1;
FEnterWithKeyboard := False;
FPersistentHotKeys := False;
FMenuBarHook := nil;
// Font := Screen.MenuFont;
end;
destructor TSysPopupStyleHook.Destroy;
begin
if Assigned(FSysPopupItem) then
FreeAndNil(FSysPopupItem);
inherited;
end;
procedure TSysPopupStyleHook.DoDrawItem(Canvas: TCanvas; const Index: integer; const ForceIsNotHot: Boolean = False);
var
LRect, LRect2, LItemRect: TRect;
P, P2: TPoint;
State: TSysPopupItemState;
Style: TSysPopupItemStyle;
LText: String;
SaveIndex: integer;
LSysPopupItem: TSysPopupItem;
ClientRectHeight: Integer;
begin
if (Index < 0) or (Index > Count - 1) then
Exit;
LSysPopupItem := Items[Index];
LItemRect := LSysPopupItem.ItemRect;
P2 := Point(LItemRect.Left, LItemRect.Bottom);
ScreenToClient(Handle, P2);
P2.Y := P2.Y - GetOffset(True); //add offset
GetMenuItemRect(0, FMenu, Index, LRect);
//OutputDebugString(PChar(Format('Index %d Width %d Height %d Left %d Top %d', [Index, LRect.Width, LRect.Height, LRect.Left, LRect.Top])));
ClientRectHeight := GetClientRectHeight(LRect2);
LRect2.Height := ClientRectHeight;
//prevent draw not visible items on larger menus
if not PtInRect (LRect2, P2) then
Exit;
P := Point(LItemRect.Left, LItemRect.Top);
ScreenToClient(Handle, P);
P2 := P;
P2.Y := P2.Y - GetOffset(True); //add offset
if ClientRectHeight>LRect.Height then
LRect2.Height:= ClientRectHeight - LRect.Height;
//prevent draw not visible items on larger menus
if not PtInRect (LRect2, P2) then
Exit;
LItemRect := Rect(P.X, P.Y, P.X + LItemRect.Width, P.Y + LItemRect.Height);
if LItemRect.Left < 2 then
LItemRect.Left := 2;
inc(LItemRect.Right, 4);
if LItemRect.Top < 2 then
inc(LItemRect.Top, 2);
{ Item State }
State := [];
if not ForceIsNotHot then
if index <> FPreviousHotItemIndex then
Include(State, isHot);
if LSysPopupItem.Disabled then
Include(State, isDisabled);
if LSysPopupItem.Checked then
Include(State, isChecked);
if LSysPopupItem.DefaultItem then
Include(State, isDefault);
{ Item Style }
Style := isNormal;
if LSysPopupItem.Separator then
Style := isSep;
if LSysPopupItem.HasSubMenu then
Style := isDropDown;
LText := '';
if Style <> isSep then
LText := LSysPopupItem.Text;
SaveIndex := SaveDC(Canvas.Handle);
try
EraseItem(Canvas, Index, LItemRect);
finally
RestoreDC(Canvas.Handle, SaveIndex)
end;
SaveIndex := SaveDC(Canvas.Handle);
try
DrawItem(Canvas, Index, LItemRect, LText, State, Style);
finally
RestoreDC(Canvas.Handle, SaveIndex)
end;
end;
procedure TSysPopupStyleHook.DrawItem(Canvas: TCanvas; const Index: integer; const ItemRect: TRect; const ItemText: String; const State: TSysPopupItemState;
const Style: TSysPopupItemStyle);
var
LTextRect: TRect;
DC: HDC;
procedure DrawSubMenu(const ItemRect: TRect);
var
LBitmap: TBitmap;
LSubMenuDetails: TThemedElementDetails;
LSubMenuDetail: TThemedMenu;
SubMenuSize: TSize;
LSubMenuRect: TRect;
begin
LSubMenuRect := Rect(0, 0, 0, 0);
LSubMenuDetail := tmPopupSubMenuNormal;
if isDisabled in State then
LSubMenuDetail := tmPopupSubMenuDisabled;
LSubMenuDetails := StyleServices.GetElementDetails(LSubMenuDetail);
StyleServices.GetElementSize(DC, LSubMenuDetails, esActual, SubMenuSize);
if not RightToLeft then
LSubMenuRect := Rect(ItemRect.Right - SubMenuSize.cx, ItemRect.Top, ItemRect.Right, ItemRect.Top + SubMenuSize.cy)
else
LSubMenuRect := Rect(ItemRect.Left + 4, ItemRect.Top, ItemRect.Left + 4 + SubMenuSize.Width, ItemRect.Bottom);
LBitmap := TBitmap.Create;
try
LBitmap.SetSize(SubMenuSize.Width, SubMenuSize.Height);
LBitmap.Canvas.Brush.Color := clFuchsia;
LBitmap.Canvas.FillRect(Rect(0, 0, SubMenuSize.Width, SubMenuSize.Height));
StyleServices.DrawElement(LBitmap.Canvas.Handle, LSubMenuDetails, Rect(0, 0, SubMenuSize.Width, SubMenuSize.Height));
if RightToLeft then
begin
RotateBitmap(LBitmap, DegToRad(180), False, clFuchsia);
inc(LSubMenuRect.Top, (LBitmap.Height div 2) - 2);
End
else
Dec(LSubMenuRect.Left, 4);
TransparentBlt(DC, LSubMenuRect.Left, LSubMenuRect.Top, SubMenuSize.Width, SubMenuSize.Height, LBitmap.Canvas.Handle, 0, 0, SubMenuSize.Width,
SubMenuSize.Height, clFuchsia);
finally
LBitmap.Free;
end;
Dec(LTextRect.Right, LSubMenuRect.Width);
end;
procedure DrawSpecialChar(DC: HDC; const Sign: Char; DestRect: TRect; const Bold: Boolean = False; const Disabled: Boolean = False);
var
LogFont: TLogFont;
pOldFont: HGDIOBJ;
AFont: HFONT;
oldColor: COLORREF;
OldMode: integer;
begin
LogFont.lfHeight := DestRect.Height;
LogFont.lfWidth := 0;
LogFont.lfEscapement := 0;
LogFont.lfOrientation := 0;
if Bold then
LogFont.lfWeight := FW_BOLD
else
LogFont.lfWeight := FW_NORMAL;
LogFont.lfItalic := 0;
LogFont.lfUnderline := 0;
LogFont.lfStrikeOut := 0;
LogFont.lfCharSet := DEFAULT_CHARSET;
LogFont.lfOutPrecision := OUT_DEFAULT_PRECIS;
LogFont.lfClipPrecision := CLIP_DEFAULT_PRECIS;
LogFont.lfQuality := DEFAULT_QUALITY;
LogFont.lfPitchAndFamily := DEFAULT_PITCH;
LogFont.lfFaceName := 'Marlett';
if Disabled then
oldColor := ColorToRGB(StyleServices.GetStyleFontColor(sfPopupMenuItemTextDisabled))
else
begin
oldColor := ColorToRGB(StyleServices.GetStyleFontColor(sfPopupMenuItemTextNormal));
if isHot in State then
oldColor := ColorToRGB(StyleServices.GetStyleFontColor(sfPopupMenuItemTextHot));
if isDisabled in State then
oldColor := ColorToRGB(StyleServices.GetStyleFontColor(sfPopupMenuItemTextDisabled));
end;
AFont := CreateFontIndirect(LogFont);
if AFont <> 0 then
try
oldColor := SetTextColor(DC, oldColor);
pOldFont := SelectObject(DC, AFont);
try
OldMode := SetBkMode(DC, Transparent);
Winapi.Windows.DrawText(DC, Sign, 1, DestRect, DT_LEFT or DT_SINGLELINE);
SetBkMode(DC, OldMode);
SelectObject(DC, oldColor);
finally
if pOldFont <> 0 then
SelectObject(DC, pOldFont);
end;
finally
DeleteObject(AFont);
end;
end;
var
LThemedMenu: TThemedMenu;
LDetails: TThemedElementDetails;
LTextFormat: TTextFormat;
LSize: TSize;
LMenuItem: TMenuItem;
LOwnerDrawState: TOwnerDrawState;
P, LImageWidth, ImageIndex: integer;
LImageRect, R: TRect;
hBmp: HBITMAP;
BmpHeight, BmpWidth: integer;
Icon: HICON;
DisplayCheckedGlyph: Boolean;
Sign: Char;
LSysPopupItem: TSysPopupItem;
sShortCut: String;
LBitmap: TBitmap;
LParentMenu: TMenu;
ItemRect2: TRect;
begin
DisplayCheckedGlyph := True;
ItemRect2 := ItemRect;
ItemRect2.Top := ItemRect.Top - GetOffset(True); //add offset
ItemRect2.Height := ItemRect.Height;
LTextRect := ItemRect2;
{ Fast access . }
LSysPopupItem := Items[Index]; // Do not destroy !!
DC := Canvas.Handle;
R := ItemRect2;
LThemedMenu := tmPopupItemNormal;
if isHot in State then
LThemedMenu := tmPopupItemHot;
if isDisabled in State then
LThemedMenu := tmPopupItemDisabled;
if Style = isSep then
begin
LThemedMenu := tmPopupSeparator;
inc(R.Left, 25);
end;
LDetails := StyleServices.GetElementDetails(LThemedMenu);
if (isHot in State) and (LThemedMenu = tmPopupItemDisabled) then
begin
LDetails := StyleServices.GetElementDetails(tmPopupItemHot);
LBitmap := TBitmap.Create;
LBitmap.SetSize(R.Width, R.Height);
LBitmap.PixelFormat := pf32bit;
try
Bitmap32_SetAlphaAndColor(LBitmap, 0, 0);
StyleServices.DrawElement(LBitmap.Canvas.Handle, LDetails, Rect(0, 0, R.Width, R.Height));
_Darkness32(LBitmap, 30);
BitBlt(DC, R.Left, R.Top, R.Width, R.Height, LBitmap.Canvas.Handle, 0, 0, SRCCOPY);
finally
LBitmap.Free;
end;
end
else
if (LThemedMenu <> tmPopupItemNormal) and (LThemedMenu <> tmPopupItemDisabled) then
StyleServices.DrawElement(DC, LDetails, R);
if Style = isDropDown then
DrawSubMenu(ItemRect2);
LImageWidth := 0;
LMenuItem := LSysPopupItem.VCLMenuItems;
if LMenuItem <> nil then
LMenuItem := LSysPopupItem.VCLItem;
LParentMenu := nil;
if (LMenuItem <> nil) then
LParentMenu := LMenuItem.GetParentMenu;
if (LParentMenu <> nil) and (LParentMenu.OwnerDraw) and (@LMenuItem.OnDrawItem <> nil) then
begin
LMenuItem.OnDrawItem(LMenuItem, Canvas, ItemRect2, (isHot in State));
Exit;
end;
if (LParentMenu <> nil) and (LParentMenu.OwnerDraw) and (LMenuItem <> nil) and (@LMenuItem.OnAdvancedDrawItem <> nil) then
begin
LOwnerDrawState := [];
if isHot in State then
Include(LOwnerDrawState, odSelected);
if isDisabled in State then
Include(LOwnerDrawState, odDisabled);
if isChecked in State then
Include(LOwnerDrawState, odChecked);
if isDefault in State then
Include(LOwnerDrawState, odDefault);
LMenuItem.OnAdvancedDrawItem(LMenuItem, Canvas, ItemRect2, LOwnerDrawState);
Exit;
end;
if LMenuItem <> nil then
begin
{ Draw Vcl PopupMenu Bitmap }
ImageIndex := LMenuItem.ImageIndex;
if (ImageIndex < 0) and (LMenuItem.Bitmap <> nil) then
begin
LBitmap := LMenuItem.Bitmap;
if (LBitmap.Width = 16) and (LBitmap.Height = 16) then
begin
LImageWidth := LBitmap.Width;
LImageRect := Rect(0, 0, LBitmap.Width, LBitmap.Height);
RectVCenter(LImageRect, ItemRect2);
if not RightToLeft then
OffsetRect(LImageRect, 4, 0)
else
begin
LImageRect.Left := ItemRect2.Right - LBitmap.Width - 4;
LImageRect.Right := ItemRect2.Right;
end;
Canvas.Draw(LImageRect.Left, LImageRect.Top, LBitmap)
end
else
if (LBitmap.Width > 0) and (LBitmap.Height > 0) then
begin
LImageWidth := 16;
LImageRect := Rect(0, 0, 16, 16);
RectVCenter(LImageRect, ItemRect2);
if not RightToLeft then
OffsetRect(LImageRect, 4, 0)
else
begin
LImageRect.Left := ItemRect2.Right - 16 - 4;
LImageRect.Right := ItemRect2.Right;
end;
if (LSysPopupItem.Checked) and (not LSysPopupItem.RadioCheck) then
begin
R:=LImageRect;
InflateRect(R, 2, 2);
Canvas.Brush.Style:=bsClear;
Canvas.Pen.Color :=StyleServices.GetSystemColor(clHotLight);
Canvas.Rectangle(R);
end;
Canvas.StretchDraw(LImageRect, LBitmap);
end;
end
else
if (LMenuItem.Parent <> nil) and (LMenuItem.Parent.SubMenuImages <> nil) and (ImageIndex > -1) then
begin
LImageWidth := LMenuItem.Parent.SubMenuImages.Width;
DisplayCheckedGlyph := False;
LImageRect := Rect(0, 0, LMenuItem.Parent.SubMenuImages.Width, LMenuItem.Parent.SubMenuImages.Height);
RectVCenter(LImageRect, ItemRect2);
if not RightToLeft then
OffsetRect(LImageRect, 4, 0)
else
begin
LImageRect.Left := ItemRect2.Right - LMenuItem.Parent.SubMenuImages.Width - 4;
LImageRect.Right := ItemRect2.Right;
end;
if (LSysPopupItem.Checked) and (not LSysPopupItem.RadioCheck) then
begin
R:=LImageRect;
InflateRect(R, 2, 2);
Canvas.Brush.Style:=bsClear;
Canvas.Pen.Color :=StyleServices.GetSystemColor(clHotLight);
Canvas.Rectangle(R);
end;
LMenuItem.Parent.SubMenuImages.Draw(Canvas, LImageRect.Left, LImageRect.Top, ImageIndex);
end
else
if (LParentMenu.Images <> nil) and (ImageIndex > -1) then
begin
LImageWidth := LParentMenu.Images.Width;
DisplayCheckedGlyph := False;
LImageRect := Rect(0, 0, LParentMenu.Images.Width, LParentMenu.Images.Height);
RectVCenter(LImageRect, ItemRect2);
if not RightToLeft then
OffsetRect(LImageRect, 4, 0)
else
begin
LImageRect.Left := ItemRect2.Right - LParentMenu.Images.Width - 4;
LImageRect.Right := ItemRect2.Right;
end;
if (LSysPopupItem.Checked) and (not LSysPopupItem.RadioCheck) then
begin
R:=LImageRect;
InflateRect(R, 2, 2);
Canvas.Brush.Style:=bsClear;
Canvas.Pen.Color :=StyleServices.GetSystemColor(clHotLight);
Canvas.Rectangle(R);
end;
LParentMenu.Images.Draw(Canvas, LImageRect.Left, LImageRect.Top, ImageIndex, not LSysPopupItem.Disabled);
end;
end
else if LSysPopupItem.Bitmap > 0 then
begin
hBmp := LSysPopupItem.Bitmap;
if hBmp < HBMMENU_POPUP_MINIMIZE + 1 then
begin
{ Draw System PopupMenu Bitmap }
DisplayCheckedGlyph := False;
case hBmp of
HBMMENU_POPUP_RESTORE:
Sign := MARLETT_RESTORE_CHAR;
HBMMENU_POPUP_MINIMIZE, HBMMENU_MBAR_MINIMIZE_D:
Sign := MARLETT_MINIMIZE_CHAR;
HBMMENU_POPUP_MAXIMIZE:
Sign := MARLETT_MAXIMIZE_CHAR;
HBMMENU_POPUP_CLOSE, HBMMENU_MBAR_CLOSE_D:
Sign := MARLETT_CLOSE_CHAR;
else
Sign := Char(0);
end;
if Sign <> #0 then
begin
LImageRect := Rect(0, 0, 10, 10);
R := Rect(ItemRect2.Left, ItemRect2.Top, ItemRect2.Left + 30, ItemRect2.Bottom);
RectCenter(LImageRect, ItemRect2);
if not RightToLeft then
LImageRect.Left := ItemRect2.Left + 6 //Fix center image
else
begin
LImageRect.Left := ItemRect2.Right - 10 - 4;
LImageRect.Right := ItemRect2.Right;
end;
DrawSpecialChar(DC, Sign, LImageRect, False, (isDisabled in State));
end;
end
else
begin
{ Draw PopupMenu Bitmap }
BmpWidth := GetBitmapWidth(hBmp);
BmpHeight := GetBitmapHeight(hBmp);
if (BmpWidth > 0) and (BmpHeight > 0) then
begin
DisplayCheckedGlyph := False;
LImageRect := Rect(0, 0, BmpWidth, BmpHeight);
RectVCenter(LImageRect, ItemRect2);
if not RightToLeft then
OffsetRect(LImageRect, 4, 0)
else
begin
LImageRect.Left := ItemRect2.Right - BmpWidth - 4;
LImageRect.Right := ItemRect2.Right;
end;
Icon := BmpToIcon(hBmp);
if Icon <> 0 then
begin
if (LSysPopupItem.Checked) and (not LSysPopupItem.RadioCheck) then
begin
R:=LImageRect;
InflateRect(R, 2, 2);
Canvas.Brush.Style:=bsClear;
Canvas.Pen.Color :=StyleServices.GetSystemColor(clHotLight);
Canvas.Rectangle(R);
end;
DrawIconEX(DC, LImageRect.Left, LImageRect.Top, Icon, BmpWidth, BmpHeight, 0, 0, DI_NORMAL);
DeleteObject(Icon);
end;
end;
end;
end;
if (LSysPopupItem.Checked) then
begin
LThemedMenu := TThemedMenu(integer(tmPopupCheckNormal) + integer(LSysPopupItem.Disabled));
if LSysPopupItem.RadioCheck then
LThemedMenu := TThemedMenu(integer(tmPopupBulletNormal) + integer(LSysPopupItem.Disabled));
LDetails := StyleServices.GetElementDetails(LThemedMenu);
StyleServices.GetElementSize(DC, LDetails, esActual, LSize);
LImageRect := Rect(0, 0, LSize.Width, LSize.Height);
RectVCenter(LImageRect, ItemRect2);
if DisplayCheckedGlyph then
begin
if not RightToLeft then
OffsetRect(LImageRect, 4, 0)
else
begin
LImageRect.Left := ItemRect2.Right - LSize.Width - 4;
LImageRect.Right := ItemRect2.Right;
end;
StyleServices.DrawElement(DC, LDetails, LImageRect);
end;
end;
{ Draw Text }
LTextFormat := [tfLeft, tfVerticalCenter, tfSingleLine, tfExpandTabs];//, tfHidePrefix];
// if (LMenuItem.Parent<>nil) then
// OutputDebugString(PChar(Format('LMenuItem.Parent %s IsItemHILITE %s', [LMenuItem.Parent.Caption, BoolToStr(IsItemHILITE(LMenuItem.Parent.Handle, LMenuItem.Parent.MenuIndex), True)])));
// if FEnterWithKeyboard then
// Exclude(LTextFormat, tfHidePrefix);
if not RightToLeft then
inc(LTextRect.Left, 28)
else
begin
LTextRect.Left := ItemRect2.Left;
LTextRect.Right := ItemRect2.Right - 28;
Exclude(LTextFormat, tfLeft);
Include(LTextFormat, tfRtlReading);
Include(LTextFormat, tfRight);
end;
if LImageWidth > 0 then
begin
if not RightToLeft then
LTextRect.Left := ItemRect2.Left + LImageWidth + 8 + 4
else
begin
LTextRect.Left := ItemRect2.Left;
LTextRect.Right := ItemRect2.Right - LImageWidth - 8;
end;
end;
LDetails := StyleServices.GetElementDetails(tmPopupItemNormal);
if isHot in State then
LDetails := StyleServices.GetElementDetails(tmPopupItemHot);
if isDisabled in State then
LDetails := StyleServices.GetElementDetails(tmPopupItemDisabled);
if LSysPopupItem.DefaultItem then
Canvas.Font.Style := [fsBold];
if LMenuItem <> nil then
DrawText(Canvas.Handle, LDetails, ItemText, LTextRect, LTextFormat)
else
begin
sShortCut := '';
// http://msdn.microsoft.com/en-us/library/ms647553%28v=VS.85%29.aspx#_win32_Menu_Shortcut_Keys
P := Pos(#9, ItemText);
if P > 1 then
begin
sShortCut := Copy(ItemText, P + 1, length(ItemText) - P);
DrawText(Canvas.Handle, LDetails, Copy(ItemText, 1, P), LTextRect, LTextFormat)
end
else
DrawText(Canvas.Handle, LDetails, ItemText, LTextRect, LTextFormat)
end;
{Draw vertical menu bar}
// LDetailsBar := StyleServices.GetElementDetails(tmPopupSeparator);
// LBitmapBar := TBitmap.Create;
// LBitmapBar.SetSize(LTextRect.Height, LTextRect.Height);
// LBitmapBar.PixelFormat := pf32bit;
// LBitmapBar.AlphaFormat := afDefined;
// LImageRect := Rect(0, 0, LBitmapBar.Width, LBitmapBar.Height);
// R:=LImageRect;
// try
// StyleServices.DrawElement(LBitmapBar.Canvas.Handle, LDetailsBar, Rect(0, 0, R.Width, R.Height));
// RotateBitmap(LBitmapBar, DegToRad(90), true);
// BitBlt(DC, LTextRect.Left - 6, LTextRect.Top, 1, LTextRect.Height, LBitmapBar.Canvas.Handle, round(LTextRect.Height / 2), 0, SRCCOPY);
// finally
// LBitmapBar.Free;
// end;
{ Draw ShortCut Text . }
if LMenuItem <> nil then
begin
if LMenuItem.ShortCut <> 0 then
begin
sShortCut := ShortCutToText(LMenuItem.ShortCut);
LTextRect := ItemRect2;
if RightToLeft then
begin
LTextRect.Left := ItemRect2.Left + 14;
LTextRect.Right := LTextRect.Left + Canvas.TextWidth(sShortCut);
end
else
begin
LTextRect.Left := ItemRect2.Right - 14 - Canvas.TextWidth(sShortCut);
LTextRect.Right := ItemRect2.Right;
end;
DrawText(Canvas.Handle, LDetails, sShortCut, LTextRect, LTextFormat);
end;
end
else if sShortCut <> '' then
begin
LTextRect := ItemRect2;
if RightToLeft then
begin
LTextRect.Left := ItemRect2.Left + 14;
LTextRect.Right := LTextRect.Left + Canvas.TextWidth(sShortCut);
end
else
begin
LTextRect.Left := ItemRect2.Right - 14 - Canvas.TextWidth(sShortCut);
LTextRect.Right := ItemRect2.Right;
end;
DrawText(Canvas.Handle, LDetails, sShortCut, LTextRect, LTextFormat);
end;
end;
procedure TSysPopupStyleHook.EraseItem(Canvas: TCanvas; const Index: integer; const ItemRect: TRect);
var
LBitmap: TBitmap;
LOffset: Integer;
begin
LBitmap := TBitmap.Create;
try
LBitmap.SetSize(SysControl.Width, SysControl.Height);
PaintBackground(LBitmap.Canvas);
LOffset := GetOffset(True);
BitBlt(Canvas.Handle, ItemRect.Left, ItemRect.Top - LOffset, ItemRect.Width, ItemRect.Height, LBitmap.Canvas.Handle, ItemRect.Left, ItemRect.Top - LOffset, SRCCOPY);
finally
LBitmap.Free;
end;
end;
function TSysPopupStyleHook.GetItemsCount: integer;
begin
Result := GetMenuItemCount(FMenu);
end;
function TSysPopupStyleHook.GetMenuFromHandle(AHandle: HWND): HMENU;
begin
Result := HMENU(SendMessage(AHandle, MN_GETHMENU, 0, 0));
end;
function TSysPopupStyleHook.GetRightToLeft: Boolean;
var
pMenuItemInfo: TMenuItemInfo;
begin
Result := False;
FillChar(pMenuItemInfo, sizeof(pMenuItemInfo), Char(0));
pMenuItemInfo.cbSize := sizeof(TMenuItemInfo);
pMenuItemInfo.fMask := MIIM_TYPE;
if GetMenuItemInfo(FMenu, 0, True, pMenuItemInfo) then
Result := ((pMenuItemInfo.fType and MFT_RIGHTORDER) = MFT_RIGHTORDER) or ((pMenuItemInfo.fType and MFT_RIGHTJUSTIFY) = MFT_RIGHTJUSTIFY);
end;
function TSysPopupStyleHook.GetSysPopupItem(Index: integer): TSysPopupItem;
begin
Result := nil;
if (Index > -1) and (index <= Count) then
begin
if Assigned(FSysPopupItem) then
FreeAndNil(FSysPopupItem);
FSysPopupItem := TSysPopupItem.Create(Self, SysControl, Index, FMenu);
Result := FSysPopupItem;
end;
end;
procedure TSysPopupStyleHook.PaintBackground(Canvas: TCanvas);
begin
StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(tmPopupBorders), SysControl.ClientRect);
end;
procedure TSysPopupStyleHook.UpdateColors;
begin
inherited;
Font := Screen.MenuFont;
end;
type
TSubMenuItemInfo = record
Menu: HMENU;
WindowHandle: HWND;
ItemIndex: integer;
end;
var
SubMenuItemInfoArray: array of TSubMenuItemInfo;
procedure TSysPopupStyleHook.MNSELECTITEM(var Message: TMessage);
var
DC: HDC;
Canvas: TCanvas;
Index, Index2: integer;
i: WORD;