Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It seems to be an overkill to depend on GDI+ for just GetDeviceCaps #3137

Closed
andrey-martynyuk opened this issue Apr 19, 2020 · 1 comment
Closed
Labels

Comments

@andrey-martynyuk
Copy link

Version/Branch of Dear ImGui:

Dear ImGui 1.76 (17600)
--------------------------------
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1925
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx11

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_win32 + imgui_impl_dx11
Compiler: Microsoft Visual Studio 16.5.4
Operating System: Windows 10

My Issue/Question:

In version 1.75 ImGui didn't have a dependency on GDI+. So I happily used it with #define NOGDI defined in pch.h.

In version 1.76 ImGui_ImplWin32_GetDpiScaleForMonitor function was added. That brought dependency on GDI+ function GetDeviceCaps. Here is guilty code:

#ifdef _MSC_VER
#pragma comment(lib, "gdi32")   // Link with gdi32.lib for GetDeviceCaps()
#endif

float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor)
{
    UINT xdpi = 96, ydpi = 96;
    if (IsWindows8Point1OrGreater())
    {
        static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
        if (PFN_GetDpiForMonitor GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor"))
            GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
    }
    else
    {
        const HDC dc = ::GetDC(NULL);
        xdpi = ::GetDeviceCaps(dc, LOGPIXELSX);
        ydpi = ::GetDeviceCaps(dc, LOGPIXELSY);
        ::ReleaseDC(NULL, dc);
    }
    IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!
    return xdpi / 96.0f;
}

float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd)
{
    HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST);
    return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
}

It seems to be an overkill to depend on GDI+ for just one function, which is used only on systems older than Windows 8!

Possible solution is to have this dependency under #ifndef NOGDI:

#ifndef NOGDI
    else
    {
        const HDC dc = ::GetDC(NULL);
        xdpi = ::GetDeviceCaps(dc, LOGPIXELSX);
        ydpi = ::GetDeviceCaps(dc, LOGPIXELSY);
        ::ReleaseDC(NULL, dc);
    }
#endif

If the client is opted out GDI+ and has its application running on Windows 7 or older, it's safe to assume GetDeviceCaps returned 96, I guess.

Standalone, minimal, complete and verifiable example:

#define NOGDI
@ocornut
Copy link
Owner

ocornut commented Apr 19, 2020

Hello,
I have merged the suggested changed along with the pragma line.
Thank you!
Ref #2327

@ocornut ocornut closed this as completed Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants