mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-06-11 12:14:53 +08:00
fancyzones: restrict dpi unaware windows horizontally to the current display to avoid unwanted resize
This commit is contained in:
parent
0fd0a8b7cc
commit
48b89609e2
@ -56,7 +56,7 @@ namespace DPIAware
|
|||||||
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
||||||
}
|
}
|
||||||
|
|
||||||
AWARENESS_LEVEL GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value)
|
AwarnessLevel GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value)
|
||||||
{
|
{
|
||||||
const std::array levels{ DPI_AWARENESS_CONTEXT_UNAWARE,
|
const std::array levels{ DPI_AWARENESS_CONTEXT_UNAWARE,
|
||||||
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE,
|
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE,
|
||||||
@ -67,9 +67,9 @@ namespace DPIAware
|
|||||||
{
|
{
|
||||||
if (AreDpiAwarenessContextsEqual(levels[i], system_returned_value))
|
if (AreDpiAwarenessContextsEqual(levels[i], system_returned_value))
|
||||||
{
|
{
|
||||||
return static_cast<AWARENESS_LEVEL>(i);
|
return static_cast<AwarnessLevel>(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return AWARENESS_LEVEL::UNAWARE;
|
return AwarnessLevel::UNAWARE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ namespace DPIAware
|
|||||||
void Convert(HMONITOR monitor_handle, int& width, int& height);
|
void Convert(HMONITOR monitor_handle, int& width, int& height);
|
||||||
void EnableDPIAwarenessForThisProcess();
|
void EnableDPIAwarenessForThisProcess();
|
||||||
|
|
||||||
enum AWARENESS_LEVEL
|
enum AwarnessLevel
|
||||||
{
|
{
|
||||||
UNAWARE,
|
UNAWARE,
|
||||||
SYSTEM_AWARE,
|
SYSTEM_AWARE,
|
||||||
@ -18,5 +18,5 @@ namespace DPIAware
|
|||||||
PER_MONITOR_AWARE_V2,
|
PER_MONITOR_AWARE_V2,
|
||||||
UNAWARE_GDISCALED
|
UNAWARE_GDISCALED
|
||||||
};
|
};
|
||||||
AWARENESS_LEVEL GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value);
|
AwarnessLevel GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value);
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "pch.h"
|
#include "pch.h"
|
||||||
|
|
||||||
|
#include <common/dpi_aware.h>
|
||||||
|
#include <common/monitors.h>
|
||||||
#include "Zone.h"
|
#include "Zone.h"
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
|
||||||
@ -66,12 +68,17 @@ void Zone::SizeWindowToZone(HWND window, HWND zoneWindow) noexcept
|
|||||||
::GetWindowRect(window, &windowRect);
|
::GetWindowRect(window, &windowRect);
|
||||||
|
|
||||||
RECT frameRect{};
|
RECT frameRect{};
|
||||||
// Failure is expected on down level systems.
|
|
||||||
|
const auto level = DPIAware::GetAwarenessLevel(GetWindowDpiAwarenessContext(window));
|
||||||
|
const bool accountForUnawareness = level < DPIAware::PER_MONITOR_AWARE;
|
||||||
if (SUCCEEDED(DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS, &frameRect, sizeof(frameRect))))
|
if (SUCCEEDED(DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS, &frameRect, sizeof(frameRect))))
|
||||||
{
|
{
|
||||||
zoneRect.bottom -= (frameRect.bottom - windowRect.bottom);
|
const auto left_margin = frameRect.left - windowRect.left;
|
||||||
zoneRect.right -= (frameRect.right - windowRect.right);
|
const auto right_margin = frameRect.right - windowRect.right;
|
||||||
zoneRect.left -= (frameRect.left - windowRect.left);
|
const auto bottom_margin = frameRect.bottom - windowRect.bottom;
|
||||||
|
zoneRect.left -= left_margin;
|
||||||
|
zoneRect.right -= right_margin;
|
||||||
|
zoneRect.bottom -= bottom_margin;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map to screen coords
|
// Map to screen coords
|
||||||
@ -80,7 +87,16 @@ void Zone::SizeWindowToZone(HWND window, HWND zoneWindow) noexcept
|
|||||||
MONITORINFO mi{sizeof(mi)};
|
MONITORINFO mi{sizeof(mi)};
|
||||||
if (GetMonitorInfoW(MonitorFromWindow(zoneWindow, MONITOR_DEFAULTTONEAREST), &mi))
|
if (GetMonitorInfoW(MonitorFromWindow(zoneWindow, MONITOR_DEFAULTTONEAREST), &mi))
|
||||||
{
|
{
|
||||||
OffsetRect(&zoneRect, mi.rcMonitor.left - mi.rcWork.left, mi.rcMonitor.top - mi.rcWork.top);
|
const auto taskbar_left_size = std::abs(mi.rcMonitor.left - mi.rcWork.left);
|
||||||
|
const auto taskbar_top_size = std::abs(mi.rcMonitor.top - mi.rcWork.top);
|
||||||
|
OffsetRect(&zoneRect, -taskbar_left_size, -taskbar_top_size);
|
||||||
|
if (accountForUnawareness)
|
||||||
|
{
|
||||||
|
zoneRect.left = max(mi.rcMonitor.left, zoneRect.left);
|
||||||
|
zoneRect.right = min(mi.rcMonitor.right - taskbar_left_size, zoneRect.right);
|
||||||
|
zoneRect.top = max(mi.rcMonitor.top, zoneRect.top);
|
||||||
|
zoneRect.bottom = min(mi.rcMonitor.bottom - taskbar_top_size, zoneRect.bottom);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WINDOWPLACEMENT placement;
|
WINDOWPLACEMENT placement;
|
||||||
@ -106,4 +122,4 @@ void Zone::StampZone(HWND window, bool stamp) noexcept
|
|||||||
winrt::com_ptr<IZone> MakeZone(RECT zoneRect) noexcept
|
winrt::com_ptr<IZone> MakeZone(RECT zoneRect) noexcept
|
||||||
{
|
{
|
||||||
return winrt::make_self<Zone>(zoneRect);
|
return winrt::make_self<Zone>(zoneRect);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user