Partial DPI awareness

This commit is contained in:
Maurice Heumann 2019-11-30 14:54:32 +01:00
parent 4778c5bda6
commit a47755f593
3 changed files with 23 additions and 0 deletions

View File

@ -34,6 +34,8 @@ void window::create(const std::string& title, const int width, const int height,
this->handle_ = CreateWindowExA(NULL, this->wc_.lpszClassName, title.data(), flags, x, y, width, height, nullptr, this->handle_ = CreateWindowExA(NULL, this->wc_.lpszClassName, title.data(), flags, x, y, width, height, nullptr,
nullptr, this->wc_.hInstance, this); nullptr, this->wc_.hInstance, this);
SendMessageA(this->handle_, WM_DPICHANGED, 0, 0);
} }
window::~window() window::~window()
@ -131,6 +133,24 @@ void window::set_callback(const std::function<LRESULT(window*, UINT, WPARAM, LPA
LRESULT window::processor(const UINT message, const WPARAM w_param, const LPARAM l_param) LRESULT window::processor(const UINT message, const WPARAM w_param, const LPARAM l_param)
{ {
if (message == WM_DPICHANGED)
{
const auto dpi = GetDpiForWindow(*this);
if (dpi != this->last_dpi_)
{
RECT rect;
GetWindowRect(*this, &rect);
const auto scale = dpi * 1.0 / this->last_dpi_;
this->last_dpi_ = dpi;
const auto width = rect.right - rect.left;
const auto height = rect.bottom - rect.top;
MoveWindow(*this, rect.left, rect.top, int(width * scale), int(height * scale), TRUE);
}
}
if (message == WM_DESTROY) if (message == WM_DESTROY)
{ {
remove_window(this); remove_window(this);

View File

@ -27,6 +27,8 @@ protected:
virtual LRESULT processor(UINT message, WPARAM w_param, LPARAM l_param); virtual LRESULT processor(UINT message, WPARAM w_param, LPARAM l_param);
private: private:
uint32_t last_dpi_ = 96;
WNDCLASSEX wc_{}; WNDCLASSEX wc_{};
HWND handle_ = nullptr; HWND handle_ = nullptr;
std::string classname_; std::string classname_;

View File

@ -81,6 +81,7 @@ FARPROC load_binary(const launcher::mode mode)
int main() int main()
{ {
FARPROC entry_point; FARPROC entry_point;
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
{ {
auto premature_shutdown = true; auto premature_shutdown = true;