I created a test GUI and it seems to work properly.
Code: Select all
'Coded by UEZ build 2025-03-27
#cmdline "'App.rc'"
#include "_WinAPI_DpiAwareness.bi"
Declare Function RtlGetVersion Lib "NtDll.dll" Alias "RtlGetVersion" (OsVersionInformation As RTL_OSVERSIONINFOW) As Long
Dim Shared As Single fDPI
Function WindowProc(ByVal hWnd As HWND, ByVal uMsg As UINT, ByVal wParam As WPARAM, ByVal lParam As LPARAM) As LRESULT
Select Case uMsg
Case WM_PAINT
Dim ps As PAINTSTRUCT
Dim hdc As HDC = BeginPaint(hWnd, @ps)
Dim hFont As HFONT = CreateFont(36 * fDPI, 0, 0, 0, FW_BOLD, False, False, False, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH Or FF_DONTCARE, "Arial")
Dim hOldFont As HFONT = Cast(hFont, SelectObject(hdc, hFont))
Dim As String sTxt = "Hello, DPI-aware demo! DPI: " & fDPI
TextOut(hdc, 50, 50, sTxt , Len(sTxt))
EndPaint(hWnd, @ps)
Return 0
Case WM_DPICHANGED
Dim As RECT Ptr tPOS = Cast(RECT Ptr, lParam)
Dim As RECT tWin
GetWindowRect(hWnd, @tWin)
SetWindowPos(hWnd, HWND_NOTOPMOST, tWin.left, tWin.top, tPOS->right - tPOS->left, tPOS->bottom - tPOS->top, SWP_SHOWWINDOW)
fDPI = LoWord(wParam) / 96
Return 0
Case WM_DESTROY
PostQuitMessage(0)
Return 0
End Select
Return DefWindowProc(hWnd, uMsg, wParam, lParam)
End Function
Sub RunApp()
Dim wc As WNDCLASSEX
Dim hWnd As HWND
Dim msg As MSG
wc.cbSize = SizeOf(WNDCLASSEX)
wc.style = CS_HREDRAW Or CS_VREDRAW
wc.lpfnWndProc = @WindowProc
wc.hInstance = GetModuleHandle(NULL)
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION)
wc.hCursor = LoadCursor(NULL, IDC_ARROW)
wc.hbrBackground = Cast(HBRUSH, COLOR_WINDOW + 1)
wc.lpszMenuName = NULL
wc.lpszClassName = Cast(LPTSTR, StrPtr("MyWindowClass"))
If RegisterClassEx(@wc) = 0 Then
MessageBox(NULL, "Window Registration Failed!", "Error", MB_OK Or MB_ICONERROR)
Exit Sub
End If
hWnd = CreateWindowEx(0, wc.lpszClassName, "DPI Aware GDI Example", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 300, NULL, NULL, GetModuleHandle(NULL), NULL)
If hWnd = NULL Then
MessageBox(NULL, "Window Creation Failed!", "Error", MB_OK Or MB_ICONERROR)
Exit Sub
End If
ShowWindow(hWnd, SW_SHOWNORMAL)
UpdateWindow(hWnd)
While GetMessage(@msg, NULL, 0, 0)
TranslateMessage(@msg)
DispatchMessage(@msg)
Wend
End Sub
Function MonitorEnumProc(hMonitor As HMONITOR, hDC As HDC, r As RECT, d As LPARAM) As BOOL
Dim As tDPI dpiM
_WinAPI_GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, dpiM)
Dim As MONITORINFOEX tMI
tMI.cbSize = SizeOf(MONITORINFOEX)
GetMonitorInfo(hMonitor, Cast(LPMONITORINFO, @tMI))
Static As DISPLAY_DEVICE tDD
tDD.cb = SizeOf(DISPLAY_DEVICE)
EnumDisplayDevices(tMI.szDevice, 0, @tDD, 0) 'https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaydevicesw
? dpiM.dpiX, dpiM.dpiX / 96, tMI.szDevice, tDD.DeviceString, IIf(tMI.dwFlags = MONITORINFOF_PRIMARY, True, False)
Return True
End Function
Dim As RTL_OSVERSIONINFOW OS
OS.dwOSVersionInfoSize = SizeOf(RTL_OSVERSIONINFOW)
RtlGetVersion(OS)
If OS.dwBuildNumber < 14393 Then
End MessageBox(0, "Most of the WINAPI functions require OS build number > 14393 aka Win10 1607+ ", "ERROR", MB_OK Or MB_ICONSTOP Or MB_TOPMOST)
End If
? "Monitor information"
EnumDisplayMonitors(0, 0, Cast(MonitorEnumProc, @MonitorEnumProc), 0) 'https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaymonitors
fDPI = _WinAPI_GetDpiForSystem() / 96
Dim As BOOL iDPI = _WinAPI_SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)
RunApp()