1
0

PasTools.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. unit PasTools;
  2. interface
  3. {$WARN SYMBOL_PLATFORM OFF}
  4. uses
  5. Windows, Types, Classes, ComCtrls, ExtCtrls, Controls, Dialogs, Forms, Messages, Graphics, SysUtils;
  6. function Construct(ComponentClass: TComponentClass; Owner: TComponent): TComponent;
  7. {$EXTERNALSYM IsWin7}
  8. function IsWin7: Boolean;
  9. {$EXTERNALSYM IsWin8}
  10. function IsWin8: Boolean;
  11. {$EXTERNALSYM CutToChar}
  12. function CutToChar(var Str: string; Ch: Char; Trim: Boolean): string;
  13. procedure FilterToFileTypes(Filter: string; FileTypes: TFileTypeItems);
  14. // Note that while we based our scaling on pixels-per-inch,
  15. // VCL actually scales based on font size
  16. const
  17. CM_DPICHANGED = WM_USER + $2000 + 10;
  18. function HasSystemParametersInfoForPixelsPerInch: Boolean;
  19. function SystemParametersInfoForPixelsPerInch(
  20. uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL;
  21. function GetMonitorFromControl(Control: TControl): TMonitor;
  22. function GetMonitorPixelsPerInch(Monitor: TMonitor): Integer;
  23. function GetControlPixelsPerInch(Control: TControl): Integer;
  24. function GetComponentPixelsPerInch(Component: TComponent): Integer;
  25. function LoadDimension(Dimension: Integer; PixelsPerInch: Integer; Control: TControl): Integer;
  26. function StrToDimensionDef(Str: string; PixelsPerInch: Integer; Control: TControl; Default: Integer): Integer;
  27. function SaveDimension(Dimension: Integer): Integer;
  28. function DimensionToDefaultPixelsPerInch(Dimension: Integer): Integer;
  29. function ScaleByPixelsPerInch(Dimension: Integer; Monitor: TMonitor): Integer; overload;
  30. function ScaleByPixelsPerInch(Dimension: Integer; Control: TControl): Integer; overload;
  31. function ScaleByPixelsPerInchFromSystem(Dimension: Integer; Control: TControl): Integer;
  32. function LoadPixelsPerInch(S: string; Control: TControl): Integer;
  33. function SavePixelsPerInch(Control: TControl): string;
  34. function SaveDefaultPixelsPerInch: string;
  35. function CalculateTextHeight(Canvas: TCanvas): Integer;
  36. function ScaleByTextHeight(Control: TControl; Dimension: Integer): Integer;
  37. function ScaleByTextHeightRunTime(Control: TControl; Dimension: Integer): Integer;
  38. function ScaleByControlTextHeightRunTime(Canvas: TCanvas; Dimension: Integer): Integer;
  39. function GetSystemMetricsForControl(Control: TControl; nIndex: Integer): Integer;
  40. type
  41. TImageListSize = (ilsSmall, ilsLarge);
  42. procedure NeedShellImageLists;
  43. function ShellImageListForSize(Width: Integer): TImageList;
  44. function ShellImageListForControl(Control: TControl; Size: TImageListSize): TImageList;
  45. function ControlHasRecreationPersistenceData(Control: TControl): Boolean;
  46. function IsAppIconic: Boolean;
  47. procedure SetAppIconic(Value: Boolean);
  48. procedure SetAppMainForm(Value: TForm);
  49. procedure SetAppTerminated(Value: Boolean);
  50. procedure ForceColorChange(Control: TWinControl);
  51. function IsUncPath(Path: string): Boolean;
  52. function FileExistsFix(Path: string): Boolean;
  53. function DirectoryExistsFix(Path: string): Boolean;
  54. const
  55. FIND_FIRST_EX_LARGE_FETCH_PAS = 2; // VCLCOPY (actually should be part of Winapi)
  56. function FindFirstEx(
  57. const Path: string; Attr: Integer; var F: TSearchRec; AdditionalFlags: DWORD = 0;
  58. SearchOp: _FINDEX_SEARCH_OPS = FindExSearchNameMatch): Integer;
  59. function SupportsDarkMode: Boolean;
  60. procedure AllowDarkModeForWindow(Control: TWinControl; Allow: Boolean); overload;
  61. procedure AllowDarkModeForWindow(Handle: THandle; Allow: Boolean); overload;
  62. procedure RefreshColorMode;
  63. procedure ResetSysDarkTheme;
  64. function GetSysDarkTheme: Boolean;
  65. type
  66. TApiPathEvent = function(Path: string): string;
  67. var
  68. OnApiPath: TApiPathEvent = nil;
  69. {$EXTERNALSYM ApiPath}
  70. function ApiPath(Path: string): string;
  71. type
  72. TAppLogEvent = procedure(S: string);
  73. var
  74. OnAppLog: TAppLogEvent = nil;
  75. {$EXTERNALSYM AppLog}
  76. procedure AppLog(S: string);
  77. type
  78. TControlScrollBeforeUpdate = procedure(ObjectToValidate: TObject) of object;
  79. TControlScrollAfterUpdate = procedure of object;
  80. TCustomControlScrollOnDragOver = class
  81. private
  82. FOnBeforeUpdate: TControlScrollBeforeUpdate;
  83. FOnAfterUpdate: TControlScrollAfterUpdate;
  84. FDragOverTimer: TTimer;
  85. FControl: TControl;
  86. FDragOverTime: FILETIME;
  87. FLastVScrollTime: FILETIME;
  88. FVScrollCount: Integer;
  89. procedure DragOverTimer(Sender: TObject);
  90. procedure BeforeUpdate(ObjectToValidate: TObject);
  91. procedure AfterUpdate;
  92. public
  93. constructor Create(Control: TControl; ScheduleDragOver: Boolean);
  94. destructor Destroy; override;
  95. procedure StartDrag; virtual;
  96. procedure EndDrag; virtual;
  97. procedure DragOver(Point: TPoint); virtual; abstract;
  98. property OnBeforeUpdate: TControlScrollBeforeUpdate read FOnBeforeUpdate write FOnBeforeUpdate;
  99. property OnAfterUpdate: TControlScrollAfterUpdate read FOnAfterUpdate write FOnAfterUpdate;
  100. end;
  101. TTreeViewScrollOnDragOver = class(TCustomControlScrollOnDragOver)
  102. private
  103. FLastDragNode: TTreeNode;
  104. FLastHScrollTime: FILETIME;
  105. public
  106. procedure StartDrag; override;
  107. procedure DragOver(Point: TPoint); override;
  108. end;
  109. TListViewScrollOnDragOver = class(TCustomControlScrollOnDragOver)
  110. public
  111. procedure DragOver(Point: TPoint); override;
  112. end;
  113. TListBoxScrollOnDragOver = class(TCustomControlScrollOnDragOver)
  114. public
  115. procedure DragOver(Point: TPoint); override;
  116. end;
  117. implementation
  118. uses
  119. StdCtrls, MultiMon, ShellAPI, Generics.Collections, CommCtrl, ImgList, Registry;
  120. const
  121. DDExpandDelay = 15000000;
  122. DDMaxSlowCount = 3;
  123. DDVScrollDelay = 2000000;
  124. DDHScrollDelay = 2000000;
  125. DDDragStartDelay = 500000;
  126. function Construct(ComponentClass: TComponentClass; Owner: TComponent): TComponent;
  127. begin
  128. Result := ComponentClass.Create(Owner);
  129. end;
  130. function IsWin7: Boolean;
  131. begin
  132. Result := CheckWin32Version(6, 1);
  133. end;
  134. function IsWin8: Boolean;
  135. begin
  136. Result := CheckWin32Version(6, 2);
  137. end;
  138. function CutToChar(var Str: string; Ch: Char; Trim: Boolean): string;
  139. var
  140. P: Integer;
  141. begin
  142. P := Pos(Ch, Str);
  143. if P > 0 then
  144. begin
  145. Result := Copy(Str, 1, P-1);
  146. Delete(Str, 1, P);
  147. end
  148. else
  149. begin
  150. Result := Str;
  151. Str := '';
  152. end;
  153. if Trim then Result := SysUtils.Trim(Result);
  154. end;
  155. procedure FilterToFileTypes(Filter: string; FileTypes: TFileTypeItems);
  156. var
  157. Item: TFileTypeItem;
  158. begin
  159. while Filter <> '' do
  160. begin
  161. Item := FileTypes.Add();
  162. Item.DisplayName := CutToChar(Filter, '|', True);
  163. Item.FileMask := CutToChar(Filter, '|', True);
  164. end;
  165. end;
  166. type
  167. TGetDpiForMonitorFunc =
  168. function (hMonitor: HMONITOR; MonitorType: Integer; out DpiX, DpiY: Cardinal): HRESULT; stdcall;
  169. TGetSystemMetricsForDpiFunc =
  170. function (nIndex: Integer; Dpi: Cardinal): Integer; stdcall;
  171. TSystemParametersInfoForDpiFunc =
  172. function (uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL; stdcall;
  173. const
  174. MDT_EFFECTIVE_DPI = 0;
  175. var
  176. GetDpiForMonitor: TGetDpiForMonitorFunc = nil;
  177. GetSystemMetricsForDpi: TGetSystemMetricsForDpiFunc = nil;
  178. SystemParametersInfoForDpi: TSystemParametersInfoForDpiFunc = nil;
  179. function HasSystemParametersInfoForPixelsPerInch: Boolean;
  180. begin
  181. Result := Assigned(SystemParametersInfoForDpi);
  182. end;
  183. function SystemParametersInfoForPixelsPerInch(
  184. uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL;
  185. begin
  186. if HasSystemParametersInfoForPixelsPerInch then
  187. begin
  188. Result := SystemParametersInfoForDpi(uiAction, uiParam, pvParam, fWinIni, dpi);
  189. end
  190. else
  191. begin
  192. Result := SystemParametersInfo(uiAction, uiParam, pvParam, fWinIni);
  193. end;
  194. end;
  195. function GetMonitorPixelsPerInch(Monitor: TMonitor): Integer;
  196. var
  197. DpiX, DpiY: Cardinal;
  198. begin
  199. if Assigned(GetDpiForMonitor) and
  200. (GetDpiForMonitor(Monitor.Handle, MDT_EFFECTIVE_DPI, DpiX, DpiY) = S_OK) then
  201. begin
  202. Result := DpiX;
  203. end
  204. else
  205. begin
  206. Result := Screen.PixelsPerInch;
  207. end;
  208. end;
  209. function GetMonitorFromControl(Control: TControl): TMonitor;
  210. begin
  211. if Control.Parent <> nil then
  212. begin
  213. Result := GetMonitorFromControl(Control.Parent);
  214. end
  215. else
  216. if Control is TCustomForm then
  217. begin
  218. Result := TCustomForm(Control).Monitor;
  219. end
  220. else
  221. if (Control is TWinControl) and TWinControl(Control).HandleAllocated then
  222. begin
  223. Result := Screen.MonitorFromWindow(TWinControl(Control).Handle);
  224. end
  225. else
  226. begin
  227. Result := nil;
  228. end;
  229. end;
  230. function GetControlPixelsPerInch(Control: TControl): Integer;
  231. var
  232. Form: TCustomForm;
  233. Monitor: TMonitor;
  234. begin
  235. if Assigned(GetDpiForMonitor) then // optimization
  236. begin
  237. Form := GetParentForm(Control);
  238. if Assigned(Form) then
  239. begin
  240. // By default, scale according to what the form is so far rendered on.
  241. // If the monitor perceived DPI does not match its monitor DPI, it's because the WM_DPICHANGED is still pending.
  242. Result := TForm(Form).PixelsPerInch;
  243. end
  244. else
  245. begin
  246. Monitor := GetMonitorFromControl(Control);
  247. if Monitor = nil then
  248. begin
  249. Assert(False);
  250. Monitor := Screen.PrimaryMonitor;
  251. end;
  252. Result := GetMonitorPixelsPerInch(Monitor);
  253. end;
  254. end
  255. else
  256. begin
  257. Result := Screen.PixelsPerInch;
  258. end;
  259. end;
  260. function GetComponentPixelsPerInch(Component: TComponent): Integer;
  261. begin
  262. Result := GetControlPixelsPerInch(TControl(Component.Owner));
  263. end;
  264. function LoadDimension(Dimension: Integer; PixelsPerInch: Integer; Control: TControl): Integer;
  265. begin
  266. Result := MulDiv(Dimension, GetControlPixelsPerInch(Control), PixelsPerInch);
  267. end;
  268. function StrToDimensionDef(Str: string; PixelsPerInch: Integer; Control: TControl; Default: Integer): Integer;
  269. begin
  270. if TryStrToInt(Str, Result) then
  271. begin
  272. Result := LoadDimension(Result, PixelsPerInch, Control);
  273. end
  274. else
  275. begin
  276. Result := Default;
  277. end;
  278. end;
  279. function SaveDimension(Dimension: Integer): Integer;
  280. begin
  281. // noop
  282. Result := Dimension;
  283. end;
  284. function DimensionToDefaultPixelsPerInch(Dimension: Integer): Integer;
  285. begin
  286. Result := MulDiv(Dimension, USER_DEFAULT_SCREEN_DPI, Screen.PixelsPerInch);
  287. end;
  288. function ScaleByPixelsPerInch(Dimension: Integer; Monitor: TMonitor): Integer;
  289. begin
  290. Result := MulDiv(Dimension, GetMonitorPixelsPerInch(Monitor), USER_DEFAULT_SCREEN_DPI);
  291. end;
  292. function ScaleByPixelsPerInch(Dimension: Integer; Control: TControl): Integer;
  293. begin
  294. Result := MulDiv(Dimension, GetControlPixelsPerInch(Control), USER_DEFAULT_SCREEN_DPI);
  295. end;
  296. function ScaleByPixelsPerInchFromSystem(Dimension: Integer; Control: TControl): Integer;
  297. begin
  298. Result := MulDiv(Dimension, GetControlPixelsPerInch(Control), Screen.PixelsPerInch);
  299. end;
  300. function LoadPixelsPerInch(S: string; Control: TControl): Integer;
  301. begin
  302. // for backward compatibility with version that did not save the DPI,
  303. // make reasonable assumption that the configuration was saved with
  304. // the same DPI as we run now
  305. Result := StrToIntDef(S, GetControlPixelsPerInch(Control));
  306. end;
  307. function SavePixelsPerInch(Control: TControl): string;
  308. begin
  309. Result := IntToStr(GetControlPixelsPerInch(Control));
  310. end;
  311. function SaveDefaultPixelsPerInch: string;
  312. begin
  313. Result := IntToStr(USER_DEFAULT_SCREEN_DPI);
  314. end;
  315. // WORKAROUND
  316. // https://stackoverflow.com/q/9410485/850848
  317. type
  318. TFormHelper = class helper for TCustomForm
  319. public
  320. function RetrieveTextHeight: Integer;
  321. end;
  322. function TFormHelper.RetrieveTextHeight: Integer;
  323. begin
  324. Result := Self.GetInternalTextHeight;
  325. end;
  326. function CalculateTextHeight(Canvas: TCanvas): Integer;
  327. begin
  328. // RTL_COPY (TCustomForm.GetTextHeight)
  329. Result := Canvas.TextHeight('0');
  330. end;
  331. function ScaleByTextHeightImpl(Canvas: TCanvas; Dimension: Integer; TextHeight: Integer): Integer; overload;
  332. var
  333. NewTextHeight: Integer;
  334. begin
  335. // RTL_COPY (TCustomForm.ReadState)
  336. NewTextHeight := CalculateTextHeight(Canvas);
  337. if TextHeight <> NewTextHeight then
  338. begin
  339. Dimension := MulDiv(Dimension, NewTextHeight, TextHeight);
  340. end;
  341. Result := Dimension;
  342. end;
  343. function ScaleByTextHeightImpl(Control: TControl; Dimension: Integer; TextHeight: Integer): Integer; overload;
  344. var
  345. Form: TCustomForm;
  346. begin
  347. // RTL_COPY (TCustomForm.ReadState)
  348. Form := ValidParentForm(Control);
  349. Result := ScaleByTextHeightImpl(Form.Canvas, Dimension, TextHeight);
  350. end;
  351. const
  352. OurDesignTimeTextHeight = 13;
  353. function ScaleByTextHeight(Control: TControl; Dimension: Integer): Integer;
  354. var
  355. Form: TCustomForm;
  356. TextHeight: Integer;
  357. begin
  358. // RTL_COPY (TCustomForm.ReadState)
  359. Form := GetParentForm(Control);
  360. if Form = nil then
  361. begin
  362. // This should happen only for screen tip over dropped down menu.
  363. // The other condition is a temporary fix is for TCustomComboEdit on TCopyParamsFrame.
  364. Assert((Control.ClassName = 'TTBXPopupWindow') or (Control.ClassName = 'TTBXChevronPopupWindow') or ((Control.Parent <> nil) and (Control.Parent.ClassName = 'TCopyParamsFrame')) or ((Control.Parent <> nil) and (Control.Parent.Parent <> nil) and (Control.Parent.Parent.ClassName = 'TCopyParamsFrame')));
  365. Result := ScaleByPixelsPerInch(Dimension, Control);
  366. end
  367. else
  368. begin
  369. TextHeight := Form.RetrieveTextHeight;
  370. // runtime form (such as TTBFloatingWindowParent)
  371. if TextHeight = 0 then
  372. begin
  373. Result := ScaleByTextHeightRunTime(Control, Dimension);
  374. end
  375. else
  376. begin
  377. // that's our design text-size, we do not expect any other value
  378. Assert(TextHeight = OurDesignTimeTextHeight);
  379. Result := ScaleByTextHeightImpl(Control, Dimension, TextHeight);
  380. end;
  381. end;
  382. end;
  383. // this differs from ScaleByTextHeight only by enforcing
  384. // constant design-time text height
  385. function ScaleByTextHeightRunTime(Control: TControl; Dimension: Integer): Integer;
  386. begin
  387. Result := ScaleByTextHeightImpl(Control, Dimension, OurDesignTimeTextHeight);
  388. end;
  389. function ScaleByControlTextHeightRunTime(Canvas: TCanvas; Dimension: Integer): Integer;
  390. begin
  391. Result := ScaleByTextHeightImpl(Canvas, Dimension, OurDesignTimeTextHeight);
  392. end;
  393. function GetSystemMetricsForControl(Control: TControl; nIndex: Integer): Integer;
  394. begin
  395. if Assigned(GetSystemMetricsForDpi) then
  396. begin
  397. Result := GetSystemMetricsForDpi(nIndex, GetControlPixelsPerInch(Control))
  398. end
  399. else
  400. begin
  401. Result := GetSystemMetrics(nIndex);
  402. end;
  403. end;
  404. var
  405. ShellImageLists: TDictionary<Integer, TImageList> = nil;
  406. // This should be replaced with IShellItemImageFactory, as already used for thumbnails
  407. procedure InitializeShellImageLists;
  408. type
  409. TSHGetImageList = function (iImageList: integer; const riid: TGUID; var ppv: Pointer): hResult; stdcall;
  410. const
  411. IID_IImageList: TGUID = '{46EB5926-582E-4017-9FDF-E8998DAA0950}';
  412. var
  413. Lib: THandle;
  414. ImageList: Integer;
  415. Handle: THandle;
  416. Height, Width: Integer;
  417. ShellImageList: TImageList;
  418. SHGetImageList: TSHGetImageList;
  419. HR: HRESULT;
  420. begin
  421. Lib := LoadLibrary('shell32');
  422. SHGetImageList := GetProcAddress(Lib, 'SHGetImageList');
  423. ShellImageLists := TDictionary<Integer, TImageList>.Create;
  424. for ImageList := 0 to SHIL_LAST do
  425. begin
  426. // VCL have declaration for SHGetImageList in ShellAPI, but it does not link
  427. HR := SHGetImageList(ImageList, IID_IImageList, Pointer(Handle));
  428. if (HR = S_OK) and
  429. ImageList_GetIconSize(Handle, Width, Height) then
  430. begin
  431. // We could use AddOrSetValue instead, but to be on a safe side, we prefer e.g. SHIL_SMALL over SHIL_SYSSMALL,
  432. // while they actually can be the same
  433. if not ShellImageLists.ContainsKey(Width) then
  434. begin
  435. ShellImageList := TImageList.Create(Application);
  436. ShellImageList.Handle := Handle;
  437. ShellImageList.ShareImages := True;
  438. ShellImageList.DrawingStyle := dsTransparent;
  439. ShellImageLists.Add(Width, ShellImageList);
  440. end;
  441. end;
  442. end;
  443. end;
  444. procedure NeedShellImageLists;
  445. begin
  446. if ShellImageLists = nil then
  447. begin
  448. InitializeShellImageLists;
  449. end;
  450. end;
  451. function ShellImageListForSize(Width: Integer): TImageList;
  452. var
  453. ImageListPair: TPair<Integer, TImageList>;
  454. ImageListWidth: Integer;
  455. Diff, BestDiff: Integer;
  456. begin
  457. // Delay load image lists, not to waste resources in console/scripting mode
  458. NeedShellImageLists;
  459. Result := nil;
  460. BestDiff := -1;
  461. for ImageListPair in ShellImageLists do
  462. begin
  463. ImageListWidth := ImageListPair.Key;
  464. if ImageListWidth <= Width then
  465. begin
  466. Diff := Width - ImageListWidth;
  467. end
  468. else
  469. begin
  470. // Prefer smaller images over larger, so for 150%, we use 100% images, not 200%
  471. // (a larger icon would make the item row higher)
  472. Diff := ImageListWidth - Width + 1;
  473. end;
  474. if (BestDiff < 0) or (BestDiff > Diff) then
  475. begin
  476. BestDiff := Diff;
  477. Result := ImageListPair.Value;
  478. end;
  479. end;
  480. end;
  481. function ShellImageListForControl(Control: TControl; Size: TImageListSize): TImageList;
  482. var
  483. Width: Integer;
  484. begin
  485. case Size of
  486. ilsSmall: Width := 16;
  487. ilsLarge: Width := 32;
  488. else Width := 0; Assert(False);
  489. end;
  490. Width := ScaleByPixelsPerInch(Width, Control);
  491. Result := ShellImageListForSize(Width);
  492. end;
  493. type
  494. TListViewHelper = class helper for TCustomListView
  495. public
  496. function HasMemStream: Boolean;
  497. end;
  498. function TListViewHelper.HasMemStream: Boolean;
  499. begin
  500. with Self do
  501. Result := Assigned(FMemStream);
  502. end;
  503. type
  504. TTreeViewHelper = class helper for TCustomTreeView
  505. public
  506. function HasMemStream: Boolean;
  507. end;
  508. function TTreeViewHelper.HasMemStream: Boolean;
  509. begin
  510. with Self do
  511. Result := Assigned(FMemStream);
  512. end;
  513. type
  514. TRichEditHelper = class helper for TCustomRichEdit
  515. public
  516. function HasMemStream: Boolean;
  517. end;
  518. function TRichEditHelper.HasMemStream: Boolean;
  519. begin
  520. with Self do
  521. Result := Assigned(FMemStream);
  522. end;
  523. function ControlHasRecreationPersistenceData(Control: TControl): Boolean;
  524. begin
  525. // not implemented for this class as we do not use it as of now
  526. Assert(not (Control is TCustomComboBoxEx));
  527. Result :=
  528. ((Control is TCustomListView) and (Control as TCustomListView).HasMemStream) or
  529. ((Control is TCustomTreeView) and (Control as TCustomTreeView).HasMemStream) or
  530. ((Control is TCustomRichEdit) and (Control as TCustomRichEdit).HasMemStream);
  531. end;
  532. type
  533. TApplicationHelper = class helper for TApplication
  534. public
  535. function IsAppIconic: Boolean;
  536. procedure SetAppIconic(Value: Boolean);
  537. procedure SetMainForm(Value: TForm);
  538. procedure SetTerminated(Value: Boolean);
  539. end;
  540. function TApplicationHelper.IsAppIconic: Boolean;
  541. begin
  542. with Self do
  543. Result := FAppIconic;
  544. end;
  545. procedure TApplicationHelper.SetAppIconic(Value: Boolean);
  546. begin
  547. with Self do
  548. FAppIconic := Value;
  549. end;
  550. procedure TApplicationHelper.SetMainForm(Value: TForm);
  551. begin
  552. with Self do
  553. FMainForm := Value;
  554. end;
  555. procedure TApplicationHelper.SetTerminated(Value: Boolean);
  556. begin
  557. with Self do
  558. FTerminate := Value;
  559. end;
  560. function IsAppIconic: Boolean;
  561. begin
  562. Result := Application.IsAppIconic;
  563. end;
  564. procedure SetAppIconic(Value: Boolean);
  565. begin
  566. Application.SetAppIconic(Value);
  567. end;
  568. procedure SetAppMainForm(Value: TForm);
  569. begin
  570. Application.SetMainForm(Value);
  571. end;
  572. procedure SetAppTerminated(Value: Boolean);
  573. begin
  574. Application.SetTerminated(Value);
  575. end;
  576. function ApiPath(Path: string): string;
  577. begin
  578. Result := Path;
  579. if Assigned(OnApiPath) then
  580. begin
  581. Result := OnApiPath(Result);
  582. end;
  583. end;
  584. procedure ForceColorChange(Control: TWinControl);
  585. begin
  586. // particularly when changing color back to default (clWindow),
  587. // non-client area (border line) is not redrawn,
  588. // keeping previous color. force redraw here
  589. if Control.HandleAllocated then
  590. begin
  591. RedrawWindow(Control.Handle, nil, 0, RDW_INVALIDATE or RDW_FRAME);
  592. end;
  593. end;
  594. procedure AppLog(S: string);
  595. begin
  596. if Assigned(OnAppLog) then
  597. begin
  598. OnAppLog(S);
  599. end;
  600. end;
  601. { TCustomControlScrollOnDragOver }
  602. constructor TCustomControlScrollOnDragOver.Create(Control: TControl;
  603. ScheduleDragOver: Boolean);
  604. begin
  605. FControl := Control;
  606. FOnBeforeUpdate := nil;
  607. FOnAfterUpdate := nil;
  608. if ScheduleDragOver then
  609. begin
  610. FDragOverTimer := TTimer.Create(Control);
  611. FDragOverTimer.Enabled := False;
  612. FDragOverTimer.Interval := 50;
  613. FDragOverTimer.OnTimer := DragOverTimer;
  614. end
  615. else FDragOverTimer := nil;
  616. end;
  617. destructor TCustomControlScrollOnDragOver.Destroy;
  618. begin
  619. FreeAndNil(FDragOverTimer);
  620. end;
  621. procedure TCustomControlScrollOnDragOver.DragOverTimer(Sender: TObject);
  622. var
  623. P: TPoint;
  624. begin
  625. P := FControl.ScreenToClient(Mouse.CursorPos);
  626. if (P.X >= 0) and (P.X < FControl.Width) and
  627. (P.Y >= 0) and (P.Y < FControl.Height) then
  628. begin
  629. DragOver(P);
  630. end;
  631. end;
  632. procedure TCustomControlScrollOnDragOver.StartDrag;
  633. begin
  634. GetSystemTimeAsFileTime(FDragOverTime);
  635. GetSystemTimeAsFileTime(FLastVScrollTime);
  636. FVScrollCount := 0;
  637. if Assigned(FDragOverTimer) then
  638. FDragOverTimer.Enabled := True;
  639. end;
  640. procedure TCustomControlScrollOnDragOver.EndDrag;
  641. begin
  642. if Assigned(FDragOverTimer) then
  643. FDragOverTimer.Enabled := False;
  644. end;
  645. type
  646. TPublicControl = class(TControl);
  647. procedure TCustomControlScrollOnDragOver.BeforeUpdate(ObjectToValidate: TObject);
  648. var
  649. DragImages: TDragImageList;
  650. begin
  651. if Assigned(FOnBeforeUpdate) then
  652. FOnBeforeUpdate(ObjectToValidate);
  653. DragImages := TPublicControl(FControl).GetDragImages;
  654. if Assigned(DragImages) then
  655. DragImages.HideDragImage;
  656. end;
  657. procedure TCustomControlScrollOnDragOver.AfterUpdate;
  658. var
  659. DragImages: TDragImageList;
  660. begin
  661. if Assigned(FOnAfterUpdate) then
  662. FOnAfterUpdate;
  663. DragImages := TPublicControl(FControl).GetDragImages;
  664. if Assigned(DragImages) then
  665. DragImages.ShowDragImage;
  666. end;
  667. procedure TTreeViewScrollOnDragOver.StartDrag;
  668. var
  669. KeyBoardState : TKeyBoardState;
  670. begin
  671. inherited;
  672. FLastDragNode := nil;
  673. if (GetKeyState(VK_SPACE) <> 0) and GetKeyboardState(KeyBoardState) then
  674. begin
  675. KeyBoardState[VK_SPACE] := 0;
  676. SetKeyBoardState(KeyBoardState);
  677. end;
  678. GetSystemTimeAsFileTime(FLastHScrollTime);
  679. end;
  680. { TTreeViewScrollOnDragOver }
  681. procedure TTreeViewScrollOnDragOver.DragOver(Point: TPoint);
  682. var
  683. TreeView: TCustomTreeView;
  684. NbPixels: Integer;
  685. KnowTime: TFileTime;
  686. Node: TTreeNode;
  687. TempTopItem: TTreeNode;
  688. ScrollInfo: TScrollInfo;
  689. KeyBoardState : TKeyBoardState;
  690. begin
  691. TreeView := (FControl as TCustomTreeView);
  692. Node := TreeView.GetNodeAt(Point.X, Point.Y);
  693. if Assigned(Node) then
  694. begin
  695. GetSystemTimeAsFileTime(KnowTime);
  696. if GetKeyState(VK_SPACE) = 0 then
  697. begin
  698. {Expand node after 2.5 seconds: }
  699. if not Assigned(FLastDragNode) or (FLastDragNode <> Node) then
  700. begin
  701. {not previous droptarget: start timer}
  702. GetSystemTimeAsFileTime(FDragOverTime);
  703. FLastDragNode := Node
  704. end
  705. else
  706. begin
  707. if ((Int64(KnowTime) - Int64(FDragOverTime)) > DDExpandDelay) then
  708. begin
  709. TempTopItem := TreeView.TopItem;
  710. BeforeUpdate(nil);
  711. Node.Expand(False);
  712. TreeView.TopItem := TempTopItem;
  713. TreeView.Update;
  714. AfterUpdate;
  715. FDragOverTime := KnowTime;
  716. end;
  717. end;
  718. end
  719. else
  720. begin
  721. {restart timer}
  722. GetSystemTimeAsFileTime(FDragOverTime);
  723. if GetKeyboardState(KeyBoardState) then
  724. begin
  725. KeyBoardState[VK_Space] := 0;
  726. SetKeyBoardState(KeyBoardState);
  727. end;
  728. TempTopItem := TreeView.TopItem;
  729. BeforeUpdate(Node);
  730. if Node.Expanded then
  731. begin
  732. if not TreeView.Selected.HasAsParent(Node) then
  733. Node.Collapse(False);
  734. end
  735. else Node.Expand(False);
  736. TreeView.TopItem := TempTopItem;
  737. TreeView.Update;
  738. AfterUpdate;
  739. end;
  740. NbPixels := Abs(TTreeView(FControl).Font.Height);
  741. {Vertical treescrolling:}
  742. if ((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  743. ((FVScrollCount > 3) and
  744. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay Div 4))) then
  745. begin
  746. {Scroll tree up, if droptarget is topitem:}
  747. if Node = TreeView.TopItem then
  748. begin
  749. BeforeUpdate(nil);
  750. TreeView.Perform(WM_VSCROLL, SB_LINEUP, 0);
  751. AfterUpdate;
  752. GetSystemTimeAsFileTime(FLastVScrollTime);
  753. Inc(FVScrollCount);
  754. end
  755. else
  756. {Scroll tree down, if next visible item of droptarget is not visible:}
  757. begin
  758. if Point.Y + 3 * nbPixels > TreeView.Height then
  759. begin
  760. BeforeUpdate(nil);
  761. TreeView.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
  762. AfterUpdate;
  763. GetSystemTimeAsFileTime(FLastVScrollTime);
  764. Inc(FVScrollCount);
  765. end
  766. else
  767. begin
  768. FVScrollCount := 0;
  769. end;
  770. end;
  771. end; {VScrollDelay}
  772. {Horizontal treescrolling:}
  773. {Scroll tree Left}
  774. if ((Int64(KnowTime) - Int64(FLastHScrollTime)) > DDHScrollDelay) then
  775. begin
  776. GetSystemTimeAsFileTime(FLastHScrollTime);
  777. ScrollInfo.cbSize := SizeOf(ScrollInfo);
  778. ScrollInfo.FMask := SIF_ALL;
  779. GetScrollInfo(TreeView.Handle, SB_HORZ, ScrollInfo);
  780. if ScrollInfo.nMin <> ScrollInfo.nMax then
  781. begin
  782. if Point.X < 50 then
  783. begin
  784. if Node.DisplayRect(True).Right + 50 < TreeView.Width then
  785. begin
  786. BeforeUpdate(nil);
  787. TreeView.Perform(WM_HSCROLL, SB_LINELEFT, 0);
  788. AfterUpdate;
  789. end;
  790. end
  791. else
  792. if Point.X > (TreeView.Width - 50) then
  793. begin
  794. if Node.DisplayRect(True).Left > 50 then
  795. begin
  796. BeforeUpdate(nil);
  797. TreeView.Perform(WM_HSCROLL, SB_LINERIGHT, 0);
  798. AfterUpdate;
  799. end;
  800. end;
  801. end;
  802. end;
  803. end;
  804. end;
  805. { TListViewScrollOnDragOver }
  806. procedure TListViewScrollOnDragOver.DragOver(Point: TPoint);
  807. var
  808. ListView: TCustomListView;
  809. KnowTime: TFileTime;
  810. NbPixels: Integer;
  811. WParam: LongInt;
  812. begin
  813. ListView := (FControl as TCustomListView);
  814. GetSystemTimeAsFileTime(KnowTime);
  815. NbPixels := Abs(TListView(ListView).Font.Height);
  816. {Vertical scrolling, if viewstyle = vsReport:}
  817. if (TListView(ListView).ViewStyle = vsReport) and Assigned(ListView.TopItem) and
  818. (((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  819. ((FVScrollCount > DDMaxSlowCount) and
  820. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay div 4)))) then
  821. begin
  822. if (Point.Y - 3 * nbPixels <= 0) and (ListView.TopItem.Index > 0) then WParam := SB_LINEUP
  823. else
  824. if (Point.Y + 3 * nbPixels > ListView.Height) then WParam := SB_LINEDOWN
  825. else WParam := -1;
  826. if WParam >= 0 then
  827. begin
  828. BeforeUpdate(nil);
  829. ListView.Perform(WM_VSCROLL, WParam, 0);
  830. if FVScrollCount > DDMaxSlowCount then
  831. ListView.Perform(WM_VSCROLL, WParam, 0);
  832. if FVScrollCount > DDMaxSlowCount * 3 then
  833. ListView.Perform(WM_VSCROLL, WParam, 0);
  834. ListView.Update;
  835. AfterUpdate;
  836. GetSystemTimeAsFileTime(FLastVScrollTime);
  837. Inc(FVScrollCount);
  838. end
  839. else FVScrollCount := 0;
  840. end;
  841. end;
  842. { TListBoxScrollOnDragOver }
  843. procedure TListBoxScrollOnDragOver.DragOver(Point: TPoint);
  844. var
  845. ListBox: TListBox;
  846. KnowTime: TFileTime;
  847. NbPixels: Integer;
  848. WParam: LongInt;
  849. begin
  850. ListBox := (FControl as TListBox);
  851. GetSystemTimeAsFileTime(KnowTime);
  852. NbPixels := Abs(ListBox.Font.Height);
  853. {Vertical scrolling, if viewstyle = vsReport:}
  854. if (ListBox.Items.Count > 0) and
  855. (((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  856. ((FVScrollCount > DDMaxSlowCount) and
  857. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay div 4)))) then
  858. begin
  859. if (Point.Y - 3 * nbPixels <= 0) and (ListBox.TopIndex > 0) then WParam := SB_LINEUP
  860. else
  861. if (Point.Y + 3 * nbPixels > ListBox.Height) then WParam := SB_LINEDOWN
  862. else WParam := -1;
  863. if WParam >= 0 then
  864. begin
  865. BeforeUpdate(nil);
  866. ListBox.Perform(WM_VSCROLL, WParam, 0);
  867. if FVScrollCount > DDMaxSlowCount then
  868. ListBox.Perform(WM_VSCROLL, WParam, 0);
  869. if FVScrollCount > DDMaxSlowCount * 3 then
  870. ListBox.Perform(WM_VSCROLL, WParam, 0);
  871. ListBox.Update;
  872. AfterUpdate;
  873. GetSystemTimeAsFileTime(FLastVScrollTime);
  874. Inc(FVScrollCount);
  875. end
  876. else FVScrollCount := 0;
  877. end;
  878. end;
  879. function IsUncPath(Path: string): Boolean;
  880. begin
  881. Result := (Copy(Path, 1, 2) = '\\') or (Copy(Path, 1, 2) = '//');
  882. end;
  883. const ERROR_CANT_ACCESS_FILE = 1920;
  884. function DoExists(R: Boolean; Path: string): Boolean;
  885. var
  886. Error: Integer;
  887. begin
  888. Result := R;
  889. if not Result then
  890. begin
  891. Error := GetLastError();
  892. if (Error = ERROR_CANT_ACCESS_FILE) or // returned when resolving symlinks in %LOCALAPPDATA%\Microsoft\WindowsApps
  893. (Error = ERROR_ACCESS_DENIED) then // returned for %USERPROFILE%\Application Data symlink
  894. begin
  895. Result := DirectoryExists(ApiPath(ExtractFileDir(Path)));
  896. end;
  897. end;
  898. end;
  899. function FileExistsFix(Path: string): Boolean;
  900. begin
  901. // WORKAROUND
  902. SetLastError(ERROR_SUCCESS);
  903. Result := DoExists(FileExists(ApiPath(Path)), Path);
  904. end;
  905. function DirectoryExistsFix(Path: string): Boolean;
  906. begin
  907. // WORKAROUND
  908. SetLastError(ERROR_SUCCESS);
  909. Result := DoExists(DirectoryExists(ApiPath(Path)), Path);
  910. end;
  911. // VCLCOPY
  912. function FindMatchingFileEx(var F: TSearchRec): Integer;
  913. var
  914. LocalFileTime: TFileTime;
  915. begin
  916. while F.FindData.dwFileAttributes and F.ExcludeAttr <> 0 do
  917. if not FindNextFile(F.FindHandle, F.FindData) then
  918. begin
  919. Result := GetLastError;
  920. Exit;
  921. end;
  922. FileTimeToLocalFileTime(F.FindData.ftLastWriteTime, LocalFileTime);
  923. {$WARN SYMBOL_DEPRECATED OFF}
  924. FileTimeToDosDateTime(LocalFileTime, LongRec(F.Time).Hi,
  925. LongRec(F.Time).Lo);
  926. {$WARN SYMBOL_DEPRECATED ON}
  927. F.Size := F.FindData.nFileSizeLow or Int64(F.FindData.nFileSizeHigh) shl 32;
  928. F.Attr := F.FindData.dwFileAttributes;
  929. F.Name := F.FindData.cFileName;
  930. Result := 0;
  931. end;
  932. var
  933. FindexAdvancedSupport: Boolean = False;
  934. // VCLCOPY (with FindFirstFile replaced by FindFirstFileEx)
  935. function FindFirstEx(
  936. const Path: string; Attr: Integer; var F: TSearchRec; AdditionalFlags: DWORD; SearchOp: _FINDEX_SEARCH_OPS): Integer;
  937. const
  938. faSpecial = faHidden or faSysFile or faDirectory;
  939. var
  940. FindexInfoLevel: TFindexInfoLevels;
  941. begin
  942. F.ExcludeAttr := not Attr and faSpecial;
  943. // FindExInfoBasic = do not retrieve cAlternateFileName, which we do not use
  944. if FindexAdvancedSupport then FindexInfoLevel := FindExInfoBasic
  945. else
  946. begin
  947. FindexInfoLevel := FindExInfoStandard;
  948. AdditionalFlags := AdditionalFlags and (not FIND_FIRST_EX_LARGE_FETCH_PAS);
  949. end;
  950. F.FindHandle := FindFirstFileEx(PChar(Path), FindexInfoLevel, @F.FindData, SearchOp, nil, AdditionalFlags);
  951. if F.FindHandle <> INVALID_HANDLE_VALUE then
  952. begin
  953. Result := FindMatchingFileEx(F);
  954. if Result <> 0 then FindClose(F);
  955. end
  956. else
  957. Result := GetLastError;
  958. end;
  959. type TPreferredAppMode = (pamDefault, pamAllowDark, pamForceDark, pamForceLight, pamMax);
  960. var
  961. AAllowDarkModeForWindow: function(hWnd: HWND; Allow: BOOL): BOOL; stdcall;
  962. ARefreshImmersiveColorPolicyState: procedure; stdcall;
  963. ASetPreferredAppMode: function(AppMode: TPreferredAppMode): TPreferredAppMode; stdcall;
  964. function SupportsDarkMode: Boolean;
  965. begin
  966. Result := Assigned(AAllowDarkModeForWindow) and Assigned(ARefreshImmersiveColorPolicyState);
  967. end;
  968. procedure AllowDarkModeForWindow(Control: TWinControl; Allow: Boolean);
  969. begin
  970. Assert(Control.HandleAllocated);
  971. if SupportsDarkMode and Control.HandleAllocated then
  972. begin
  973. AAllowDarkModeForWindow(Control.Handle, Allow);
  974. end;
  975. end;
  976. procedure AllowDarkModeForWindow(Handle: THandle; Allow: Boolean);
  977. begin
  978. if SupportsDarkMode then
  979. begin
  980. AAllowDarkModeForWindow(Handle, Allow);
  981. end;
  982. end;
  983. procedure RefreshColorMode;
  984. begin
  985. if SupportsDarkMode then
  986. begin
  987. ARefreshImmersiveColorPolicyState;
  988. end;
  989. end;
  990. var
  991. SysDarkTheme: Integer;
  992. procedure ResetSysDarkTheme;
  993. begin
  994. SysDarkTheme := -1;
  995. end;
  996. function DoGetSysDarkTheme(RootKey: HKEY): Integer;
  997. const
  998. ThemesPersonalizeKey = 'Software\Microsoft\Windows\CurrentVersion\Themes\Personalize';
  999. AppsUseLightThemeValue = 'AppsUseLightTheme';
  1000. var
  1001. Registry: TRegistry;
  1002. begin
  1003. Registry := TRegistry.Create;
  1004. try
  1005. Registry.RootKey := RootKey;
  1006. Result := -1;
  1007. if Registry.OpenKeyReadOnly(ThemesPersonalizeKey) and
  1008. Registry.ValueExists(AppsUseLightThemeValue) then
  1009. begin
  1010. if Registry.ReadBool(AppsUseLightThemeValue) then Result := 0
  1011. else Result := 1;
  1012. end;
  1013. finally
  1014. Registry.Free;
  1015. end;
  1016. end;
  1017. function GetSysDarkTheme: Boolean;
  1018. begin
  1019. if SysDarkTheme < 0 then
  1020. begin
  1021. SysDarkTheme := DoGetSysDarkTheme(HKEY_CURRENT_USER);
  1022. if SysDarkTheme < 0 then
  1023. begin
  1024. SysDarkTheme := DoGetSysDarkTheme(HKEY_LOCAL_MACHINE);
  1025. if SysDarkTheme < 0 then
  1026. begin
  1027. SysDarkTheme := 0;
  1028. end;
  1029. end;
  1030. end;
  1031. Result := (SysDarkTheme > 0);
  1032. end;
  1033. const
  1034. LOAD_LIBRARY_SEARCH_SYSTEM32 = $00000800;
  1035. LOAD_LIBRARY_SEARCH_USER_DIRS = $00000400;
  1036. var
  1037. Lib: THandle;
  1038. OSVersionInfo: TOSVersionInfoEx;
  1039. SetDefaultDllDirectories: function(DirectoryFlags: DWORD): BOOL; stdcall;
  1040. initialization
  1041. FindexAdvancedSupport := IsWin7;
  1042. // Translated from PuTTY's dll_hijacking_protection().
  1043. // Inno Setup does not use LOAD_LIBRARY_SEARCH_USER_DIRS and falls back to SetDllDirectory.
  1044. Lib := LoadLibrary(kernel32);
  1045. SetDefaultDllDirectories := GetProcAddress(Lib, 'SetDefaultDllDirectories');
  1046. if Assigned(SetDefaultDllDirectories) then
  1047. begin
  1048. SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32 or LOAD_LIBRARY_SEARCH_USER_DIRS);
  1049. end;
  1050. Lib := LoadLibrary('shcore');
  1051. if Lib <> 0 then
  1052. begin
  1053. GetDpiForMonitor := GetProcAddress(Lib, 'GetDpiForMonitor');
  1054. end;
  1055. Lib := LoadLibrary('user32');
  1056. if Lib <> 0 then
  1057. begin
  1058. GetSystemMetricsForDpi := GetProcAddress(Lib, 'GetSystemMetricsForDpi');
  1059. SystemParametersInfoForDpi := GetProcAddress(Lib, 'SystemParametersInfoForDpi');
  1060. end;
  1061. AAllowDarkModeForWindow := nil;
  1062. ARefreshImmersiveColorPolicyState := nil;
  1063. ASetPreferredAppMode := nil;
  1064. OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
  1065. if GetVersionEx(OSVersionInfo) and (OSVersionInfo.dwBuildNumber >= 17763) then
  1066. begin
  1067. Lib := GetModuleHandle('uxtheme');
  1068. if Lib <> 0 then
  1069. begin
  1070. AAllowDarkModeForWindow := GetProcAddress(Lib, MakeIntResource(133));
  1071. ARefreshImmersiveColorPolicyState := GetProcAddress(Lib, MakeIntResource(104));
  1072. if OSVersionInfo.dwBuildNumber >= 18334 then
  1073. begin
  1074. ASetPreferredAppMode := GetProcAddress(Lib, MakeIntResource(135));
  1075. end;
  1076. if SupportsDarkMode then
  1077. begin
  1078. // Both SetPreferredAppMode and RefreshImmersiveColorPolicyState is needed for
  1079. // dark list view headers and dark list view and tree view scrollbars
  1080. if Assigned(ASetPreferredAppMode) then
  1081. begin
  1082. ASetPreferredAppMode(pamAllowDark);
  1083. end;
  1084. ARefreshImmersiveColorPolicyState;
  1085. end;
  1086. end;
  1087. end;
  1088. ResetSysDarkTheme;
  1089. finalization
  1090. // No need to release individual image lists as they are owned by Application object.
  1091. FreeAndNil(ShellImageLists);
  1092. end.