PasTools.pas 36 KB

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