PasTools.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. unit PasTools;
  2. interface
  3. uses
  4. Windows, Types, Classes, ComCtrls, ExtCtrls, Controls, Dialogs, Forms, Messages;
  5. function Construct(ComponentClass: TComponentClass; Owner: TComponent): TComponent;
  6. function IsVistaHard: Boolean;
  7. function IsVista: Boolean;
  8. // Prevent name conflict with C++ IsWin8.
  9. {$HPPEMIT '#define IsWin7 IsWin7Pas'}
  10. {$HPPEMIT END '#undef IsWin7'}
  11. function IsWin7: Boolean;
  12. // Prevent name conflict with C++ IsWin8.
  13. {$HPPEMIT '#define IsWin8 IsWin8Pas'}
  14. {$HPPEMIT END '#undef IsWin8'}
  15. function IsWin8: Boolean;
  16. // Prevent name conflict with C++ CutToChar.
  17. {$HPPEMIT '#define CutToChar CutToCharPas'}
  18. {$HPPEMIT END '#undef CutToChar'}
  19. function CutToChar(var Str: string; Ch: Char; Trim: Boolean): string;
  20. procedure FilterToFileTypes(Filter: string; FileTypes: TFileTypeItems);
  21. // Note that while we based our scaling on pixels-per-inch,
  22. // VCL actually scales based on font size
  23. const
  24. CM_DPICHANGED = WM_USER + $2000 + 10;
  25. WM_DPICHANGED_BEFOREPARENT = $02E2;
  26. WM_DPICHANGED_AFTERPARENT = $02E3;
  27. function HasSystemParametersInfoForPixelsPerInch: Boolean;
  28. function SystemParametersInfoForPixelsPerInch(
  29. uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL;
  30. procedure GetFormScaleRatio(Form: TForm; var M, D: Integer);
  31. function GetMonitorFromControl(Control: TControl): TMonitor;
  32. function GetMonitorPixelsPerInch(Monitor: TMonitor): Integer;
  33. function GetControlPixelsPerInch(Control: TControl): Integer;
  34. function GetComponentPixelsPerInch(Component: TComponent): Integer;
  35. function LoadDimension(Dimension: Integer; PixelsPerInch: Integer; Control: TControl): Integer;
  36. function StrToDimensionDef(Str: string; PixelsPerInch: Integer; Control: TControl; Default: Integer): Integer;
  37. function SaveDimension(Dimension: Integer): Integer;
  38. function DimensionToDefaultPixelsPerInch(Dimension: Integer): Integer;
  39. function ScaleByPixelsPerInch(Dimension: Integer; Monitor: TMonitor): Integer; overload;
  40. function ScaleByPixelsPerInch(Dimension: Integer; Control: TControl): Integer; overload;
  41. function ScaleByPixelsPerInchFromSystem(Dimension: Integer; Control: TControl): Integer;
  42. function LoadPixelsPerInch(S: string; Control: TControl): Integer;
  43. function SavePixelsPerInch(Control: TControl): string;
  44. function SaveDefaultPixelsPerInch: string;
  45. function ScaleByTextHeight(Control: TControl; Dimension: Integer): Integer;
  46. function ScaleByTextHeightRunTime(Control: TControl; Dimension: Integer): Integer;
  47. function GetSystemMetricsForControl(Control: TControl; nIndex: Integer): Integer;
  48. type
  49. TImageListSize = (ilsSmall, ilsLarge);
  50. function ShellImageListForControl(Control: TControl; Size: TImageListSize): TImageList;
  51. function ControlHasRecreationPersistenceData(Control: TControl): Boolean;
  52. function IsAppIconic: Boolean;
  53. procedure SetAppIconic(Value: Boolean);
  54. procedure SetAppMainForm(Value: TForm);
  55. procedure ForceColorChange(Control: TWinControl);
  56. type
  57. TApiPathEvent = function(Path: string): string;
  58. var
  59. OnApiPath: TApiPathEvent = nil;
  60. // Prevent name conflict with C++ ApiPath.
  61. // We would not want to call this implementation in any case anyway.
  62. {$HPPEMIT '#define ApiPath ApiPathPas'}
  63. {$HPPEMIT END '#undef ApiPath'}
  64. function ApiPath(Path: string): string;
  65. type
  66. TControlScrollBeforeUpdate = procedure(ObjectToValidate: TObject) of object;
  67. TControlScrollAfterUpdate = procedure of object;
  68. TCustomControlScrollOnDragOver = class
  69. private
  70. FOnBeforeUpdate: TControlScrollBeforeUpdate;
  71. FOnAfterUpdate: TControlScrollAfterUpdate;
  72. FDragOverTimer: TTimer;
  73. FControl: TControl;
  74. FDragOverTime: FILETIME;
  75. FLastVScrollTime: FILETIME;
  76. FVScrollCount: Integer;
  77. procedure DragOverTimer(Sender: TObject);
  78. procedure BeforeUpdate(ObjectToValidate: TObject);
  79. procedure AfterUpdate;
  80. public
  81. constructor Create(Control: TControl; ScheduleDragOver: Boolean);
  82. destructor Destroy; override;
  83. procedure StartDrag; virtual;
  84. procedure EndDrag; virtual;
  85. procedure DragOver(Point: TPoint); virtual; abstract;
  86. property OnBeforeUpdate: TControlScrollBeforeUpdate read FOnBeforeUpdate write FOnBeforeUpdate;
  87. property OnAfterUpdate: TControlScrollAfterUpdate read FOnAfterUpdate write FOnAfterUpdate;
  88. end;
  89. TTreeViewScrollOnDragOver = class(TCustomControlScrollOnDragOver)
  90. private
  91. FLastDragNode: TTreeNode;
  92. FLastHScrollTime: FILETIME;
  93. public
  94. procedure StartDrag; override;
  95. procedure DragOver(Point: TPoint); override;
  96. end;
  97. TListViewScrollOnDragOver = class(TCustomControlScrollOnDragOver)
  98. public
  99. procedure DragOver(Point: TPoint); override;
  100. end;
  101. TListBoxScrollOnDragOver = class(TCustomControlScrollOnDragOver)
  102. public
  103. procedure DragOver(Point: TPoint); override;
  104. end;
  105. implementation
  106. uses
  107. SysUtils, StdCtrls, Graphics, MultiMon, ShellAPI, Generics.Collections, CommCtrl, ImgList;
  108. const
  109. DDExpandDelay = 15000000;
  110. DDMaxSlowCount = 3;
  111. DDVScrollDelay = 2000000;
  112. DDHScrollDelay = 2000000;
  113. DDDragStartDelay = 500000;
  114. function Construct(ComponentClass: TComponentClass; Owner: TComponent): TComponent;
  115. begin
  116. Result := ComponentClass.Create(Owner);
  117. end;
  118. // detects vista, even in compatibility mode
  119. // (GetLocaleInfoEx is available since Vista only)
  120. function IsVistaHard: Boolean;
  121. begin
  122. Result := (GetProcAddress(GetModuleHandle(Kernel32), 'GetLocaleInfoEx') <> nil);
  123. end;
  124. function IsVista: Boolean;
  125. begin
  126. Result := CheckWin32Version(6, 0);
  127. end;
  128. function IsWin7: Boolean;
  129. begin
  130. Result := CheckWin32Version(6, 1);
  131. end;
  132. function IsWin8: Boolean;
  133. begin
  134. Result := CheckWin32Version(6, 2);
  135. end;
  136. function CutToChar(var Str: string; Ch: Char; Trim: Boolean): string;
  137. var
  138. P: Integer;
  139. begin
  140. P := Pos(Ch, Str);
  141. if P > 0 then
  142. begin
  143. Result := Copy(Str, 1, P-1);
  144. Delete(Str, 1, P);
  145. end
  146. else
  147. begin
  148. Result := Str;
  149. Str := '';
  150. end;
  151. if Trim then Result := SysUtils.Trim(Result);
  152. end;
  153. procedure FilterToFileTypes(Filter: string; FileTypes: TFileTypeItems);
  154. var
  155. Item: TFileTypeItem;
  156. begin
  157. while Filter <> '' do
  158. begin
  159. Item := FileTypes.Add();
  160. Item.DisplayName := CutToChar(Filter, '|', True);
  161. Item.FileMask := CutToChar(Filter, '|', True);
  162. end;
  163. end;
  164. type
  165. TGetDpiForMonitorFunc =
  166. function (hMonitor: HMONITOR; MonitorType: Integer; out DpiX, DpiY: Cardinal): HRESULT; stdcall;
  167. TGetSystemMetricsForDpiFunc =
  168. function (nIndex: Integer; Dpi: Cardinal): Integer; stdcall;
  169. TSystemParametersInfoForDpiFunc =
  170. function (uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL; stdcall;
  171. const
  172. MDT_EFFECTIVE_DPI = 0;
  173. var
  174. GetDpiForMonitor: TGetDpiForMonitorFunc = nil;
  175. GetSystemMetricsForDpi: TGetSystemMetricsForDpiFunc = nil;
  176. SystemParametersInfoForDpi: TSystemParametersInfoForDpiFunc = nil;
  177. function HasSystemParametersInfoForPixelsPerInch: Boolean;
  178. begin
  179. Result := Assigned(SystemParametersInfoForDpi);
  180. end;
  181. function SystemParametersInfoForPixelsPerInch(
  182. uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL;
  183. begin
  184. if HasSystemParametersInfoForPixelsPerInch then
  185. begin
  186. Result := SystemParametersInfoForDpi(uiAction, uiParam, pvParam, fWinIni, dpi);
  187. end
  188. else
  189. begin
  190. Result := SystemParametersInfo(uiAction, uiParam, pvParam, fWinIni);
  191. end;
  192. end;
  193. function GetMonitorPixelsPerInch(Monitor: TMonitor): Integer;
  194. var
  195. DpiX, DpiY: Cardinal;
  196. begin
  197. if Assigned(GetDpiForMonitor) and
  198. (GetDpiForMonitor(Monitor.Handle, MDT_EFFECTIVE_DPI, DpiX, DpiY) = S_OK) then
  199. begin
  200. Result := DpiX;
  201. end
  202. else
  203. begin
  204. Result := Screen.PixelsPerInch;
  205. end;
  206. end;
  207. function GetMonitorFromControl(Control: TControl): TMonitor;
  208. begin
  209. if Control.Parent <> nil then
  210. begin
  211. Result := GetMonitorFromControl(Control.Parent);
  212. end
  213. else
  214. if Control is TCustomForm then
  215. begin
  216. Result := TCustomForm(Control).Monitor;
  217. end
  218. else
  219. if (Control is TWinControl) and TWinControl(Control).HandleAllocated then
  220. begin
  221. Result := Screen.MonitorFromWindow(TWinControl(Control).Handle);
  222. end
  223. else
  224. begin
  225. Result := nil;
  226. end;
  227. end;
  228. function GetControlPixelsPerInch(Control: TControl): Integer;
  229. var
  230. Form: TCustomForm;
  231. Monitor: TMonitor;
  232. begin
  233. if Assigned(GetDpiForMonitor) then // optimization
  234. begin
  235. Form := GetParentForm(Control);
  236. if Assigned(Form) then
  237. begin
  238. // By default, scale according to what the form is so far rendered on.
  239. // If the monitor perceived DPI does not match its monitor DPI, it's because the WM_DPICHANGED is still pending.
  240. Result := TForm(Form).PixelsPerInch;
  241. end
  242. else
  243. begin
  244. Monitor := GetMonitorFromControl(Control);
  245. if Monitor = nil then
  246. begin
  247. Assert(False);
  248. Monitor := Screen.PrimaryMonitor;
  249. end;
  250. Result := GetMonitorPixelsPerInch(Monitor);
  251. end;
  252. end
  253. else
  254. begin
  255. Result := Screen.PixelsPerInch;
  256. end;
  257. end;
  258. function GetComponentPixelsPerInch(Component: TComponent): Integer;
  259. begin
  260. Result := GetControlPixelsPerInch(TControl(Component.Owner));
  261. end;
  262. function LoadDimension(Dimension: Integer; PixelsPerInch: Integer; Control: TControl): Integer;
  263. begin
  264. Result := MulDiv(Dimension, GetControlPixelsPerInch(Control), PixelsPerInch);
  265. end;
  266. function StrToDimensionDef(Str: string; PixelsPerInch: Integer; Control: TControl; Default: Integer): Integer;
  267. begin
  268. if TryStrToInt(Str, Result) then
  269. begin
  270. Result := LoadDimension(Result, PixelsPerInch, Control);
  271. end
  272. else
  273. begin
  274. Result := Default;
  275. end;
  276. end;
  277. function SaveDimension(Dimension: Integer): Integer;
  278. begin
  279. // noop
  280. Result := Dimension;
  281. end;
  282. function DimensionToDefaultPixelsPerInch(Dimension: Integer): Integer;
  283. begin
  284. Result := MulDiv(Dimension, USER_DEFAULT_SCREEN_DPI, Screen.PixelsPerInch);
  285. end;
  286. function ScaleByPixelsPerInch(Dimension: Integer; Monitor: TMonitor): Integer;
  287. begin
  288. Result := MulDiv(Dimension, GetMonitorPixelsPerInch(Monitor), USER_DEFAULT_SCREEN_DPI);
  289. end;
  290. function ScaleByPixelsPerInch(Dimension: Integer; Control: TControl): Integer;
  291. begin
  292. Result := MulDiv(Dimension, GetControlPixelsPerInch(Control), USER_DEFAULT_SCREEN_DPI);
  293. end;
  294. function ScaleByPixelsPerInchFromSystem(Dimension: Integer; Control: TControl): Integer;
  295. begin
  296. Result := MulDiv(Dimension, GetControlPixelsPerInch(Control), Screen.PixelsPerInch);
  297. end;
  298. function LoadPixelsPerInch(S: string; Control: TControl): Integer;
  299. begin
  300. // for backward compatibility with version that did not save the DPI,
  301. // make reasonable assumption that the configuration was saved with
  302. // the same DPI as we run now
  303. Result := StrToIntDef(S, GetControlPixelsPerInch(Control));
  304. end;
  305. function SavePixelsPerInch(Control: TControl): string;
  306. begin
  307. Result := IntToStr(GetControlPixelsPerInch(Control));
  308. end;
  309. function SaveDefaultPixelsPerInch: string;
  310. begin
  311. Result := IntToStr(USER_DEFAULT_SCREEN_DPI);
  312. end;
  313. // WORKAROUND
  314. // https://stackoverflow.com/q/9410485/850848
  315. type
  316. TFormHelper = class helper for TCustomForm
  317. public
  318. function RetrieveTextHeight: Integer;
  319. function CalculateTextHeight: Integer;
  320. end;
  321. function TFormHelper.RetrieveTextHeight: Integer;
  322. begin
  323. Result := Self.FTextHeight;
  324. end;
  325. function TFormHelper.CalculateTextHeight: Integer;
  326. begin
  327. Result := Self.GetTextHeight;
  328. end;
  329. function ScaleByTextHeightImpl(Control: TControl; Dimension: Integer; TextHeight: Integer): Integer;
  330. var
  331. Form: TCustomForm;
  332. NewTextHeight: Integer;
  333. begin
  334. // RTL_COPY (TCustomForm.ReadState)
  335. Form := ValidParentForm(Control);
  336. NewTextHeight := Form.CalculateTextHeight;
  337. if TextHeight <> NewTextHeight then
  338. begin
  339. Dimension := MulDiv(Dimension, NewTextHeight, TextHeight);
  340. end;
  341. Result := Dimension;
  342. end;
  343. const
  344. OurDesignTimeTextHeight = 13;
  345. function ScaleByTextHeight(Control: TControl; Dimension: Integer): Integer;
  346. var
  347. Form: TCustomForm;
  348. TextHeight: Integer;
  349. begin
  350. // RTL_COPY (TCustomForm.ReadState)
  351. Form := GetParentForm(Control);
  352. if Form = nil then
  353. begin
  354. // This should happen only for screen tip over dropped down menu
  355. Assert(Control.ClassName = 'TTBXPopupWindow');
  356. Result := ScaleByPixelsPerInch(Dimension, Control);
  357. end
  358. else
  359. begin
  360. TextHeight := Form.RetrieveTextHeight;
  361. // runtime form (such as TTBFloatingWindowParent)
  362. if TextHeight = 0 then
  363. begin
  364. Result := ScaleByTextHeightRunTime(Control, Dimension);
  365. end
  366. else
  367. begin
  368. // that's our design text-size, we do not expect any other value
  369. Assert(TextHeight = OurDesignTimeTextHeight);
  370. Result := ScaleByTextHeightImpl(Control, Dimension, TextHeight);
  371. end;
  372. end;
  373. end;
  374. procedure GetFormScaleRatio(Form: TForm; var M, D: Integer);
  375. begin
  376. M := Form.CalculateTextHeight;
  377. D := Form.RetrieveTextHeight;
  378. end;
  379. // this differs from ScaleByTextHeight only by enforcing
  380. // constant design-time text height
  381. function ScaleByTextHeightRunTime(Control: TControl; Dimension: Integer): Integer;
  382. begin
  383. Result := ScaleByTextHeightImpl(Control, Dimension, OurDesignTimeTextHeight);
  384. end;
  385. function GetSystemMetricsForControl(Control: TControl; nIndex: Integer): Integer;
  386. begin
  387. if Assigned(GetSystemMetricsForDpi) then
  388. begin
  389. Result := GetSystemMetricsForDpi(nIndex, GetControlPixelsPerInch(Control))
  390. end
  391. else
  392. begin
  393. Result := GetSystemMetrics(nIndex);
  394. end;
  395. end;
  396. type
  397. TSHGetImageList = function (iImageList: integer; const riid: TGUID; var ppv: Pointer): hResult; stdcall;
  398. const
  399. IID_IImageList: TGUID = '{46EB5926-582E-4017-9FDF-E8998DAA0950}';
  400. var
  401. SHGetImageList: TSHGetImageList;
  402. ShellImageLists: TDictionary<Integer, TImageList>;
  403. procedure AddShellImageList(ImageList: Integer);
  404. var
  405. Handle: THandle;
  406. Height, Width: Integer;
  407. ShellImageList: TImageList;
  408. begin
  409. // VCL have declaration for SHGetImageList in ShellAPI, but it does not link
  410. if (SHGetImageList(ImageList, IID_IImageList, Pointer(Handle)) = S_OK) and
  411. ImageList_GetIconSize(Handle, Width, Height) then
  412. begin
  413. // We could use AddOrSetValue instead, but to be on a safe siz, we prefer e.g. SHIL_SMALL over SHIL_SYSSMALL,
  414. // while they actually can be the same
  415. if not ShellImageLists.ContainsKey(Width) then
  416. begin
  417. ShellImageList := TImageList.Create(Application);
  418. ShellImageList.Handle := Handle;
  419. ShellImageList.ShareImages := True;
  420. ShellImageList.DrawingStyle := dsTransparent;
  421. ShellImageLists.Add(Width, ShellImageList);
  422. end;
  423. end;
  424. end;
  425. function ShellImageListForControl(Control: TControl; Size: TImageListSize): TImageList;
  426. var
  427. ImageListPair: TPair<Integer, TImageList>;
  428. Width, ImageListWidth: Integer;
  429. Diff, BestDiff: Integer;
  430. begin
  431. case Size of
  432. ilsSmall: Width := 16;
  433. ilsLarge: Width := 32;
  434. else Width := 0; Assert(False);
  435. end;
  436. Width := ScaleByPixelsPerInch(Width, Control);
  437. Result := nil;
  438. BestDiff := -1;
  439. for ImageListPair in ShellImageLists do
  440. begin
  441. ImageListWidth := ImageListPair.Key;
  442. if ImageListWidth <= Width then
  443. begin
  444. Diff := Width - ImageListWidth;
  445. end
  446. else
  447. begin
  448. // Prefer smaller images over larger, so for 150%, we use 100% images, not 200%
  449. Diff := ImageListWidth - Width + 1;
  450. end;
  451. if (BestDiff < 0) or (BestDiff > Diff) then
  452. begin
  453. BestDiff := Diff;
  454. Result := ImageListPair.Value;
  455. end;
  456. end;
  457. end;
  458. type
  459. TListViewHelper = class helper for TCustomListView
  460. public
  461. function HasMemStream: Boolean;
  462. end;
  463. function TListViewHelper.HasMemStream: Boolean;
  464. begin
  465. Result := Assigned(Self.FMemStream);
  466. end;
  467. type
  468. TTreeViewHelper = class helper for TCustomTreeView
  469. public
  470. function HasMemStream: Boolean;
  471. end;
  472. function TTreeViewHelper.HasMemStream: Boolean;
  473. begin
  474. Result := Assigned(Self.FMemStream);
  475. end;
  476. type
  477. TRichEditHelper = class helper for TCustomRichEdit
  478. public
  479. function HasMemStream: Boolean;
  480. end;
  481. function TRichEditHelper.HasMemStream: Boolean;
  482. begin
  483. Result := Assigned(Self.FMemStream);
  484. end;
  485. function ControlHasRecreationPersistenceData(Control: TControl): Boolean;
  486. begin
  487. // not implemented for this class as we do not use it as of now
  488. Assert(not (Control is TCustomComboBoxEx));
  489. Result :=
  490. ((Control is TCustomListView) and (Control as TCustomListView).HasMemStream) or
  491. ((Control is TCustomTreeView) and (Control as TCustomTreeView).HasMemStream) or
  492. ((Control is TCustomRichEdit) and (Control as TCustomRichEdit).HasMemStream);
  493. end;
  494. type
  495. TApplicationHelper = class helper for TApplication
  496. public
  497. function IsAppIconic: Boolean;
  498. procedure SetAppIconic(Value: Boolean);
  499. procedure SetMainForm(Value: TForm);
  500. end;
  501. function TApplicationHelper.IsAppIconic: Boolean;
  502. begin
  503. Result := Self.FAppIconic;
  504. end;
  505. procedure TApplicationHelper.SetAppIconic(Value: Boolean);
  506. begin
  507. Self.FAppIconic := Value;
  508. end;
  509. procedure TApplicationHelper.SetMainForm(Value: TForm);
  510. begin
  511. Self.FMainForm := Value;
  512. end;
  513. function IsAppIconic: Boolean;
  514. begin
  515. Result := Application.IsAppIconic;
  516. end;
  517. procedure SetAppIconic(Value: Boolean);
  518. begin
  519. Application.SetAppIconic(Value);
  520. end;
  521. procedure SetAppMainForm(Value: TForm);
  522. begin
  523. Application.SetMainForm(Value);
  524. end;
  525. function ApiPath(Path: string): string;
  526. begin
  527. Result := Path;
  528. if Assigned(OnApiPath) then
  529. begin
  530. Result := OnApiPath(Result);
  531. end;
  532. end;
  533. procedure ForceColorChange(Control: TWinControl);
  534. begin
  535. // particularly when changing color back to default (clWindow),
  536. // non-client area (border line) is not redrawn,
  537. // keeping previous color. force redraw here
  538. if Control.HandleAllocated then
  539. begin
  540. RedrawWindow(Control.Handle, nil, 0, RDW_INVALIDATE or RDW_FRAME);
  541. end;
  542. end;
  543. { TCustomControlScrollOnDragOver }
  544. constructor TCustomControlScrollOnDragOver.Create(Control: TControl;
  545. ScheduleDragOver: Boolean);
  546. begin
  547. FControl := Control;
  548. FOnBeforeUpdate := nil;
  549. FOnAfterUpdate := nil;
  550. if ScheduleDragOver then
  551. begin
  552. FDragOverTimer := TTimer.Create(Control);
  553. FDragOverTimer.Enabled := False;
  554. FDragOverTimer.Interval := 50;
  555. FDragOverTimer.OnTimer := DragOverTimer;
  556. end
  557. else FDragOverTimer := nil;
  558. end;
  559. destructor TCustomControlScrollOnDragOver.Destroy;
  560. begin
  561. FreeAndNil(FDragOverTimer);
  562. end;
  563. procedure TCustomControlScrollOnDragOver.DragOverTimer(Sender: TObject);
  564. var
  565. P: TPoint;
  566. begin
  567. P := FControl.ScreenToClient(Mouse.CursorPos);
  568. if (P.X >= 0) and (P.X < FControl.Width) and
  569. (P.Y >= 0) and (P.Y < FControl.Height) then
  570. begin
  571. DragOver(P);
  572. end;
  573. end;
  574. procedure TCustomControlScrollOnDragOver.StartDrag;
  575. begin
  576. GetSystemTimeAsFileTime(FDragOverTime);
  577. GetSystemTimeAsFileTime(FLastVScrollTime);
  578. FVScrollCount := 0;
  579. if Assigned(FDragOverTimer) then
  580. FDragOverTimer.Enabled := True;
  581. end;
  582. procedure TCustomControlScrollOnDragOver.EndDrag;
  583. begin
  584. if Assigned(FDragOverTimer) then
  585. FDragOverTimer.Enabled := False;
  586. end;
  587. type
  588. TPublicControl = class(TControl);
  589. procedure TCustomControlScrollOnDragOver.BeforeUpdate(ObjectToValidate: TObject);
  590. var
  591. DragImages: TDragImageList;
  592. begin
  593. if Assigned(FOnBeforeUpdate) then
  594. FOnBeforeUpdate(ObjectToValidate);
  595. DragImages := TPublicControl(FControl).GetDragImages;
  596. if Assigned(DragImages) then
  597. DragImages.HideDragImage;
  598. end;
  599. procedure TCustomControlScrollOnDragOver.AfterUpdate;
  600. var
  601. DragImages: TDragImageList;
  602. begin
  603. if Assigned(FOnAfterUpdate) then
  604. FOnAfterUpdate;
  605. DragImages := TPublicControl(FControl).GetDragImages;
  606. if Assigned(DragImages) then
  607. DragImages.ShowDragImage;
  608. end;
  609. procedure TTreeViewScrollOnDragOver.StartDrag;
  610. var
  611. KeyBoardState : TKeyBoardState;
  612. begin
  613. inherited;
  614. FLastDragNode := nil;
  615. if (GetKeyState(VK_SPACE) <> 0) and GetKeyboardState(KeyBoardState) then
  616. begin
  617. KeyBoardState[VK_SPACE] := 0;
  618. SetKeyBoardState(KeyBoardState);
  619. end;
  620. GetSystemTimeAsFileTime(FLastHScrollTime);
  621. end;
  622. { TTreeViewScrollOnDragOver }
  623. procedure TTreeViewScrollOnDragOver.DragOver(Point: TPoint);
  624. var
  625. TreeView: TCustomTreeView;
  626. NbPixels: Integer;
  627. KnowTime: TFileTime;
  628. Node: TTreeNode;
  629. TempTopItem: TTreeNode;
  630. ScrollInfo: TScrollInfo;
  631. KeyBoardState : TKeyBoardState;
  632. begin
  633. TreeView := (FControl as TCustomTreeView);
  634. Node := TreeView.GetNodeAt(Point.X, Point.Y);
  635. if Assigned(Node) then
  636. begin
  637. GetSystemTimeAsFileTime(KnowTime);
  638. if GetKeyState(VK_SPACE) = 0 then
  639. begin
  640. {Expand node after 2.5 seconds: }
  641. if not Assigned(FLastDragNode) or (FLastDragNode <> Node) then
  642. begin
  643. {not previous droptarget: start timer}
  644. GetSystemTimeAsFileTime(FDragOverTime);
  645. FLastDragNode := Node
  646. end
  647. else
  648. begin
  649. if ((Int64(KnowTime) - Int64(FDragOverTime)) > DDExpandDelay) then
  650. begin
  651. TempTopItem := TreeView.TopItem;
  652. BeforeUpdate(nil);
  653. Node.Expand(False);
  654. TreeView.TopItem := TempTopItem;
  655. TreeView.Update;
  656. AfterUpdate;
  657. FDragOverTime := KnowTime;
  658. end;
  659. end;
  660. end
  661. else
  662. begin
  663. {restart timer}
  664. GetSystemTimeAsFileTime(FDragOverTime);
  665. if GetKeyboardState(KeyBoardState) then
  666. begin
  667. KeyBoardState[VK_Space] := 0;
  668. SetKeyBoardState(KeyBoardState);
  669. end;
  670. TempTopItem := TreeView.TopItem;
  671. BeforeUpdate(Node);
  672. if Node.Expanded then
  673. begin
  674. if not TreeView.Selected.HasAsParent(Node) then
  675. Node.Collapse(False);
  676. end
  677. else Node.Expand(False);
  678. TreeView.TopItem := TempTopItem;
  679. TreeView.Update;
  680. AfterUpdate;
  681. end;
  682. NbPixels := Abs(TTreeView(FControl).Font.Height);
  683. {Vertical treescrolling:}
  684. if ((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  685. ((FVScrollCount > 3) and
  686. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay Div 4))) then
  687. begin
  688. {Scroll tree up, if droptarget is topitem:}
  689. if Node = TreeView.TopItem then
  690. begin
  691. BeforeUpdate(nil);
  692. TreeView.Perform(WM_VSCROLL, SB_LINEUP, 0);
  693. AfterUpdate;
  694. GetSystemTimeAsFileTime(FLastVScrollTime);
  695. Inc(FVScrollCount);
  696. end
  697. else
  698. {Scroll tree down, if next visible item of droptarget is not visible:}
  699. begin
  700. if Point.Y + 3 * nbPixels > TreeView.Height then
  701. begin
  702. BeforeUpdate(nil);
  703. TreeView.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
  704. AfterUpdate;
  705. GetSystemTimeAsFileTime(FLastVScrollTime);
  706. Inc(FVScrollCount);
  707. end
  708. else
  709. begin
  710. FVScrollCount := 0;
  711. end;
  712. end;
  713. end; {VScrollDelay}
  714. {Horizontal treescrolling:}
  715. {Scroll tree Left}
  716. if ((Int64(KnowTime) - Int64(FLastHScrollTime)) > DDHScrollDelay) then
  717. begin
  718. GetSystemTimeAsFileTime(FLastHScrollTime);
  719. ScrollInfo.cbSize := SizeOf(ScrollInfo);
  720. ScrollInfo.FMask := SIF_ALL;
  721. GetScrollInfo(TreeView.Handle, SB_HORZ, ScrollInfo);
  722. if ScrollInfo.nMin <> ScrollInfo.nMax then
  723. begin
  724. if Point.X < 50 then
  725. begin
  726. if Node.DisplayRect(True).Right + 50 < TreeView.Width then
  727. begin
  728. BeforeUpdate(nil);
  729. TreeView.Perform(WM_HSCROLL, SB_LINELEFT, 0);
  730. AfterUpdate;
  731. end;
  732. end
  733. else
  734. if Point.X > (TreeView.Width - 50) then
  735. begin
  736. if Node.DisplayRect(True).Left > 50 then
  737. begin
  738. BeforeUpdate(nil);
  739. TreeView.Perform(WM_HSCROLL, SB_LINERIGHT, 0);
  740. AfterUpdate;
  741. end;
  742. end;
  743. end;
  744. end;
  745. end;
  746. end;
  747. { TListViewScrollOnDragOver }
  748. procedure TListViewScrollOnDragOver.DragOver(Point: TPoint);
  749. var
  750. ListView: TCustomListView;
  751. KnowTime: TFileTime;
  752. NbPixels: Integer;
  753. WParam: LongInt;
  754. begin
  755. ListView := (FControl as TCustomListView);
  756. GetSystemTimeAsFileTime(KnowTime);
  757. NbPixels := Abs(TListView(ListView).Font.Height);
  758. {Vertical scrolling, if viewstyle = vsReport:}
  759. if (TListView(ListView).ViewStyle = vsReport) and Assigned(ListView.TopItem) and
  760. (((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  761. ((FVScrollCount > DDMaxSlowCount) and
  762. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay div 4)))) then
  763. begin
  764. if (Point.Y - 3 * nbPixels <= 0) and (ListView.TopItem.Index > 0) then WParam := SB_LINEUP
  765. else
  766. if (Point.Y + 3 * nbPixels > ListView.Height) then WParam := SB_LINEDOWN
  767. else WParam := -1;
  768. if WParam >= 0 then
  769. begin
  770. BeforeUpdate(nil);
  771. ListView.Perform(WM_VSCROLL, WParam, 0);
  772. if FVScrollCount > DDMaxSlowCount then
  773. ListView.Perform(WM_VSCROLL, WParam, 0);
  774. if FVScrollCount > DDMaxSlowCount * 3 then
  775. ListView.Perform(WM_VSCROLL, WParam, 0);
  776. ListView.Update;
  777. AfterUpdate;
  778. GetSystemTimeAsFileTime(FLastVScrollTime);
  779. Inc(FVScrollCount);
  780. end
  781. else FVScrollCount := 0;
  782. end;
  783. end;
  784. { TListBoxScrollOnDragOver }
  785. procedure TListBoxScrollOnDragOver.DragOver(Point: TPoint);
  786. var
  787. ListBox: TListBox;
  788. KnowTime: TFileTime;
  789. NbPixels: Integer;
  790. WParam: LongInt;
  791. begin
  792. ListBox := (FControl as TListBox);
  793. GetSystemTimeAsFileTime(KnowTime);
  794. NbPixels := Abs(ListBox.Font.Height);
  795. {Vertical scrolling, if viewstyle = vsReport:}
  796. if (ListBox.Items.Count > 0) and
  797. (((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  798. ((FVScrollCount > DDMaxSlowCount) and
  799. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay div 4)))) then
  800. begin
  801. if (Point.Y - 3 * nbPixels <= 0) and (ListBox.TopIndex > 0) then WParam := SB_LINEUP
  802. else
  803. if (Point.Y + 3 * nbPixels > ListBox.Height) then WParam := SB_LINEDOWN
  804. else WParam := -1;
  805. if WParam >= 0 then
  806. begin
  807. BeforeUpdate(nil);
  808. ListBox.Perform(WM_VSCROLL, WParam, 0);
  809. if FVScrollCount > DDMaxSlowCount then
  810. ListBox.Perform(WM_VSCROLL, WParam, 0);
  811. if FVScrollCount > DDMaxSlowCount * 3 then
  812. ListBox.Perform(WM_VSCROLL, WParam, 0);
  813. ListBox.Update;
  814. AfterUpdate;
  815. GetSystemTimeAsFileTime(FLastVScrollTime);
  816. Inc(FVScrollCount);
  817. end
  818. else FVScrollCount := 0;
  819. end;
  820. end;
  821. var
  822. Lib: THandle;
  823. I: Integer;
  824. initialization
  825. Lib := LoadLibrary('shcore');
  826. if Lib <> 0 then
  827. begin
  828. GetDpiForMonitor := GetProcAddress(Lib, 'GetDpiForMonitor');
  829. end;
  830. Lib := LoadLibrary('user32');
  831. if Lib <> 0 then
  832. begin
  833. GetSystemMetricsForDpi := GetProcAddress(Lib, 'GetSystemMetricsForDpi');
  834. SystemParametersInfoForDpi := GetProcAddress(Lib, 'SystemParametersInfoForDpi');
  835. end;
  836. Lib := LoadLibrary('shell32');
  837. SHGetImageList := GetProcAddress(Lib, 'SHGetImageList');
  838. ShellImageLists := TDictionary<Integer, TImageList>.Create;
  839. for I := 0 to SHIL_LAST do
  840. begin
  841. AddShellImageList(I);
  842. end;
  843. finalization
  844. // No need to release individual image lists as they are owned by Application object.
  845. FreeAndNil(ShellImageLists);
  846. end.