PasTools.pas 36 KB

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