2012-02-21 38 views
1

我有一個如下所述的CDialogBar派生類。一位同事對我說,MFC不提供對齊流程佈局控制(我發現在2012年有些令人難以置信的事情!)。我不得不做的事與篩上部分功能爲我顯示:MFC對齊CDialogBar上的控件

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

即使看到這方面的工作後,我不相信它非常。所以我的問題是:是否有更好的方法來對齊控件?

由於提前,

塞爾吉奧

回答

2

即使在2013年你的同事是正確的 - 有與MFC來控制沒有自動佈局。

據CodeProject上「Layout Manager for Dialogs, Formviews, DialogBars and PropertyPages」:

「如果你經常使用對話框,並希望調整它們的大小,你會注意到,有在MFC中沒有的功能,可以幫助你調整後自動安排對話框控件。你必須親自去做。「

對於簡單的對話框,我認爲你的解決方案很好,而OnSize()是手工完成佈局的正確位置。 否則,您將不得不查看其他佈局類,比如上面提到的一個佈局類或幾年前的「Automatic Layout of Resizable Dialogs」。

編輯:
據sergiols評論,微軟開發Dynamic Layout對該Blog post這似乎處理這個問題推出的Visual Studio 2015年。

+0

它在Visual C++ 2015中進行了更改,現在它們具有稱爲「動態佈局」的功能,可以完全解決此問題。 – sergiol 2016-06-07 22:55:36