Skip to content

Commit 991fcf2

Browse files
authored
Merge pull request Embarcadero#28 from Embarcadero/hidpi
Including high dpi support - Fixes Embarcadero/DelphiFMX4Python#12
2 parents 254e97b + 026bce4 commit 991fcf2

File tree

3 files changed

+124
-8
lines changed

3 files changed

+124
-8
lines changed

Source/WrapDelphiWindows.pas

Lines changed: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,40 @@
44

55
interface
66

7-
{$IFDEF Windows}
87
uses
98
Windows, Classes, SysUtils, PythonEngine, WrapDelphi, WrapDelphiClasses;
10-
{$ENDIF Windows}
9+
1110
implementation
1211

13-
{$IFDEF Windows}
12+
{$IFDEF DELPHI11_OR_HIGHER}
13+
uses
14+
System.Win.HighDpi, Winapi.ShellScaling;
15+
{$ENDIF DELPHI11_OR_HIGHER}
16+
1417
{ Register the wrappers, the globals and the constants }
1518
type
1619
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}
1727
public
1828
function Name : string; override;
1929
procedure RegisterWrappers(APyDelphiWrapper : TPyDelphiWrapper); override;
2030
procedure DefineVars(APyDelphiWrapper : TPyDelphiWrapper); override;
31+
procedure DefineFunctions(APyDelphiWrapper : TPyDelphiWrapper); override;
2132
end;
2233

2334
{ TWindowsRegistration }
2435

36+
function TWindowsRegistration.Name: string;
37+
begin
38+
Result := 'Windows';
39+
end;
40+
2541
procedure TWindowsRegistration.DefineVars(APyDelphiWrapper: TPyDelphiWrapper);
2642
begin
2743
inherited;
@@ -66,18 +82,116 @@ procedure TWindowsRegistration.DefineVars(APyDelphiWrapper: TPyDelphiWrapper);
6682
{$ENDIF FPC}
6783
end;
6884

69-
function TWindowsRegistration.Name: string;
85+
procedure TWindowsRegistration.DefineFunctions(
86+
APyDelphiWrapper: TPyDelphiWrapper);
7087
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}
72110
end;
73111

74112
procedure TWindowsRegistration.RegisterWrappers(APyDelphiWrapper: TPyDelphiWrapper);
75113
begin
76114
inherited;
77115
end;
78116

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+
79187
initialization
80188
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+
82196
end.
83197

Source/fmx/WrapDelphiFmx.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ implementation
77
uses
88
WrapDelphiTypes,
99
WrapDelphiClasses,
10-
{$IFDEF Windows}
10+
{$IFDEF MSWINDOWS}
1111
WrapDelphiWindows,
12-
{$ENDIF Windows}
12+
{$ENDIF MSWINDOWS}
1313
WrapDelphiDataBind,
1414
WrapFmxTypes,
1515
WrapFmxStdCtrls,

Source/vcl/WrapDelphiVCL.pas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ implementation
1212
uses
1313
WrapDelphiTypes,
1414
WrapDelphiClasses,
15+
{$IFDEF MSWINDOWS}
1516
WrapDelphiWindows,
17+
{$ENDIF MSWINDOWS}
1618
WrapDelphiDataBind,
1719
WrapActions,
1820
WrapVclControls,

0 commit comments

Comments
 (0)