本文介绍了重新计算动态布局属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个可调整大小的窗口:

Here is a window that is resizable:

设置动态布局属性,以便顶部组框的宽度调整大小,下部组/框和树的尺寸调整以及3个按钮的大小.

The dynamic layout properties are set so that the top group box resizes in width and the lower group/box and tree resize in both dimensions along with the 3 buttons.

对于高级复选框,我添加了代码以隐藏额外的控件并调整关联框的高度.所以看起来像这样:

For the advanced check box I added code to hide the extra controls and adjust the heights of the associated boxes. So it looks like this:

用于切换控制值的代码为:

The code that is used to toggle the control values are:

void CWishListDlg::ToggleAdvancedMode()
{
    CRect rtSortTalk, rtTalkSettings, rtTreeGroup, rtTree, rtTalkCombo;

    m_staticSortTalk.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);
    m_cbTalkSortField.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);
    m_cbTalkSortOrder.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);
    m_staticSortSpeaker.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);
    m_cbSpeakerSortField.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);
    m_cbSpeakerSortOrder.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);
    m_staticTalkHistory.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);
    m_checkIncludeTalkHistory.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);
    m_staticStyle.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);
    m_cbStyle.ShowWindow(m_bAdvancedMode ? SW_SHOW : SW_HIDE);

    m_staticSortTalk.GetWindowRect(&rtSortTalk);
    m_staticSettings.GetWindowRect(&rtTalkSettings);
    m_staticTreeDetails.GetWindowRect(&rtTreeGroup);
    m_Tree.GetWindowRect(&rtTree);

    if (m_bAdvancedMode)
    {
        rtTalkSettings.bottom += m_iOffsetY;
        rtTreeGroup.top += m_iOffsetY;
        rtTree.top += m_iOffsetY;
    }
    else
    {
        rtTalkSettings.bottom -= m_iOffsetY;
        rtTreeGroup.top -= m_iOffsetY;
        rtTree.top -= m_iOffsetY;
    }

    ScreenToClient(&rtTalkSettings);
    ScreenToClient(&rtTreeGroup);
    ScreenToClient(&rtTree);

    m_staticSettings.MoveWindow(&rtTalkSettings);
    m_staticTreeDetails.MoveWindow(&rtTreeGroup);
    m_Tree.MoveWindow(&rtTree);
}

工作正常.我可以切换到我的内心满足感.直到我尝试调整窗口大小:

It works fine. I can toggle to my hearts content. Until I try to resize the window:

我看不到有任何方法可以根据活动显示重新计算动态布局属性.

I can't see any way to recalculate the dynamic layout properties based on the active display.

如果勾选了高级",则控件可见(因此与资源编辑器匹配),然后将其调整大小.只有在未选中它并且我修改了两个控件的情况下,调整大小才能正常使用.

If "Advanced" is ticked, so that the controls are visible (thus matching the resource editor) then it resizes fine. It is only when it is unticked and I have modified two of the controls that resizing won't work right.

推荐答案

我找到了这个很好的资源:

I found this excellent resource:

https://mariusbancila.ro/blog/2015/07/27/dynamic-dialog-layout-for-mfc-in-visual-c-2015/

您要做的是删除动态布局并重新创建它:

What you have to do is delete the dynamic layout and recreate it:

void CWishListDlg::SetupDynamicLayout()
{
    // Disable dynamic layout (this will delete the pointer and set it to NULL)
    EnableDynamicLayout(FALSE);

    // Enable dynamic layout (this will create a new pointer with no elements)
    EnableDynamicLayout(TRUE);

    // Re-create the dynamic layout content
    auto pManager = GetDynamicLayout();
    if (pManager != nullptr)
    {
        pManager->Create(this); // Assign the window!
        auto moveNone = CMFCDynamicLayout::MoveNone();
        auto moveVertical100 = CMFCDynamicLayout::MoveVertical(100);
        auto moveBoth100 = CMFCDynamicLayout::MoveHorizontalAndVertical(100, 100);
        auto sizeBoth100 = CMFCDynamicLayout::SizeHorizontalAndVertical(100, 100);
        auto sizeHorizontal100 = CMFCDynamicLayout::SizeHorizontal(100);
        auto sizeNone = CMFCDynamicLayout::SizeNone();

        pManager->AddItem(m_staticSettings.GetSafeHwnd(), moveNone, sizeHorizontal100);
        pManager->AddItem(m_staticTreeDetails.GetSafeHwnd(), moveNone, sizeBoth100);
        pManager->AddItem(m_Tree.GetSafeHwnd(), moveNone, sizeBoth100);
        pManager->AddItem(IDC_BUTTON_HELP, moveVertical100, sizeNone);
        pManager->AddItem(IDC_BUTTON_REPORT, moveBoth100, sizeNone);
        pManager->AddItem(IDC_BUTTON_EXPAND_ALL, moveBoth100, sizeNone);
        pManager->AddItem(IDC_BUTTON_COLLAPSE_ALL, moveBoth100, sizeNone);
        pManager->AddItem(IDC_STATIC_RESIZE, moveBoth100, sizeNone);
    }
}

可惜的是,您不能只是在布局中获得一个现有控件,或者告诉它根据活动内容重新初始化.但是这种方式很好用.现在可以正确调整大小:

It is a pity that you can't just get an existing control in the layout or tell it to re-initialise based on the active content. But this way works fine. It now resizes correctly:

这篇关于重新计算动态布局属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:52