在辅助监视器中限制窗口的最大大小 [英] restrict window maximum size in secondary monitor

查看:89
本文介绍了在辅助监视器中限制窗口的最大大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多显示器设置.在辅助监视器上最大化窗口时,我需要限制最大尺寸和位置.

I have a multi monitor setup. When a window is maximized on secondary monitor, I need to restrict the maximum size and position.

在MSDN中,MINMAXINFO的文档提到以下内容:

In MSDN, the documentation for MINMAXINFO mentions the following:

对于具有多个监视器的系统,ptMaxSize和ptMaxPosition成员描述了窗口在主监视器上的最大化大小和位置,即使该窗口最终最终在辅助监视器上也已最大化.在这种情况下,窗口管理器会调整这些值以补偿主监视器和显示窗口的监视器之间的差异.因此,如果用户保持ptMaxSize不变,则大于主监视器的监视器上的窗口将最大化为较大监视器的大小.

For systems with multiple monitors, the ptMaxSize and ptMaxPosition members describe the maximized size and position of the window on the primary monitor, even if the window ultimately maximizes onto a secondary monitor. In that case, the window manager adjusts these values to compensate for differences between the primary monitor and the monitor that displays the window. Thus, if the user leaves ptMaxSize untouched, a window on a monitor larger than the primary monitor maximizes to the size of the larger monitor.

因此,如果nIdSC_MAXIMIZE,我通过在OnSysCommand中执行SetWindowPos来尝试限制.当用户单击最大化按钮/双击标题栏时,它会起作用.

So, I tried restriction by doing SetWindowPos in OnSysCommand if nId is SC_MAXIMIZE . It works, when the user clicks on maximize button/double click the title bar.

但是,当用户使用Win +向上箭头键或将窗口移到监视器顶部以最大化时,我将无法处理最大限制.

But, when the user uses Win+Up Arrow key or move the window to top of monitor to maximize, I am not able to handle the maximize restriction.

那么,有什么通用的地方可以处理我的所有情况?

So, is there any common place to handle my all scenarios?

有什么办法可以接收到WM_GETMINMAXINFO消息吗?

Is there any way to do trick on receiving WM_GETMINMAXINFO message.

推荐答案

我知道这篇文章很旧,但是我希望与仍然需要解决方案的人分享我的代码.

I know this post is old, but I wish to share my code for those who still need a solution.

void CMyDialog::OnWindowPosChanging(WINDOWPOS * pos)
{
    //let us do the default processing first
    CDialogEx::OnWindowPosChanging(pos);

    //We are only interested in setting the window size when our window is in maximized state.
    //When maximized, the window will have a WS_MAIMIZE window style set
    LONG_PTR lWndStyle = GetWindowLongPtr(this->m_hWnd, GWL_STYLE);
    if ((lWndStyle & WS_MAXIMIZE) != WS_MAXIMIZE)
        return;

    //Use the proposed window from OS to identify the monitor.
    //I found that, the MonitorFromWindow() API returns primary monitor info when I restore a minimized window from taskbar.
    RECT rectWnd = {pos->x, pos->y, pos->x + pos->cx, pos->y + pos->cy};
    HMONITOR hMon = MonitorFromRect(&rectWnd, MONITOR_DEFAULTTONEAREST);

    MONITORINFO info;
    info.cbSize = sizeof(info);
    GetMonitorInfo(hMon, &info);

    LONG nMaxWndWidth = (info.rcWork.right - info.rcWork.left);
    LONG nMaxWndHeight = (info.rcWork.bottom - info.rcWork.top);

    //The window and workspace height can be > or <
    if (pos->cy != nMaxWndHeight)
    {
        pos->cy = nMaxWndHeight;
    }

    //The window and workspace width can be > or <
    if (pos->cx != nMaxWndWidth)
    {
        pos->cx = nMaxWndWidth;
    }
}

这篇关于在辅助监视器中限制窗口的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆