【问题标题】:How do I prevent this CMFCPropertySheet from resizing too small in the height?如何防止此 CMFCPropertySheet 将高度调整得太小?
【发布时间】:2020-11-11 01:40:09
【问题描述】:

我有一个CResizingMFCPropertySheet,上面有几页。该类派生自CMFCPropertySheet。类定义相当基本:

#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);
    }
}

在大多数情况下,它会按照我的意愿行事。但是我有一个问题,它让我将工作表的高度调整得太小。例如,这是我在 IDE 中的一个页面:

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

如您所见,它允许我将高度降低太多。我们有这个处理程序:

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

并且,minimum_rcOnInitDialog 中定义:

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

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

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

    return bResult;
}

所以我被难住了。如何防止此工作表的高度调整得太小?

【问题讨论】:

    标签: visual-c++ mfc propertysheet


    【解决方案1】:

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

    在您的派生类构造函数(或声明)中将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 像素的值。这可能是因为边框厚度的原因,但没有偏移量,窗口大小有点太小了。

    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();
            }
        }
    

    就是这样。唯一的改进是使矩形膨胀的精确尺寸,但除此之外,这是完全可操作的。

    【讨论】:

    • 似乎OnGetMinMaxInfoOnInitDialog 之前被多次调用,结果它使用了错误的大小。
    • @AndrewTruckle 这就是您需要IsRectEmpty() 检查的原因。只需确保在第一次构造类对象时设置为空,就像在声明中一样:CRect minimum_rc{ 0, 0, 0, 0 };
    • 确实,对于顶级窗口,WM_GETMINMAXINFO 消息甚至在WM_NCCREATE 之前发送到窗口过程,因此建议将最小矩形初始化为某种无效状态(如空矩形)非常有效。
    • 更新了答案。
    • @and 您可以使用CWnd::CalcWindowRect 根据客户区的大小获取窗口矩形。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 2014-01-07
    • 2017-06-20
    • 1970-01-01
    • 2016-10-05
    • 2018-09-27
    相关资源
    最近更新 更多