如何防止此 CMFCPropertySheet 在高度上调整得太小?

How do I prevent this CMFCPropertySheet from resizing too small in the height?

我有一个 CResizingMFCPropertySheet,上面有好几页。这个class是从CMFCPropertySheet派生出来的。 class 定义相当基本:

#include "stdafx.h"
#include "ResizingMFCPropertySheet.h"
#include "resource.h"

/////////////////////////////////////////////////////////////////////////////
// CMyPropertySheet

IMPLEMENT_DYNAMIC(CResizingMFCPropertySheet, CMFCPropertySheet)

CResizingMFCPropertySheet::CResizingMFCPropertySheet(const CString& strWindowID)
    : CMFCPropertySheet()
    , m_strWindowID(strWindowID)
{
}

CResizingMFCPropertySheet::CResizingMFCPropertySheet(const CString& strWindowID, UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
    : CMFCPropertySheet(nIDCaption, pParentWnd, iSelectPage)
    , m_strWindowID(strWindowID)
{
}

CResizingMFCPropertySheet::CResizingMFCPropertySheet(const CString& strWindowID, LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
    : CMFCPropertySheet(pszCaption, pParentWnd, iSelectPage)
    , m_strWindowID(strWindowID)
{
}

CResizingMFCPropertySheet::~CResizingMFCPropertySheet()
= default;

BEGIN_MESSAGE_MAP(CResizingMFCPropertySheet, CMFCPropertySheet)
    ON_WM_SIZE()
    ON_WM_GETMINMAXINFO()
    ON_WM_DESTROY()
    ON_WM_PAINT()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyPropertySheet message handlers

int CALLBACK XmnPropSheetCallback(HWND hWnd, UINT message, LPARAM lParam)
{
    extern int CALLBACK AfxPropSheetCallback(HWND, UINT message, LPARAM lParam);
    // XMN: Call MFC's callback
    int nRes = AfxPropSheetCallback(hWnd, message, lParam);

    switch (message)
    {
    case PSCB_PRECREATE:
        // Set our own window styles
        ((LPDLGTEMPLATE)lParam)->style |= (DS_3DLOOK | DS_SETFONT
            | WS_THICKFRAME | WS_SYSMENU | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
        break;
    }
    return nRes;
}

BOOL CResizingMFCPropertySheet::OnInitDialog()
{
    BOOL bResult = CMFCPropertySheet::OnInitDialog();

    //GetClientRect(save_rc);
    GetClientRect(minimum_rc);

    //InitialiseResizeIcon();
    SetupDynamicLayout();
    RestoreWindowPosition(m_strWindowID, this, true);

    return bResult;
}

void CResizingMFCPropertySheet::OnSize(UINT nType, int cx, int cy)
{
    CMFCPropertySheet::OnSize(nType, cx, cy);
    /*

    if (!GetActivePage()) return;
    if (!GetTabControl()) return;

    if (nType == SIZE_MINIMIZED)
        return;

    int dx = cx - save_rc.Width();
    int dy = cy - save_rc.Height();

    int count = 0;
    for (CWnd *child = GetWindow(GW_CHILD); child; child = child->GetWindow(GW_HWNDNEXT))
        count++;

    HDWP hDWP = ::BeginDeferWindowPos(count);
    for (CWnd *child = GetWindow(GW_CHILD); child; child = child->GetWindow(GW_HWNDNEXT))
    {
        bool move = false;

        //If you add child controls manually, you want to move not resize
        if(child == &m_lblResize && m_lblResize.GetSafeHwnd() != nullptr)
            move = true;

        CRect r;
        child->GetWindowRect(&r);
        ScreenToClient(&r);

        if (move || child->SendMessage(WM_GETDLGCODE) & DLGC_BUTTON)
        {
            //move the main buttons and the child controls
            r.left += dx;
            r.top += dy;
            ::DeferWindowPos(hDWP, child->m_hWnd, 0, r.left, r.top, 0, 0,
                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
        }
        else
        {
            if (child->GetSafeHwnd() == m_pNavigationControl->GetSafeHwnd())
            {
                r.bottom += dy;
                ::DeferWindowPos(hDWP, child->m_hWnd, nullptr,
                    r.left, r.top, r.Width(), r.Height(),
                    SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
            }
            else
            {
                //this must be a child window, resize it
                r.right += dx;
                r.bottom += dy;
                ::DeferWindowPos(hDWP, child->m_hWnd, 0, 0, 0, r.Width(), r.Height(),
                    SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
            }
        }
    }

    ::EndDeferWindowPos(hDWP);
    GetClientRect(&save_rc);
    Invalidate(TRUE);
    */
    Invalidate(TRUE);
}

INT_PTR CResizingMFCPropertySheet::DoModal()
{
    // Hook into property sheet creation code
    m_psh.dwFlags |= PSH_USECALLBACK;
    m_psh.pfnCallback = XmnPropSheetCallback;

    return CMFCPropertySheet::DoModal();
}

void CResizingMFCPropertySheet::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
    CMFCPropertySheet::OnGetMinMaxInfo(lpMMI);
    lpMMI->ptMinTrackSize.x = minimum_rc.Width();
    lpMMI->ptMinTrackSize.y = minimum_rc.Height();
}

void CResizingMFCPropertySheet::InitialiseResizeIcon()
{
    CRect rcIcon, rcClient;

    m_bmpResize.LoadOEMBitmap(OBM_SIZE);
    m_lblResize.Create(nullptr, WS_CHILD | WS_VISIBLE | SS_BITMAP | WS_CLIPSIBLINGS,
        CRect(0, 0, 16, 16), this, IDC_STATIC_RESIZE);
    m_lblResize.SetBitmap(m_bmpResize);

    GetClientRect(rcClient);
    m_lblResize.GetClientRect(rcIcon);
    m_lblResize.SetWindowPos(&CWnd::wndBottom,
        rcClient.right - rcIcon.Width(), 
        rcClient.bottom - rcIcon.Height(), 0, 0, SWP_NOSIZE);
}

void CResizingMFCPropertySheet::RestoreWindowPosition(CString strWindow, CWnd* pWindow, bool bOverrideState)
{
    int     max_x, max_y;
    RECT    rtWindow;

    if (pWindow == nullptr)
        return;

    // Only restore if there is a previously saved position
    if ((rtWindow.top = AfxGetApp()->GetProfileInt(strWindow, _T("Top"), -1)) != -1 &&
        (rtWindow.left = AfxGetApp()->GetProfileInt(strWindow, _T("Left"), -1)) != -1 &&
        (rtWindow.bottom = AfxGetApp()->GetProfileInt(strWindow, _T("Bottom"), -1)) != -1 &&
        (rtWindow.right = AfxGetApp()->GetProfileInt(strWindow, _T("Right"), -1)))
    {
        max_x = rtWindow.right - rtWindow.left;
        max_y = rtWindow.bottom - rtWindow.top;

        // Get a handle to the monitor
        HMONITOR hMonitor = ::MonitorFromPoint(
            CPoint(rtWindow.left, rtWindow.top), MONITOR_DEFAULTTONEAREST);

        // Get the monitor info
        MONITORINFO monInfo;

        monInfo.cbSize = sizeof(MONITORINFO);
        if (::GetMonitorInfo(hMonitor, &monInfo) == 0)
            AfxMessageBox(_T("GetMonitorInfo failed"));
        else
        {
            // Adjust for work area
            rtWindow.left += monInfo.rcWork.left - monInfo.rcMonitor.left;
            rtWindow.top += monInfo.rcWork.top - monInfo.rcMonitor.top;

            // Ensure top left point is on screen
            if (CRect(monInfo.rcWork).PtInRect(CPoint(rtWindow.left, rtWindow.top)) == FALSE)
            {
                rtWindow.left = monInfo.rcWork.left;
                rtWindow.top = monInfo.rcWork.top;
            }

            rtWindow.right = rtWindow.left + max_x;
            rtWindow.bottom = rtWindow.top + max_y;

            // Restore window size
            pWindow->MoveWindow(&rtWindow, FALSE);
        }

        if (bOverrideState)
        {
            // Let us override by restoring the window state
            int iState = AfxGetApp()->GetProfileInt(strWindow, _T("ShowCmd"), SW_SHOWNORMAL);
            pWindow->ShowWindow(iState);
        }
    }
}

void CResizingMFCPropertySheet::SaveWindowPosition(CString strWindow, CWnd* pWindow)
{
    WINDOWPLACEMENT wp;

    if (pWindow == nullptr)
        return;

    pWindow->GetWindowPlacement(&wp);

    // Commit to registry
    AfxGetApp()->WriteProfileInt(strWindow, _T("Top"), wp.rcNormalPosition.top);
    AfxGetApp()->WriteProfileInt(strWindow, _T("Left"), wp.rcNormalPosition.left);
    AfxGetApp()->WriteProfileInt(strWindow, _T("Bottom"), wp.rcNormalPosition.bottom);
    AfxGetApp()->WriteProfileInt(strWindow, _T("Right"), wp.rcNormalPosition.right);
    AfxGetApp()->WriteProfileInt(strWindow, _T("ShowCmd"), wp.showCmd);
}


void CResizingMFCPropertySheet::OnDestroy()
{
    SaveWindowPosition(m_strWindowID, this);

    CMFCPropertySheet::OnDestroy();
}


CWnd* CResizingMFCPropertySheet::InitNavigationControl()
{
    m_pNavigationControl = CMFCPropertySheet::InitNavigationControl();

    return m_pNavigationControl;
}


void CResizingMFCPropertySheet::SetupDynamicLayout()
{
    EnableDynamicLayout(TRUE);
    auto pManager = GetDynamicLayout();
    if (pManager != nullptr)
    {
        pManager->Create(this);

        // The navigation control only needs to be stretched vertically
        pManager->AddItem(m_pNavigationControl->GetSafeHwnd(),
            CMFCDynamicLayout::MoveNone(), CMFCDynamicLayout::SizeVertical(100));

        // The resize control needs to be moved 100% in both directions
        //pManager->AddItem(m_lblResize.GetSafeHwnd(),
            //CMFCDynamicLayout::MoveHorizontalAndVertical(100, 100), CMFCDynamicLayout::SizeNone());

        for (CWnd *child = GetWindow(GW_CHILD); child; child = child->GetWindow(GW_HWNDNEXT))
        {
            if (child->GetSafeHwnd() != m_lblResize.GetSafeHwnd() &&
                child->GetSafeHwnd() != m_pNavigationControl->GetSafeHwnd())
            {
                // All buttons need to be moved 100% in all directions
                if (child->SendMessage(WM_GETDLGCODE) & DLGC_BUTTON)
                {
                    pManager->AddItem(child->GetSafeHwnd(),
                        CMFCDynamicLayout::MoveHorizontalAndVertical(100, 100), CMFCDynamicLayout::SizeNone());
                }
                else // This will be the main tab control which needs to be stretched in both directions
                {
                    pManager->AddItem(child->GetSafeHwnd(),
                        CMFCDynamicLayout::MoveNone(), CMFCDynamicLayout::SizeHorizontalAndVertical(100, 100));
                }
            }
        }
    }
}



INT_PTR CResizingMFCPropertySheet::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
{
    CRect rc;

    GetWindowRect(rc);
    rc.left = rc.right - ::GetSystemMetrics(SM_CXHSCROLL);
    rc.top = rc.bottom - ::GetSystemMetrics(SM_CYVSCROLL);
    if (rc.PtInRect(point))
        return HTBOTTOMRIGHT;

    return CMFCPropertySheet::OnToolHitTest(point, pTI);
}

void CResizingMFCPropertySheet::OnPaint()
{
    CPaintDC dc(this); // device context for painting
                       // TODO: Add your message handler code here
                       // Do not call CResizingMFCPropertySheet::OnPaint() for painting messages

    CRect rc;
    GetClientRect(&rc);
    rc.left = rc.right - ::GetSystemMetrics(SM_CXHSCROLL);
    rc.top = rc.bottom - ::GetSystemMetrics(SM_CYVSCROLL);
    HTHEME ht = OpenThemeData(GetSafeHwnd(), L"STATUS");
    if (ht)
    {
        DrawThemeBackground(ht, dc, SP_GRIPPER, 0, &rc, nullptr);
        CloseThemeData(ht);
    }
    else
    {
        dc.DrawFrameControl(rc, DFC_SCROLL, DFCS_SCROLLSIZEGRIP);
    }
}

在大多数情况下,它的行为符合我的要求。但是我有一个问题,它让我可以调整 sheet 的高度太小。例如,这是我在 IDE 中的一个页面:

所有其他页面都设计为完全相同的高度。然而,当我显示 sheet 时,我可以像这样调整它的大小:

如您所见,这让我可以将高度降低太多。我们有这个处理程序:

void CResizingMFCPropertySheet::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
    CMFCPropertySheet::OnGetMinMaxInfo(lpMMI);
    lpMMI->ptMinTrackSize.x = minimum_rc.Width();
    lpMMI->ptMinTrackSize.y = minimum_rc.Height();
}

并且,minimum_rc 定义在 OnInitDialog:

BOOL CResizingMFCPropertySheet::OnInitDialog()
{
    BOOL bResult = CMFCPropertySheet::OnInitDialog();

    //GetClientRect(save_rc);
    GetClientRect(minimum_rc);

    //InitialiseResizeIcon();
    SetupDynamicLayout();
    RestoreWindowPosition(m_strWindowID, this, true);

    return bResult;
}

所以我在这里被难住了。如何防止 sheet 的高度调整得太小?

您应该在传递的 lpMMI 结构中设置最小范围 调用基础 class OnGetMinMaxInfo 函数之前。

在派生的 class 构造函数(或声明)中将 minimum_rc 设置为“空”也是一个好主意,并检查它是否 而不是 在尝试使用它的宽度和高度之前为空,这样您只能在 OnInitDialog 设置它们之后使用这些值。

void CResizingMFCPropertySheet::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
//  CMFCPropertySheet::OnGetMinMaxInfo(lpMMI); // Will set 'default' values
    if (!minimum_rc.IsRectEmpty()) {
        lpMMI->ptMinTrackSize.x = minimum_rc.Width();
        lpMMI->ptMinTrackSize.y = minimum_rc.Height();
    }
    CMFCPropertySheet::OnGetMinMaxInfo(lpMMI); // Will set your modified values
}

更新

按照上述方式对 属性 张纸进行操作时似乎出现了一些问题。最后这就是工作。

  1. 在构造函数中将矩形初始化为空:
    CResizingMFCPropertySheet::CResizingMFCPropertySheet(const CString& strWindowID)
        : CMFCPropertySheet()
        , m_strWindowID(strWindowID)
    {
        minimum_rc.SetRect(0, 0, 0, 0);
    }
  1. 当你得到OnInitDialog中的尺寸时,扩大矩形的尺寸:
    BOOL CResizingMFCPropertySheet::OnInitDialog()
    {
        BOOL bResult = CMFCPropertySheet::OnInitDialog();
    
        GetWindowRect(minimum_rc);
        minimum_rc.InflateRect(10, 10);
    
        //InitialiseResizeIcon();
        SetupDynamicLayout();
        RestoreWindowPosition(m_strWindowID, this, true);
    
        return bResult;
    }

我选择了 10 个像素的值。这可能是因为边框粗细但没有偏移 window 大小只是有点太小了。

  1. 像这样处理OnGetMinMaxInfo
    void CResizingMFCPropertySheet::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
    {
        CMFCPropertySheet::OnGetMinMaxInfo(lpMMI); // Will set your modified values
        if (!minimum_rc.IsRectEmpty())
        {
            lpMMI->ptMinTrackSize.x = minimum_rc.Width();
            lpMMI->ptMinTrackSize.y = minimum_rc.Height();
        }
    }

就是这样。唯一的改进是为矩形充气的准确尺寸,但除此之外这是完全可操作的。