I have a CDialogBar derived class stated as below. A colleague said to me that MFC doesn't offer aligment flow layout control (which I find something unbelievable in the year 2012!). I had to do the thing with the OnSize function as I show:

//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;
}

Even after seeing this working, I do not trust it very much. So my question is: Is there a better way of right -aligning controls?

Thanks in advance,

Sérgio

有帮助吗?

解决方案

Even in the year 2013 your colleague is right - there is no auto layout of controls coming with MFC.

According to CodeProject "Layout Manager for Dialogs, Formviews, DialogBars and PropertyPages":

"If you often use Dialogs and want to resize them you will have noticed that there is no feature in MFC that helps you arranging the dialog controls automatically after resizing. You have to do it by hand."

For simple dialogs I think your solution is just fine, and OnSize() is the right place to do the layout by hand. Otherwise you will have to look at additional layout classes like to one referenced above or "Automatic Layout of Resizable Dialogs" which is a few years younger.

EDIT:
According to sergiols comment, Microsoft developed Dynamic Layout for Visual Studio 2015 introduced in this Blog post which seems to handle the problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top