PasTools.pas 36 KB

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