Skip to content

Commit

Permalink
- dynamically import GetDpiForWindow from USER32.dll in case it does …
Browse files Browse the repository at this point in the history
…not exist, use try/catch to return a default value
  • Loading branch information
madame-rachelle committed Apr 21, 2024
1 parent 1fe6556 commit 3509977
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion libraries/ZWidget/src/window/win32/win32window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,51 @@ int Win32Window::GetPixelHeight() const
return box.bottom;
}

typedef UINT(WINAPI* GetDpiForWindow_t)(HWND);
double OSGetWindowDPIFactor(HWND winhandle)
{
if (winhandle == nullptr)
{
return 1.0;
}

HMODULE hMod = LoadLibrary(TEXT("User32.dll"));
if (hMod == nullptr)
{
throw std::runtime_error("Failed to load User32.dll");
}

GetDpiForWindow_t pGetDpiForWindow = reinterpret_cast<GetDpiForWindow_t>(GetProcAddress(hMod, "GetDpiForWindow"));
if (pGetDpiForWindow == nullptr)
{
FreeLibrary(hMod);
throw std::runtime_error("Failed to get GetDpiForWindow function");
}

UINT dpi = pGetDpiForWindow(winhandle);
if (dpi == 0)
{
FreeLibrary(hMod);
throw std::runtime_error("GetDpiForWindow failed");
}

FreeLibrary(hMod);
return static_cast<double>(dpi) / 96.0;
}

double Win32Window::GetDpiScale() const
{
return GetDpiForWindow(WindowHandle) / 96.0;
double retval;
try
{
retval = OSGetWindowDPIFactor(WindowHandle);
}
catch (std::runtime_error& e)
{
retval = 1.0;
//DPrintf(DMSG_NOTIFY, "Caught error: %s\n", e.what());
}
return retval;
}

std::string Win32Window::GetClipboardText()
Expand Down

0 comments on commit 3509977

Please sign in to comment.