Skip to content

Instantly share code, notes, and snippets.

@lniwn
Created October 13, 2017 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lniwn/23920a171b4af328c6429b0a3d52f6a0 to your computer and use it in GitHub Desktop.
Save lniwn/23920a171b4af328c6429b0a3d52f6a0 to your computer and use it in GitHub Desktop.
Windows应用支持高分屏
void EnableHighDPISupport() {
// Enable per-monitor DPI for Win10 or above instead of Win8.1 since Win8.1
// does not have EnableChildWindowDpiMessage, necessary for correct non-client
// area scaling across monitors.
PROCESS_DPI_AWARENESS process_dpi_awareness =
GetVersion() >= VERSION_WIN10 ? PROCESS_PER_MONITOR_DPI_AWARE
: PROCESS_SYSTEM_DPI_AWARE;
if (!SetProcessDpiAwarenessWrapper(process_dpi_awareness)) {
// For windows versions where SetProcessDpiAwareness is not available or
// failed, try its predecessor.
::SetProcessDPIAware();
}
}
// Method used for Windows 8.1 and later.
// Since we support versions earlier than 8.1, we must dynamically load this
// function from user32.dll, so it won't fail to load in runtime. For earlier
// Windows versions GetProcAddress will return null and report failure so that
// callers can fall back on the deprecated SetProcessDPIAware.
bool SetProcessDpiAwarenessWrapper(PROCESS_DPI_AWARENESS value) {
decltype(&::SetProcessDpiAwareness) set_process_dpi_awareness_func =
reinterpret_cast<decltype(&::SetProcessDpiAwareness)>(GetProcAddress(
GetModuleHandle(L"user32.dll"), "SetProcessDpiAwarenessInternal"));
if (set_process_dpi_awareness_func) {
HRESULT hr = set_process_dpi_awareness_func(value);
if (SUCCEEDED(hr))
return true;
DLOG_IF(ERROR, hr == E_ACCESSDENIED)
<< "Access denied error from SetProcessDpiAwareness. Function called "
"twice, or manifest was used.";
}
// TODO(pbos): Consider DCHECKing / NOTREACHED here if the platform version is
// >= 8.1. That way we can detect future name changes instead of having to
// discover silent failures.
return false;
}
// https://chromium.googlesource.com/chromium/src/+/7818e07434df5da87d6cdfe0d30830bf2191097e/chrome/app/chrome_exe_main_win.cc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment