Skip to content

Commit 8e1bade

Browse files
committed
[WIN] Fix High DPI support on text
Fix nwjs#2998, nwjs#3272
1 parent da7a71d commit 8e1bade

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

src/shell_main.cc

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "sandbox/win/src/sandbox_types.h"
2626

2727
#if defined(OS_WIN)
28+
#include <windows.h>
29+
#include <shellscalingapi.h>
30+
2831
#include "base/win/win_util.h"
2932
#include "base/win/windows_version.h"
3033
#include "content/public/app/startup_helper_win.h"
@@ -39,13 +42,49 @@ using base::CommandLine;
3942

4043
#if defined(OS_WIN)
4144

45+
// Win8.1 supports monitor-specific DPI scaling.
46+
bool SetProcessDpiAwarenessWrapper(PROCESS_DPI_AWARENESS value) {
47+
typedef HRESULT(WINAPI *SetProcessDpiAwarenessPtr)(PROCESS_DPI_AWARENESS);
48+
SetProcessDpiAwarenessPtr set_process_dpi_awareness_func =
49+
reinterpret_cast<SetProcessDpiAwarenessPtr>(
50+
GetProcAddress(GetModuleHandleA("user32.dll"),
51+
"SetProcessDpiAwarenessInternal"));
52+
if (set_process_dpi_awareness_func) {
53+
HRESULT hr = set_process_dpi_awareness_func(value);
54+
if (SUCCEEDED(hr)) {
55+
VLOG(1) << "SetProcessDpiAwareness succeeded.";
56+
return true;
57+
} else if (hr == E_ACCESSDENIED) {
58+
LOG(ERROR) << "Access denied error from SetProcessDpiAwareness. "
59+
"Function called twice, or manifest was used.";
60+
}
61+
}
62+
return false;
63+
}
64+
65+
// This function works for Windows Vista through Win8. Win8.1 must use
66+
// SetProcessDpiAwareness[Wrapper].
67+
BOOL SetProcessDPIAwareWrapper() {
68+
typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID);
69+
SetProcessDPIAwarePtr set_process_dpi_aware_func =
70+
reinterpret_cast<SetProcessDPIAwarePtr>(
71+
GetProcAddress(GetModuleHandleA("user32.dll"),
72+
"SetProcessDPIAware"));
73+
return set_process_dpi_aware_func &&
74+
set_process_dpi_aware_func();
75+
}
76+
77+
void EnableHighDPISupport() {
78+
if (!SetProcessDpiAwarenessWrapper(PROCESS_SYSTEM_DPI_AWARE)) {
79+
SetProcessDPIAwareWrapper();
80+
}
81+
}
82+
4283
int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) {
4384
CommandLine::Init(0, NULL);
44-
#if 0 //FIXME
45-
if (base::win::GetVersion() > base::win::VERSION_VISTA)
46-
gfx::EnableHighDPISupport();
47-
#endif
4885

86+
if (base::win::GetVersion() >= base::win::VERSION_WIN7)
87+
EnableHighDPISupport();
4988

5089
sandbox::SandboxInterfaceInfo sandbox_info = {0};
5190
content::InitializeSandboxInfo(&sandbox_info);

0 commit comments

Comments
 (0)