// Copyright (c) 2014 CEF Python, see the Authors file.
// All rights reserved. Licensed under BSD 3-clause license.
// Project website: https://github.com/cztomczak/cefpython
// Windows only
#pragma comment(lib, "Gdi32.lib")
#include
#include "dpi_aware.h"
#include "include/wrapper/cef_closure_task.h"
#include "include/base/cef_bind.h"
#include "include/base/cef_callback.h"
#include "include/base/cef_logging.h"
const int DEFAULT_DPIX = 96;
bool IsProcessDpiAware() {
typedef BOOL(WINAPI *IsProcessDPIAwarePtr)(VOID);
IsProcessDPIAwarePtr is_process_dpi_aware_func =
reinterpret_cast(
GetProcAddress(GetModuleHandleA("user32.dll"),
"IsProcessDPIAware"));
BOOL is_aware = FALSE;
if (is_process_dpi_aware_func) {
is_aware = is_process_dpi_aware_func();
if (is_aware == TRUE) {
return true;
}
}
// GetProcessDpiAwareness is available only on Win8. So it
// should be called only as a back-up plan after IsProcessDPIAware
// was called. Also if IsProcessDPIAware returned false,
// then GetProcessDpiAwareness is called as well.
if (GetProcessDpiAwareness() != PROCESS_DPI_UNAWARE) {
return true;
}
return false;
}
PROCESS_DPI_AWARENESS GetProcessDpiAwareness() {
// Win8.1 supports monitor-specific DPI scaling, so it is
// recommended to use GetProcessDPIAwareness instead of the
// deprecated IsProcessDPIAware.
typedef HRESULT(WINAPI *GetProcessDpiAwarenessPtr)
(HANDLE,PROCESS_DPI_AWARENESS*);
GetProcessDpiAwarenessPtr get_process_dpi_awareness_func =
reinterpret_cast(
GetProcAddress(GetModuleHandleA("user32.dll"),
"GetProcessDpiAwarenessInternal"));
if (get_process_dpi_awareness_func) {
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false,
GetCurrentProcessId());
PROCESS_DPI_AWARENESS result;
HRESULT hr = get_process_dpi_awareness_func(hProcess, &result);
if (SUCCEEDED(hr)) {
return result;
}
// Possible failures include E_INVALIDARG or E_ACCESSDENIED.
LOG(INFO) Display):
// text size Larger 150% => dpix/dpiy 144
// text size Medium 125% => dpix/dpiy 120
// text size Smaller 100% => dpix/dpiy 96
//
// DPI settings should not be cached. When SetProcessDpiAware
// is not yet called, then OS returns 96 DPI, even though it
// is set to 144 DPI. After DPI Awareness is enabled for the
// running process it will return the correct 144 DPI.
HDC hdc = GetDC(HWND_DESKTOP);
*dpix = GetDeviceCaps(hdc, LOGPIXELSX);
*dpiy = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(HWND_DESKTOP, hdc);
}
void GetDpiAwareWindowSize(int* width, int* height) {
int dpix = 0;
int dpiy = 0;
GetSystemDpi(&dpix, &dpiy);
double newZoomLevel = 0.0;
// 1. Using only "dpix" value to calculate zoom level. All
// modern displays have equal horizontal and vertical resolution.
// 2. Calculation for DPI < 96 is not yet supported (newZoomLevel 0.0) {
*width = *width + (int)ceil(newZoomLevel * 0.25 * (*width));
*height = *height + (int)ceil(newZoomLevel * 0.25 * (*height));
LOG(INFO)