【问题标题】:MFC right align controls on a CDialogBarCDialogBar 上的 MFC 右对齐控件
【发布时间】:2016-06-08 19:04:11
【问题描述】:

我有一个 CDialogBar 派生类,如下所述。一位同事对我说,MFC 不提供对齐流布局控制(我在 2012 年发现了一些令人难以置信的东西!)。正如我所展示的,我必须使用 OnSize 函数来做这件事:

//declaration of member variable
class CMyDialogBar : public CDialogBar
{
private:
    int m_old_cx;
    //...    
}


//the message map
BEGIN_MESSAGE_MAP(CMyDialogBar, CDialogBar)
    //...
    ON_WM_SIZE()
END_MESSAGE_MAP()    

//the implementation
void CMyDialogBar::OnSize(UINT nType, int cx, int cy)
{
    CDialogBar::OnSize(nType, cx, cy);

    if (!::IsWindow(this->GetSafeHwnd()))
        return;

    // align right Combo1 and its label
    CRect rc;
    CWnd *pWnd= this->GetDlgItem(IDC_COMBO1);
    if(pWnd)
    {
        pWnd->GetWindowRect(&rc);
        ScreenToClient(&rc);
        pWnd->MoveWindow(rc.left + cx - m_old_cx, rc.top ,rc.Width(), rc.Height());
    }

    pWnd= this->GetDlgItem(IDC_STATIC_COMBO_LABEL);
    if(pWnd)
    {
        pWnd->GetWindowRect(&rc);
        ScreenToClient(&rc);
        pWnd->MoveWindow(rc.left + cx - m_old_cx, rc.top ,rc.Width(), rc.Height());
    }


    m_old_cx= cx;
}

即使在看到这个工作之后,我也不太相信它。所以我的问题是:有没有更好的右对齐控件方法?

提前致谢,

塞尔吉奥

【问题讨论】:

    标签: mfc controls right-align


    【解决方案1】:

    即使在 2013 年,您的同事也是对的 - MFC 没有控件的自动布局。

    根据代码项目“Layout Manager for Dialogs, Formviews, DialogBars and PropertyPages”:

    “如果您经常使用对话框并想要调整它们的大小,您会注意到 MFC 中没有帮助您在调整大小后自动排列对话框控件的功能。您必须手动完成。”

    对于简单的对话框,我认为您的解决方案很好,并且 OnSize() 是手动进行布局的正确位置。 否则,您将不得不查看其他布局类,例如上面引用的一个或年轻几年的“Automatic Layout of Resizable Dialogs”。

    编辑:
    根据 sergiols 的评论,微软为 Blog post 中介绍的 Visual Studio 2015 开发了 Dynamic Layout,似乎可以解决这个问题。

    【讨论】:

    • 它在 Visual C++2015 中发生了变化,他们现在有一个名为“动态布局”的功能,可以解决这个问题。
    猜你喜欢
    • 2015-07-10
    • 2013-08-23
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2015-08-30
    • 2010-10-05
    • 1970-01-01
    • 2012-07-18
    相关资源
    最近更新 更多