|
4 | 4 |
|
5 | 5 | interface |
6 | 6 |
|
7 | | -{$IFDEF Windows} |
8 | 7 | uses |
9 | 8 | Windows, Classes, SysUtils, PythonEngine, WrapDelphi, WrapDelphiClasses; |
10 | | -{$ENDIF Windows} |
| 9 | + |
11 | 10 | implementation |
12 | 11 |
|
13 | | -{$IFDEF Windows} |
| 12 | +{$IFDEF DELPHI11_OR_HIGHER} |
| 13 | +uses |
| 14 | + System.Win.HighDpi, Winapi.ShellScaling; |
| 15 | +{$ENDIF DELPHI11_OR_HIGHER} |
| 16 | + |
14 | 17 | { Register the wrappers, the globals and the constants } |
15 | 18 | type |
16 | 19 | TWindowsRegistration = class(TRegisteredUnit) |
| 20 | + private |
| 21 | + {$IFDEF DELPHI11_OR_HIGHER} |
| 22 | + class function IsDpiAware_Wrapper(PySelf, AArgs: PPyObject): PPyObject; cdecl; static; |
| 23 | + class function SetHighDpiAware_Wrapper(PySelf, AArgs: PPyObject): PPyObject; cdecl; static; |
| 24 | + class function GetProcessDpiAwareness_Wrapper(PySelf, AArgs: PPyObject): PPyObject; cdecl; static; |
| 25 | + class function SetProcessDpiAwareness_Wrapper(PySelf, AArgs: PPyObject): PPyObject; cdecl; static; |
| 26 | + {$ENDIF DELPHI11_OR_HIGHER} |
17 | 27 | public |
18 | 28 | function Name : string; override; |
19 | 29 | procedure RegisterWrappers(APyDelphiWrapper : TPyDelphiWrapper); override; |
20 | 30 | procedure DefineVars(APyDelphiWrapper : TPyDelphiWrapper); override; |
| 31 | + procedure DefineFunctions(APyDelphiWrapper : TPyDelphiWrapper); override; |
21 | 32 | end; |
22 | 33 |
|
23 | 34 | { TWindowsRegistration } |
24 | 35 |
|
| 36 | +function TWindowsRegistration.Name: string; |
| 37 | +begin |
| 38 | + Result := 'Windows'; |
| 39 | +end; |
| 40 | + |
25 | 41 | procedure TWindowsRegistration.DefineVars(APyDelphiWrapper: TPyDelphiWrapper); |
26 | 42 | begin |
27 | 43 | inherited; |
@@ -66,18 +82,116 @@ procedure TWindowsRegistration.DefineVars(APyDelphiWrapper: TPyDelphiWrapper); |
66 | 82 | {$ENDIF FPC} |
67 | 83 | end; |
68 | 84 |
|
69 | | -function TWindowsRegistration.Name: string; |
| 85 | +procedure TWindowsRegistration.DefineFunctions( |
| 86 | + APyDelphiWrapper: TPyDelphiWrapper); |
70 | 87 | begin |
71 | | - Result := 'Windows'; |
| 88 | + inherited; |
| 89 | + {$IFDEF DELPHI11_OR_HIGHER} |
| 90 | + APyDelphiWrapper.RegisterFunction(PAnsiChar('IsDpiAware'), |
| 91 | + TWindowsRegistration.IsDpiAware_Wrapper, |
| 92 | + PAnsiChar('IsDPIAware()'#10 + |
| 93 | + 'Check for process DPI awareness.')); |
| 94 | + |
| 95 | + APyDelphiWrapper.RegisterFunction(PAnsiChar('SetHighDpiAware'), |
| 96 | + TWindowsRegistration.SetHighDpiAware_Wrapper, |
| 97 | + PAnsiChar('SetHighDpiAware()'#10 + |
| 98 | + 'Automatically set the DPI awareness that best fits to the process.')); |
| 99 | + |
| 100 | + APyDelphiWrapper.RegisterFunction(PAnsiChar('GetProcessDpiAwareness'), |
| 101 | + TWindowsRegistration.GetProcessDpiAwareness_Wrapper, |
| 102 | + PAnsiChar('GetProcessDpiAwareness()'#10 + |
| 103 | + 'Get the DPI awareness of the process.')); |
| 104 | + |
| 105 | + APyDelphiWrapper.RegisterFunction(PAnsiChar('SetProcessDpiAwareness'), |
| 106 | + TWindowsRegistration.SetProcessDpiAwareness_Wrapper, |
| 107 | + PAnsiChar('SetProcessDpiAwareness()'#10 + |
| 108 | + 'Set the DPI awareness to the process.')); |
| 109 | + {$ENDIF DELPHI11_OR_HIGHER} |
72 | 110 | end; |
73 | 111 |
|
74 | 112 | procedure TWindowsRegistration.RegisterWrappers(APyDelphiWrapper: TPyDelphiWrapper); |
75 | 113 | begin |
76 | 114 | inherited; |
77 | 115 | end; |
78 | 116 |
|
| 117 | +{$IFDEF DELPHI11_OR_HIGHER} |
| 118 | +class function TWindowsRegistration.IsDpiAware_Wrapper(PySelf, AArgs: PPyObject): PPyObject; |
| 119 | +begin |
| 120 | + with GetPythonEngine() do |
| 121 | + begin |
| 122 | + if IsDpiAware() then |
| 123 | + Result := GetPythonEngine().ReturnTrue() |
| 124 | + else |
| 125 | + Result := GetPythonEngine().ReturnFalse(); |
| 126 | + end; |
| 127 | +end; |
| 128 | + |
| 129 | +class function TWindowsRegistration.SetHighDpiAware_Wrapper(PySelf, AArgs: PPyObject): PPyObject; |
| 130 | +begin |
| 131 | + with GetPythonEngine() do |
| 132 | + begin |
| 133 | + if SetHighDpiAware() then |
| 134 | + Result := GetPythonEngine().ReturnTrue() |
| 135 | + else |
| 136 | + Result := GetPythonEngine().ReturnFalse(); |
| 137 | + end; |
| 138 | +end; |
| 139 | + |
| 140 | +class function TWindowsRegistration.GetProcessDpiAwareness_Wrapper(PySelf, |
| 141 | + AArgs: PPyObject): PPyObject; |
| 142 | +var |
| 143 | + LErrorCode: HResult; |
| 144 | + LDpiAwareness: TProcessDpiAwareness; |
| 145 | +begin |
| 146 | + with GetPythonEngine() do |
| 147 | + begin |
| 148 | + if (PyArg_ParseTuple(AArgs, ':GetProcessDpiAwareness') <> 0) then |
| 149 | + begin |
| 150 | + LErrorCode := WinAPI.ShellScaling.GetProcessDpiAwareness(GetCurrentProcess(), LDpiAwareness); |
| 151 | + |
| 152 | + Result := PyList_New(0); |
| 153 | + PyList_Append(Result, PyLong_FromLong(LErrorCode)); |
| 154 | + PyList_Append(Result, PyLong_FromLong(Ord(LDpiAwareness))); |
| 155 | + end else |
| 156 | + Result := nil; |
| 157 | + end; |
| 158 | +end; |
| 159 | + |
| 160 | +class function TWindowsRegistration.SetProcessDpiAwareness_Wrapper(PySelf, |
| 161 | + AArgs: PPyObject): PPyObject; |
| 162 | +var |
| 163 | + LErrorCode: HResult; |
| 164 | + LDpiAwareness: integer; |
| 165 | +begin |
| 166 | + with GetPythonEngine() do |
| 167 | + begin |
| 168 | + if (PyArg_ParseTuple(AArgs, 'i:SetProcessDpiAwareness', @LDpiAwareness) <> 0) then |
| 169 | + begin |
| 170 | + if not (LDpiAwareness in [ |
| 171 | + Ord(Low(TProcessDpiAwareness)) |
| 172 | + .. |
| 173 | + Ord(High(TProcessDpiAwareness))]) then |
| 174 | + begin |
| 175 | + PyErr_SetString(PyExc_ValueError^, 'DPI awareness value out of range'); |
| 176 | + Result := nil; |
| 177 | + end else begin |
| 178 | + LErrorCode := WinAPI.ShellScaling.SetProcessDpiAwareness(TProcessDpiAwareness(LDpiAwareness)); |
| 179 | + Result := PyLong_FromLong(LErrorCode); |
| 180 | + end; |
| 181 | + end else |
| 182 | + Result := nil; |
| 183 | + end; |
| 184 | +end; |
| 185 | +{$ENDIF DELPHI11_OR_HIGHER} |
| 186 | + |
79 | 187 | initialization |
80 | 188 | RegisteredUnits.Add( TWindowsRegistration.Create ); |
81 | | -{$ENDIF Windows} |
| 189 | + |
| 190 | + {$IFDEF MSWINDOWS} |
| 191 | + {$IFDEF DELPHI11_OR_HIGHER} |
| 192 | + SetHighDpiAware(); |
| 193 | + {$ENDIF DELPHI11_OR_HIGHER} |
| 194 | + {$ENDIF MSWINDOWS} |
| 195 | + |
82 | 196 | end. |
83 | 197 |
|
0 commit comments