PasTools.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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. function HasSystemParametersInfoForPixelsPerInch: Boolean;
  21. function SystemParametersInfoForPixelsPerInch(
  22. uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL;
  23. procedure GetFormScaleRatio(Form: TForm; var M, D: Integer);
  24. function GetMonitorFromControl(Control: TControl): TMonitor;
  25. function GetMonitorPixelsPerInch(Monitor: TMonitor): Integer;
  26. function GetControlPixelsPerInch(Control: TControl): Integer;
  27. function GetComponentPixelsPerInch(Component: TComponent): Integer;
  28. function LoadDimension(Dimension: Integer; PixelsPerInch: Integer; Control: TControl): Integer;
  29. function StrToDimensionDef(Str: string; PixelsPerInch: Integer; Control: TControl; Default: Integer): Integer;
  30. function SaveDimension(Dimension: Integer): Integer;
  31. function DimensionToDefaultPixelsPerInch(Dimension: Integer): Integer;
  32. function ScaleByPixelsPerInch(Dimension: Integer; Monitor: TMonitor): Integer; overload;
  33. function ScaleByPixelsPerInch(Dimension: Integer; Control: TControl): Integer; overload;
  34. function ScaleByPixelsPerInchFromSystem(Dimension: Integer; Control: TControl): Integer;
  35. function LoadPixelsPerInch(S: string; Control: TControl): Integer;
  36. function SavePixelsPerInch(Control: TControl): string;
  37. function SaveDefaultPixelsPerInch: string;
  38. function CalculateTextHeight(Canvas: TCanvas): Integer;
  39. function ScaleByTextHeight(Control: TControl; Dimension: Integer): Integer;
  40. function ScaleByTextHeightRunTime(Control: TControl; Dimension: Integer): Integer;
  41. function ScaleByControlTextHeightRunTime(Canvas: TCanvas; Dimension: Integer): Integer;
  42. function GetSystemMetricsForControl(Control: TControl; nIndex: Integer): Integer;
  43. type
  44. TImageListSize = (ilsSmall, ilsLarge);
  45. procedure NeedShellImageLists;
  46. function ShellImageListForSize(Width: Integer): TImageList;
  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.GetInternalTextHeight;
  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. // This should be replaced with IShellItemImageFactory, as already used for thumbnails
  425. procedure InitializeShellImageLists;
  426. type
  427. TSHGetImageList = function (iImageList: integer; const riid: TGUID; var ppv: Pointer): hResult; stdcall;
  428. const
  429. IID_IImageList: TGUID = '{46EB5926-582E-4017-9FDF-E8998DAA0950}';
  430. var
  431. Lib: THandle;
  432. ImageList: Integer;
  433. Handle: THandle;
  434. Height, Width: Integer;
  435. ShellImageList: TImageList;
  436. SHGetImageList: TSHGetImageList;
  437. HR: HRESULT;
  438. begin
  439. Lib := LoadLibrary('shell32');
  440. SHGetImageList := GetProcAddress(Lib, 'SHGetImageList');
  441. ShellImageLists := TDictionary<Integer, TImageList>.Create;
  442. for ImageList := 0 to SHIL_LAST do
  443. begin
  444. // VCL have declaration for SHGetImageList in ShellAPI, but it does not link
  445. HR := SHGetImageList(ImageList, IID_IImageList, Pointer(Handle));
  446. if (HR = S_OK) and
  447. ImageList_GetIconSize(Handle, Width, Height) then
  448. begin
  449. // We could use AddOrSetValue instead, but to be on a safe side, we prefer e.g. SHIL_SMALL over SHIL_SYSSMALL,
  450. // while they actually can be the same
  451. if not ShellImageLists.ContainsKey(Width) then
  452. begin
  453. ShellImageList := TImageList.Create(Application);
  454. ShellImageList.Handle := Handle;
  455. ShellImageList.ShareImages := True;
  456. ShellImageList.DrawingStyle := dsTransparent;
  457. ShellImageLists.Add(Width, ShellImageList);
  458. end;
  459. end;
  460. end;
  461. end;
  462. procedure NeedShellImageLists;
  463. begin
  464. if ShellImageLists = nil then
  465. begin
  466. InitializeShellImageLists;
  467. end;
  468. end;
  469. function ShellImageListForSize(Width: Integer): TImageList;
  470. var
  471. ImageListPair: TPair<Integer, TImageList>;
  472. ImageListWidth: Integer;
  473. Diff, BestDiff: Integer;
  474. begin
  475. // Delay load image lists, not to waste resources in console/scripting mode
  476. NeedShellImageLists;
  477. Result := nil;
  478. BestDiff := -1;
  479. for ImageListPair in ShellImageLists do
  480. begin
  481. ImageListWidth := ImageListPair.Key;
  482. if ImageListWidth <= Width then
  483. begin
  484. Diff := Width - ImageListWidth;
  485. end
  486. else
  487. begin
  488. // Prefer smaller images over larger, so for 150%, we use 100% images, not 200%
  489. // (a larger icon would make the item row higher)
  490. Diff := ImageListWidth - Width + 1;
  491. end;
  492. if (BestDiff < 0) or (BestDiff > Diff) then
  493. begin
  494. BestDiff := Diff;
  495. Result := ImageListPair.Value;
  496. end;
  497. end;
  498. end;
  499. function ShellImageListForControl(Control: TControl; Size: TImageListSize): TImageList;
  500. var
  501. Width: Integer;
  502. begin
  503. case Size of
  504. ilsSmall: Width := 16;
  505. ilsLarge: Width := 32;
  506. else Width := 0; Assert(False);
  507. end;
  508. Width := ScaleByPixelsPerInch(Width, Control);
  509. Result := ShellImageListForSize(Width);
  510. end;
  511. type
  512. TListViewHelper = class helper for TCustomListView
  513. public
  514. function HasMemStream: Boolean;
  515. end;
  516. function TListViewHelper.HasMemStream: Boolean;
  517. begin
  518. with Self do
  519. Result := Assigned(FMemStream);
  520. end;
  521. type
  522. TTreeViewHelper = class helper for TCustomTreeView
  523. public
  524. function HasMemStream: Boolean;
  525. end;
  526. function TTreeViewHelper.HasMemStream: Boolean;
  527. begin
  528. with Self do
  529. Result := Assigned(FMemStream);
  530. end;
  531. type
  532. TRichEditHelper = class helper for TCustomRichEdit
  533. public
  534. function HasMemStream: Boolean;
  535. end;
  536. function TRichEditHelper.HasMemStream: Boolean;
  537. begin
  538. with Self do
  539. Result := Assigned(FMemStream);
  540. end;
  541. function ControlHasRecreationPersistenceData(Control: TControl): Boolean;
  542. begin
  543. // not implemented for this class as we do not use it as of now
  544. Assert(not (Control is TCustomComboBoxEx));
  545. Result :=
  546. ((Control is TCustomListView) and (Control as TCustomListView).HasMemStream) or
  547. ((Control is TCustomTreeView) and (Control as TCustomTreeView).HasMemStream) or
  548. ((Control is TCustomRichEdit) and (Control as TCustomRichEdit).HasMemStream);
  549. end;
  550. type
  551. TApplicationHelper = class helper for TApplication
  552. public
  553. function IsAppIconic: Boolean;
  554. procedure SetAppIconic(Value: Boolean);
  555. procedure SetMainForm(Value: TForm);
  556. procedure SetTerminated(Value: Boolean);
  557. end;
  558. function TApplicationHelper.IsAppIconic: Boolean;
  559. begin
  560. with Self do
  561. Result := FAppIconic;
  562. end;
  563. procedure TApplicationHelper.SetAppIconic(Value: Boolean);
  564. begin
  565. with Self do
  566. FAppIconic := Value;
  567. end;
  568. procedure TApplicationHelper.SetMainForm(Value: TForm);
  569. begin
  570. with Self do
  571. FMainForm := Value;
  572. end;
  573. procedure TApplicationHelper.SetTerminated(Value: Boolean);
  574. begin
  575. with Self do
  576. FTerminate := Value;
  577. end;
  578. function IsAppIconic: Boolean;
  579. begin
  580. Result := Application.IsAppIconic;
  581. end;
  582. procedure SetAppIconic(Value: Boolean);
  583. begin
  584. Application.SetAppIconic(Value);
  585. end;
  586. procedure SetAppMainForm(Value: TForm);
  587. begin
  588. Application.SetMainForm(Value);
  589. end;
  590. procedure SetAppTerminated(Value: Boolean);
  591. begin
  592. Application.SetTerminated(Value);
  593. end;
  594. function ApiPath(Path: string): string;
  595. begin
  596. Result := Path;
  597. if Assigned(OnApiPath) then
  598. begin
  599. Result := OnApiPath(Result);
  600. end;
  601. end;
  602. procedure ForceColorChange(Control: TWinControl);
  603. begin
  604. // particularly when changing color back to default (clWindow),
  605. // non-client area (border line) is not redrawn,
  606. // keeping previous color. force redraw here
  607. if Control.HandleAllocated then
  608. begin
  609. RedrawWindow(Control.Handle, nil, 0, RDW_INVALIDATE or RDW_FRAME);
  610. end;
  611. end;
  612. procedure AppLog(S: string);
  613. begin
  614. if Assigned(OnAppLog) then
  615. begin
  616. OnAppLog(S);
  617. end;
  618. end;
  619. { TCustomControlScrollOnDragOver }
  620. constructor TCustomControlScrollOnDragOver.Create(Control: TControl;
  621. ScheduleDragOver: Boolean);
  622. begin
  623. FControl := Control;
  624. FOnBeforeUpdate := nil;
  625. FOnAfterUpdate := nil;
  626. if ScheduleDragOver then
  627. begin
  628. FDragOverTimer := TTimer.Create(Control);
  629. FDragOverTimer.Enabled := False;
  630. FDragOverTimer.Interval := 50;
  631. FDragOverTimer.OnTimer := DragOverTimer;
  632. end
  633. else FDragOverTimer := nil;
  634. end;
  635. destructor TCustomControlScrollOnDragOver.Destroy;
  636. begin
  637. FreeAndNil(FDragOverTimer);
  638. end;
  639. procedure TCustomControlScrollOnDragOver.DragOverTimer(Sender: TObject);
  640. var
  641. P: TPoint;
  642. begin
  643. P := FControl.ScreenToClient(Mouse.CursorPos);
  644. if (P.X >= 0) and (P.X < FControl.Width) and
  645. (P.Y >= 0) and (P.Y < FControl.Height) then
  646. begin
  647. DragOver(P);
  648. end;
  649. end;
  650. procedure TCustomControlScrollOnDragOver.StartDrag;
  651. begin
  652. GetSystemTimeAsFileTime(FDragOverTime);
  653. GetSystemTimeAsFileTime(FLastVScrollTime);
  654. FVScrollCount := 0;
  655. if Assigned(FDragOverTimer) then
  656. FDragOverTimer.Enabled := True;
  657. end;
  658. procedure TCustomControlScrollOnDragOver.EndDrag;
  659. begin
  660. if Assigned(FDragOverTimer) then
  661. FDragOverTimer.Enabled := False;
  662. end;
  663. type
  664. TPublicControl = class(TControl);
  665. procedure TCustomControlScrollOnDragOver.BeforeUpdate(ObjectToValidate: TObject);
  666. var
  667. DragImages: TDragImageList;
  668. begin
  669. if Assigned(FOnBeforeUpdate) then
  670. FOnBeforeUpdate(ObjectToValidate);
  671. DragImages := TPublicControl(FControl).GetDragImages;
  672. if Assigned(DragImages) then
  673. DragImages.HideDragImage;
  674. end;
  675. procedure TCustomControlScrollOnDragOver.AfterUpdate;
  676. var
  677. DragImages: TDragImageList;
  678. begin
  679. if Assigned(FOnAfterUpdate) then
  680. FOnAfterUpdate;
  681. DragImages := TPublicControl(FControl).GetDragImages;
  682. if Assigned(DragImages) then
  683. DragImages.ShowDragImage;
  684. end;
  685. procedure TTreeViewScrollOnDragOver.StartDrag;
  686. var
  687. KeyBoardState : TKeyBoardState;
  688. begin
  689. inherited;
  690. FLastDragNode := nil;
  691. if (GetKeyState(VK_SPACE) <> 0) and GetKeyboardState(KeyBoardState) then
  692. begin
  693. KeyBoardState[VK_SPACE] := 0;
  694. SetKeyBoardState(KeyBoardState);
  695. end;
  696. GetSystemTimeAsFileTime(FLastHScrollTime);
  697. end;
  698. { TTreeViewScrollOnDragOver }
  699. procedure TTreeViewScrollOnDragOver.DragOver(Point: TPoint);
  700. var
  701. TreeView: TCustomTreeView;
  702. NbPixels: Integer;
  703. KnowTime: TFileTime;
  704. Node: TTreeNode;
  705. TempTopItem: TTreeNode;
  706. ScrollInfo: TScrollInfo;
  707. KeyBoardState : TKeyBoardState;
  708. begin
  709. TreeView := (FControl as TCustomTreeView);
  710. Node := TreeView.GetNodeAt(Point.X, Point.Y);
  711. if Assigned(Node) then
  712. begin
  713. GetSystemTimeAsFileTime(KnowTime);
  714. if GetKeyState(VK_SPACE) = 0 then
  715. begin
  716. {Expand node after 2.5 seconds: }
  717. if not Assigned(FLastDragNode) or (FLastDragNode <> Node) then
  718. begin
  719. {not previous droptarget: start timer}
  720. GetSystemTimeAsFileTime(FDragOverTime);
  721. FLastDragNode := Node
  722. end
  723. else
  724. begin
  725. if ((Int64(KnowTime) - Int64(FDragOverTime)) > DDExpandDelay) then
  726. begin
  727. TempTopItem := TreeView.TopItem;
  728. BeforeUpdate(nil);
  729. Node.Expand(False);
  730. TreeView.TopItem := TempTopItem;
  731. TreeView.Update;
  732. AfterUpdate;
  733. FDragOverTime := KnowTime;
  734. end;
  735. end;
  736. end
  737. else
  738. begin
  739. {restart timer}
  740. GetSystemTimeAsFileTime(FDragOverTime);
  741. if GetKeyboardState(KeyBoardState) then
  742. begin
  743. KeyBoardState[VK_Space] := 0;
  744. SetKeyBoardState(KeyBoardState);
  745. end;
  746. TempTopItem := TreeView.TopItem;
  747. BeforeUpdate(Node);
  748. if Node.Expanded then
  749. begin
  750. if not TreeView.Selected.HasAsParent(Node) then
  751. Node.Collapse(False);
  752. end
  753. else Node.Expand(False);
  754. TreeView.TopItem := TempTopItem;
  755. TreeView.Update;
  756. AfterUpdate;
  757. end;
  758. NbPixels := Abs(TTreeView(FControl).Font.Height);
  759. {Vertical treescrolling:}
  760. if ((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  761. ((FVScrollCount > 3) and
  762. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay Div 4))) then
  763. begin
  764. {Scroll tree up, if droptarget is topitem:}
  765. if Node = TreeView.TopItem then
  766. begin
  767. BeforeUpdate(nil);
  768. TreeView.Perform(WM_VSCROLL, SB_LINEUP, 0);
  769. AfterUpdate;
  770. GetSystemTimeAsFileTime(FLastVScrollTime);
  771. Inc(FVScrollCount);
  772. end
  773. else
  774. {Scroll tree down, if next visible item of droptarget is not visible:}
  775. begin
  776. if Point.Y + 3 * nbPixels > TreeView.Height then
  777. begin
  778. BeforeUpdate(nil);
  779. TreeView.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
  780. AfterUpdate;
  781. GetSystemTimeAsFileTime(FLastVScrollTime);
  782. Inc(FVScrollCount);
  783. end
  784. else
  785. begin
  786. FVScrollCount := 0;
  787. end;
  788. end;
  789. end; {VScrollDelay}
  790. {Horizontal treescrolling:}
  791. {Scroll tree Left}
  792. if ((Int64(KnowTime) - Int64(FLastHScrollTime)) > DDHScrollDelay) then
  793. begin
  794. GetSystemTimeAsFileTime(FLastHScrollTime);
  795. ScrollInfo.cbSize := SizeOf(ScrollInfo);
  796. ScrollInfo.FMask := SIF_ALL;
  797. GetScrollInfo(TreeView.Handle, SB_HORZ, ScrollInfo);
  798. if ScrollInfo.nMin <> ScrollInfo.nMax then
  799. begin
  800. if Point.X < 50 then
  801. begin
  802. if Node.DisplayRect(True).Right + 50 < TreeView.Width then
  803. begin
  804. BeforeUpdate(nil);
  805. TreeView.Perform(WM_HSCROLL, SB_LINELEFT, 0);
  806. AfterUpdate;
  807. end;
  808. end
  809. else
  810. if Point.X > (TreeView.Width - 50) then
  811. begin
  812. if Node.DisplayRect(True).Left > 50 then
  813. begin
  814. BeforeUpdate(nil);
  815. TreeView.Perform(WM_HSCROLL, SB_LINERIGHT, 0);
  816. AfterUpdate;
  817. end;
  818. end;
  819. end;
  820. end;
  821. end;
  822. end;
  823. { TListViewScrollOnDragOver }
  824. procedure TListViewScrollOnDragOver.DragOver(Point: TPoint);
  825. var
  826. ListView: TCustomListView;
  827. KnowTime: TFileTime;
  828. NbPixels: Integer;
  829. WParam: LongInt;
  830. begin
  831. ListView := (FControl as TCustomListView);
  832. GetSystemTimeAsFileTime(KnowTime);
  833. NbPixels := Abs(TListView(ListView).Font.Height);
  834. {Vertical scrolling, if viewstyle = vsReport:}
  835. if (TListView(ListView).ViewStyle = vsReport) and Assigned(ListView.TopItem) and
  836. (((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  837. ((FVScrollCount > DDMaxSlowCount) and
  838. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay div 4)))) then
  839. begin
  840. if (Point.Y - 3 * nbPixels <= 0) and (ListView.TopItem.Index > 0) then WParam := SB_LINEUP
  841. else
  842. if (Point.Y + 3 * nbPixels > ListView.Height) then WParam := SB_LINEDOWN
  843. else WParam := -1;
  844. if WParam >= 0 then
  845. begin
  846. BeforeUpdate(nil);
  847. ListView.Perform(WM_VSCROLL, WParam, 0);
  848. if FVScrollCount > DDMaxSlowCount then
  849. ListView.Perform(WM_VSCROLL, WParam, 0);
  850. if FVScrollCount > DDMaxSlowCount * 3 then
  851. ListView.Perform(WM_VSCROLL, WParam, 0);
  852. ListView.Update;
  853. AfterUpdate;
  854. GetSystemTimeAsFileTime(FLastVScrollTime);
  855. Inc(FVScrollCount);
  856. end
  857. else FVScrollCount := 0;
  858. end;
  859. end;
  860. { TListBoxScrollOnDragOver }
  861. procedure TListBoxScrollOnDragOver.DragOver(Point: TPoint);
  862. var
  863. ListBox: TListBox;
  864. KnowTime: TFileTime;
  865. NbPixels: Integer;
  866. WParam: LongInt;
  867. begin
  868. ListBox := (FControl as TListBox);
  869. GetSystemTimeAsFileTime(KnowTime);
  870. NbPixels := Abs(ListBox.Font.Height);
  871. {Vertical scrolling, if viewstyle = vsReport:}
  872. if (ListBox.Items.Count > 0) and
  873. (((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  874. ((FVScrollCount > DDMaxSlowCount) and
  875. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay div 4)))) then
  876. begin
  877. if (Point.Y - 3 * nbPixels <= 0) and (ListBox.TopIndex > 0) then WParam := SB_LINEUP
  878. else
  879. if (Point.Y + 3 * nbPixels > ListBox.Height) then WParam := SB_LINEDOWN
  880. else WParam := -1;
  881. if WParam >= 0 then
  882. begin
  883. BeforeUpdate(nil);
  884. ListBox.Perform(WM_VSCROLL, WParam, 0);
  885. if FVScrollCount > DDMaxSlowCount then
  886. ListBox.Perform(WM_VSCROLL, WParam, 0);
  887. if FVScrollCount > DDMaxSlowCount * 3 then
  888. ListBox.Perform(WM_VSCROLL, WParam, 0);
  889. ListBox.Update;
  890. AfterUpdate;
  891. GetSystemTimeAsFileTime(FLastVScrollTime);
  892. Inc(FVScrollCount);
  893. end
  894. else FVScrollCount := 0;
  895. end;
  896. end;
  897. function IsUncPath(Path: string): Boolean;
  898. begin
  899. Result := (Copy(Path, 1, 2) = '\\') or (Copy(Path, 1, 2) = '//');
  900. end;
  901. const ERROR_CANT_ACCESS_FILE = 1920;
  902. function DoExists(R: Boolean; Path: string): Boolean;
  903. var
  904. Error: Integer;
  905. begin
  906. Result := R;
  907. if not Result then
  908. begin
  909. Error := GetLastError();
  910. if (Error = ERROR_CANT_ACCESS_FILE) or // returned when resolving symlinks in %LOCALAPPDATA%\Microsoft\WindowsApps
  911. (Error = ERROR_ACCESS_DENIED) then // returned for %USERPROFILE%\Application Data symlink
  912. begin
  913. Result := DirectoryExists(ApiPath(ExtractFileDir(Path)));
  914. end;
  915. end;
  916. end;
  917. function FileExistsFix(Path: string): Boolean;
  918. begin
  919. // WORKAROUND
  920. SetLastError(ERROR_SUCCESS);
  921. Result := DoExists(FileExists(ApiPath(Path)), Path);
  922. end;
  923. function DirectoryExistsFix(Path: string): Boolean;
  924. begin
  925. // WORKAROUND
  926. SetLastError(ERROR_SUCCESS);
  927. Result := DoExists(DirectoryExists(ApiPath(Path)), Path);
  928. end;
  929. // VCLCOPY
  930. function FindMatchingFileEx(var F: TSearchRec): Integer;
  931. var
  932. LocalFileTime: TFileTime;
  933. begin
  934. while F.FindData.dwFileAttributes and F.ExcludeAttr <> 0 do
  935. if not FindNextFile(F.FindHandle, F.FindData) then
  936. begin
  937. Result := GetLastError;
  938. Exit;
  939. end;
  940. FileTimeToLocalFileTime(F.FindData.ftLastWriteTime, LocalFileTime);
  941. {$WARN SYMBOL_DEPRECATED OFF}
  942. FileTimeToDosDateTime(LocalFileTime, LongRec(F.Time).Hi,
  943. LongRec(F.Time).Lo);
  944. {$WARN SYMBOL_DEPRECATED ON}
  945. F.Size := F.FindData.nFileSizeLow or Int64(F.FindData.nFileSizeHigh) shl 32;
  946. F.Attr := F.FindData.dwFileAttributes;
  947. F.Name := F.FindData.cFileName;
  948. Result := 0;
  949. end;
  950. var
  951. FindexAdvancedSupport: Boolean = False;
  952. // VCLCOPY (with FindFirstFile replaced by FindFirstFileEx)
  953. function FindFirstEx(
  954. const Path: string; Attr: Integer; var F: TSearchRec; AdditionalFlags: DWORD; SearchOp: _FINDEX_SEARCH_OPS): Integer;
  955. const
  956. faSpecial = faHidden or faSysFile or faDirectory;
  957. var
  958. FindexInfoLevel: TFindexInfoLevels;
  959. begin
  960. F.ExcludeAttr := not Attr and faSpecial;
  961. // FindExInfoBasic = do not retrieve cAlternateFileName, which we do not use
  962. if FindexAdvancedSupport then FindexInfoLevel := FindExInfoBasic
  963. else
  964. begin
  965. FindexInfoLevel := FindExInfoStandard;
  966. AdditionalFlags := AdditionalFlags and (not FIND_FIRST_EX_LARGE_FETCH_PAS);
  967. end;
  968. F.FindHandle := FindFirstFileEx(PChar(Path), FindexInfoLevel, @F.FindData, SearchOp, nil, AdditionalFlags);
  969. if F.FindHandle <> INVALID_HANDLE_VALUE then
  970. begin
  971. Result := FindMatchingFileEx(F);
  972. if Result <> 0 then FindClose(F);
  973. end
  974. else
  975. Result := GetLastError;
  976. end;
  977. type TPreferredAppMode = (pamDefault, pamAllowDark, pamForceDark, pamForceLight, pamMax);
  978. var
  979. AAllowDarkModeForWindow: function(hWnd: HWND; Allow: BOOL): BOOL; stdcall;
  980. ARefreshImmersiveColorPolicyState: procedure; stdcall;
  981. ASetPreferredAppMode: function(AppMode: TPreferredAppMode): TPreferredAppMode; stdcall;
  982. function SupportsDarkMode: Boolean;
  983. begin
  984. Result := Assigned(AAllowDarkModeForWindow) and Assigned(ARefreshImmersiveColorPolicyState);
  985. end;
  986. procedure AllowDarkModeForWindow(Control: TWinControl; Allow: Boolean);
  987. begin
  988. Assert(Control.HandleAllocated);
  989. if SupportsDarkMode and Control.HandleAllocated then
  990. begin
  991. AAllowDarkModeForWindow(Control.Handle, Allow);
  992. end;
  993. end;
  994. procedure AllowDarkModeForWindow(Handle: THandle; Allow: Boolean);
  995. begin
  996. if SupportsDarkMode then
  997. begin
  998. AAllowDarkModeForWindow(Handle, Allow);
  999. end;
  1000. end;
  1001. procedure RefreshColorMode;
  1002. begin
  1003. if SupportsDarkMode then
  1004. begin
  1005. ARefreshImmersiveColorPolicyState;
  1006. end;
  1007. end;
  1008. var
  1009. SysDarkTheme: Integer;
  1010. procedure ResetSysDarkTheme;
  1011. begin
  1012. SysDarkTheme := -1;
  1013. end;
  1014. function DoGetSysDarkTheme(RootKey: HKEY): Integer;
  1015. const
  1016. ThemesPersonalizeKey = 'Software\Microsoft\Windows\CurrentVersion\Themes\Personalize';
  1017. AppsUseLightThemeValue = 'AppsUseLightTheme';
  1018. var
  1019. Registry: TRegistry;
  1020. begin
  1021. Registry := TRegistry.Create;
  1022. try
  1023. Registry.RootKey := RootKey;
  1024. Result := -1;
  1025. if Registry.OpenKeyReadOnly(ThemesPersonalizeKey) and
  1026. Registry.ValueExists(AppsUseLightThemeValue) then
  1027. begin
  1028. if Registry.ReadBool(AppsUseLightThemeValue) then Result := 0
  1029. else Result := 1;
  1030. end;
  1031. finally
  1032. Registry.Free;
  1033. end;
  1034. end;
  1035. function GetSysDarkTheme: Boolean;
  1036. begin
  1037. if SysDarkTheme < 0 then
  1038. begin
  1039. SysDarkTheme := DoGetSysDarkTheme(HKEY_CURRENT_USER);
  1040. if SysDarkTheme < 0 then
  1041. begin
  1042. SysDarkTheme := DoGetSysDarkTheme(HKEY_LOCAL_MACHINE);
  1043. if SysDarkTheme < 0 then
  1044. begin
  1045. SysDarkTheme := 0;
  1046. end;
  1047. end;
  1048. end;
  1049. Result := (SysDarkTheme > 0);
  1050. end;
  1051. const
  1052. LOAD_LIBRARY_SEARCH_SYSTEM32 = $00000800;
  1053. LOAD_LIBRARY_SEARCH_USER_DIRS = $00000400;
  1054. var
  1055. Lib: THandle;
  1056. OSVersionInfo: TOSVersionInfoEx;
  1057. SetDefaultDllDirectories: function(DirectoryFlags: DWORD): BOOL; stdcall;
  1058. initialization
  1059. FindexAdvancedSupport := IsWin7;
  1060. // Translated from PuTTY's dll_hijacking_protection().
  1061. // Inno Setup does not use LOAD_LIBRARY_SEARCH_USER_DIRS and falls back to SetDllDirectory.
  1062. Lib := LoadLibrary(kernel32);
  1063. SetDefaultDllDirectories := GetProcAddress(Lib, 'SetDefaultDllDirectories');
  1064. if Assigned(SetDefaultDllDirectories) then
  1065. begin
  1066. SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32 or LOAD_LIBRARY_SEARCH_USER_DIRS);
  1067. end;
  1068. Lib := LoadLibrary('shcore');
  1069. if Lib <> 0 then
  1070. begin
  1071. GetDpiForMonitor := GetProcAddress(Lib, 'GetDpiForMonitor');
  1072. end;
  1073. Lib := LoadLibrary('user32');
  1074. if Lib <> 0 then
  1075. begin
  1076. GetSystemMetricsForDpi := GetProcAddress(Lib, 'GetSystemMetricsForDpi');
  1077. SystemParametersInfoForDpi := GetProcAddress(Lib, 'SystemParametersInfoForDpi');
  1078. end;
  1079. AAllowDarkModeForWindow := nil;
  1080. ARefreshImmersiveColorPolicyState := nil;
  1081. ASetPreferredAppMode := nil;
  1082. OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
  1083. if GetVersionEx(OSVersionInfo) and (OSVersionInfo.dwBuildNumber >= 17763) then
  1084. begin
  1085. Lib := GetModuleHandle('uxtheme');
  1086. if Lib <> 0 then
  1087. begin
  1088. AAllowDarkModeForWindow := GetProcAddress(Lib, MakeIntResource(133));
  1089. ARefreshImmersiveColorPolicyState := GetProcAddress(Lib, MakeIntResource(104));
  1090. if OSVersionInfo.dwBuildNumber >= 18334 then
  1091. begin
  1092. ASetPreferredAppMode := GetProcAddress(Lib, MakeIntResource(135));
  1093. end;
  1094. if SupportsDarkMode then
  1095. begin
  1096. // Both SetPreferredAppMode and RefreshImmersiveColorPolicyState is needed for
  1097. // dark list view headers and dark list view and tree view scrollbars
  1098. if Assigned(ASetPreferredAppMode) then
  1099. begin
  1100. ASetPreferredAppMode(pamAllowDark);
  1101. end;
  1102. ARefreshImmersiveColorPolicyState;
  1103. end;
  1104. end;
  1105. end;
  1106. ResetSysDarkTheme;
  1107. finalization
  1108. // No need to release individual image lists as they are owned by Application object.
  1109. FreeAndNil(ShellImageLists);
  1110. end.