PasTools.pas 36 KB

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