CDialogBar上的MFC右对齐控件

CDialogBar上的MFC右对齐控件,mfc,controls,right-align,Mfc,Controls,Right Align,我有一个CDialogBar派生类,如下所述。一位同事对我说,MFC不提供对齐流布局控制(我在2012年发现了一些不可思议的事情!)。我必须使用OnSize函数,正如我所示: //declaration of member variable class CMyDialogBar : public CDialogBar { private: int m_old_cx; //... } //the message map BEGIN_MESSAGE_MAP(CMyDial

我有一个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;
}
即使在看到这种方法奏效之后,我也不太相信它。所以我的问题是:有没有更好的方法来正确对齐控件

提前感谢,


Sérgio

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

根据代码项目“”:

“如果您经常使用对话框并想调整其大小,您会注意到MFC中没有任何功能可以帮助您在调整大小后自动排列对话框控件。您必须手动操作。”

对于简单的对话框,我认为您的解决方案很好,OnSize()是手动布局的正确位置。 否则,您将不得不查看额外的布局类,如上面引用的布局类或年轻几年的布局类“”

编辑:

根据sergiols的评论,微软为Visual Studio 2015开发的软件在本文中介绍,它似乎解决了这个问题。

它在Visual C++2015中发生了变化,现在它们有了一个名为“动态布局”的功能,正好解决了这个问题。