windows - 窗口定位导致 Windows 10 上窗口周围的空间

标签 windows winapi

我有一些代码可以将窗口定位到屏幕象限。它在 Windows XP、7 和 8/8.1 上运行良好。但是,在 Windows 10 上,窗口之间存在奇怪的差距。额外的空间围绕着 window 的所有 4 个侧面。我认为它与窗口边框有关,但无法弄清楚如何解决该问题。任何输入将不胜感激。代码如下:

// Get monitor info
HMONITOR hm = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO mi;
mi.cbSize = sizeof(mi);
GetMonitorInfo(hm, &mi);

// Set screen coordinates and dimensions of monitor's work area
DWORD x = mi.rcWork.left;
DWORD y = mi.rcWork.top;
DWORD w = mi.rcWork.right - x;
DWORD h = mi.rcWork.bottom - y;

switch (corner) {
case 0: // Left top
    SetWindowPos(hWnd, HWND_TOP, x, y, w / 2, h / 2, SWP_NOZORDER);
    break;
case 1: // Right top
    SetWindowPos(hWnd, HWND_TOP, x + w / 2, y, w / 2, h / 2, SWP_NOZORDER);
    break;
case 2: // Right bottom
    SetWindowPos(hWnd, HWND_TOP, x + w / 2, y + h / 2, w / 2, h / 2, SWP_NOZORDER);
    break;
case 3: // Left bottom
    SetWindowPos(hWnd, HWND_TOP, x, y + h / 2, w / 2, h / 2, SWP_NOZORDER);
    break;
}

最佳答案

我设法通过像这样按计算出的边距膨胀目标矩形来纠正这种效果:

static RECT GetSystemMargin(IntPtr handle) {
    HResult success = DwmGetWindowAttribute(handle, DwmApi.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS,
        out var withMargin, Marshal.SizeOf<RECT>());
    if (!success.Succeeded) {
        Debug.WriteLine($"DwmGetWindowAttribute: {success.GetException()}");
        return new RECT();
    }

    if (!GetWindowRect(handle, out var noMargin)) {
        Debug.WriteLine($"GetWindowRect: {new Win32Exception()}");
        return new RECT();
    }

    return new RECT {
        left = withMargin.left - noMargin.left,
        top = withMargin.top - noMargin.top,
        right = noMargin.right - withMargin.right,
        bottom = noMargin.bottom - withMargin.bottom,
    };
}

然后做

RECT systemMargin = GetSystemMargin(this.Handle);
targetBounds.X -= systemMargin.left;
targetBounds.Y -= systemMargin.top;
targetBounds.Width += systemMargin.left + systemMargin.right;
targetBounds.Height += systemMargin.top + systemMargin.bottom;

这适用于我可以测试它的所有窗口,除了我硬编码排除的资源管理器窗口。如果我在靠近屏幕边缘的 Explorer 上进行扩展,窗口最终会将大面积区域溢出到相邻的显示器。

关于windows - 窗口定位导致 Windows 10 上窗口周围的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32752728/

相关文章:

delphi - Windows ETW : Kernel consumer receives no EventCallback or BufferCallback events

python - os.path.split 似乎返回错误

node.js - Node ffi - GetWindowRect

python - 在 Windows 上单独安装/分发 Py Launcher

winapi - 什么是 "transactional"文件操作?

vb.net - 窗口 - 按代码单击确定按钮

c - 如何为 GetModuleFileNameEx 禁用 WOW64 文件系统重定向?

c++ - 创建虚拟设备

.net - MS Access 驱动程序问题

c++ - WinApi 和 C++ - 状态栏不更新