Prevent window resize on DPI change

This commit is contained in:
Grayson Riffe 2025-02-02 13:12:27 -06:00
parent 91afd4ad1b
commit bd8eda13aa
3 changed files with 25 additions and 11 deletions

View File

@ -6,7 +6,7 @@
namespace nf::cli {
Client::Client(EngineConfig config)
: m_currentConfig(config)
, m_window()
, m_window(1280, 720)
{
// Setup window and renderer
}

View File

@ -5,10 +5,12 @@
#include "util.h"
namespace nf::cli {
Window::Window()
Window::Window(unsigned int width, unsigned int height)
: m_handle(nullptr)
, m_wndClassName("NothinFancyWindow")
, m_windowedStyle(WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
, m_width(width)
, m_height(height)
{
NFLog("Creating window");
@ -20,6 +22,7 @@ namespace nf::cli {
RegisterClass(&wc);
m_handle = CreateWindow(m_wndClassName, "NF Window", m_windowedStyle, 0, 0, 0, 0, nullptr, nullptr, nullptr, this);
setSize();
}
void Window::runLoop() {
@ -48,15 +51,15 @@ namespace nf::cli {
MONITORINFO mi = {};
mi.cbSize = sizeof(mi);
GetMonitorInfo(MonitorFromPoint(cursor, MONITOR_DEFAULTTONEAREST), &mi);
int x = mi.rcMonitor.left + 100, y = mi.rcMonitor.top + 100;
int monWidth = mi.rcMonitor.right - mi.rcMonitor.left, monHeight = mi.rcMonitor.bottom - mi.rcMonitor.top;
int x = mi.rcMonitor.left + (monWidth / 2) - (wnd->m_width / 2), y = mi.rcMonitor.top + (monHeight / 2) - (wnd->m_height / 2);
SetWindowPos(hWnd, nullptr, x , y , 0, 0, SWP_NOZORDER | SWP_NOSIZE);
return 0;
}
RECT cli = {};
cli.right = 1280;
cli.bottom = 720;
AdjustWindowRect(&cli, wnd->m_windowedStyle, FALSE);
unsigned int width = cli.right - cli.left, height = cli.bottom - cli.top;
SetWindowPos(hWnd, nullptr, x, y, width, height, SWP_NOZORDER);
case WM_DPICHANGED: {
// Prevents resize on DPI change
wnd->setSize();
return 0;
}
@ -72,4 +75,12 @@ namespace nf::cli {
return DefWindowProc(hWnd, msg, wParam, lParam);
}
void Window::setSize() {
RECT cli = {};
cli.right = m_width;
cli.bottom = m_height;
AdjustWindowRectExForDpi(&cli, m_windowedStyle, FALSE, NULL, GetDpiForWindow(m_handle));
unsigned int width = cli.right - cli.left, height = cli.bottom - cli.top;
SetWindowPos(m_handle, nullptr, 0, 0, width, height, SWP_NOZORDER | SWP_NOMOVE);
}
}

View File

@ -6,16 +6,19 @@
namespace nf::cli {
class Window {
public:
Window();
Window(unsigned int width, unsigned int height);
void runLoop();
void show(bool show = true);
private:
static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void setSize();
HWND m_handle;
const char* m_wndClassName;
const DWORD m_windowedStyle;
unsigned int m_width, m_height;
};
}