Path: blob/master/src/java.desktop/windows/native/common/awt/systemscale/systemScale.cpp
41171 views
/*1* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24#include "systemScale.h"25#include <jdk_util.h>26#ifndef MDT_EFFECTIVE_DPI27#define MDT_EFFECTIVE_DPI 028#endif2930void GetScreenDpi(HMONITOR hmon, float *dpiX, float *dpiY)31{32unsigned x = 0;33unsigned y = 0;3435// for debug purposes36static float scale = -2.0f;37if (scale == -2) {38scale = -1;39char *uiScale = getenv("J2D_UISCALE");40if (uiScale != NULL) {41scale = (float)strtod(uiScale, NULL);42if (errno == ERANGE || scale <= 0) {43scale = -1;44}45}46}4748if (scale > 0) {49*dpiX = *dpiY = scale;50return;51}5253typedef HRESULT(WINAPI GetDpiForMonitorFunc)(HMONITOR, int, UINT*, UINT*);54static HMODULE hLibSHCoreDll = NULL;55static GetDpiForMonitorFunc *lpGetDpiForMonitor = NULL;5657if (hLibSHCoreDll == NULL) {58hLibSHCoreDll = JDK_LoadSystemLibrary("shcore.dll");59if (hLibSHCoreDll != NULL) {60lpGetDpiForMonitor = (GetDpiForMonitorFunc*)GetProcAddress(61hLibSHCoreDll, "GetDpiForMonitor");62}63}6465if (lpGetDpiForMonitor != NULL) {66HRESULT hResult = lpGetDpiForMonitor(hmon,67MDT_EFFECTIVE_DPI, &x, &y);68if (hResult == S_OK) {69*dpiX = static_cast<float>(x);70*dpiY = static_cast<float>(y);71}72} else {73HDC hdc = GetDC(NULL);74if (hdc) {75*dpiX = static_cast<float>(GetDeviceCaps(hdc, LOGPIXELSX));76*dpiY = static_cast<float>(GetDeviceCaps(hdc, LOGPIXELSY));77ReleaseDC(NULL, hdc);78}79}80return;81}8283HMONITOR WINAPI getPrimaryMonitor()84{85const POINT point = { 0, 0 };86return MonitorFromPoint(point, MONITOR_DEFAULTTOPRIMARY);87}888990