简体   繁体   中英

Windows MFC: adjust child dialog to a tab control display area

I am creating some dialog-based MFC application (C++) and need to use tab control. Here is the code where I try to adjust child dialog to a tab control display area (Visual Studio 2015):

/* main dialog */
BOOL CResourceBrowserDlg::OnInitDialog()
{
   ....
   /* 
    * `m_Page` is my child dialog instance:
    * CDlgFilterPage::CDialogEx *m_Page
    */
   m_Page = new CDlgFilterPage();
   m_Page->Create(IDD_FILTERPAGE, m_FilterTab.GetWindow(IDD_FILTERPAGE));

   RECT rect;

   /*
    * `m_FilterTab` is a tab control element:
    * CTabCtrl m_FilterTab
    */
   m_FilterTab.GetWindowRect(&rect);
   m_FilterTab.AdjustRect(FALSE, &rect);

   m_Page->MoveWindow(&rect);
   m_Page->ShowWindow(SW_SHOW);

   m_FilterTab.InsertItem(0, L"Page1");
   ...
}

Running this i got the following:

在此处输入图片说明

So how should I act to get child window fit nicely within tab control?

First of all, you probably want to first add a page and then position the other dialog within the client area of the tab. Otherwise, your tab window will not have the tab buttons and the size of the dialog will be larger than what you expect.

Second, you need to position the new dialog inside the client area. You have to retrieve that and then translate it based on the window area.

Here is how you do all that:

m_Page = new CDlgFilterPage();
m_Page->Create(IDD_FILTERPAGE, m_FilterTab.GetWindow(IDD_FILTERPAGE));

m_FilterTab.InsertItem(0, L"Page1");

CRect rcClient, rcWindow;

m_FilterTab.GetClientRect(&rcClient);   
m_FilterTab.AdjustRect(FALSE, &rcClient);

m_FilterTab.GetWindowRect(&rcWindow);   
ScreenToClient(rcWindow);

rcClient.OffsetRect(rcWindow.left, rcWindow.top);

m_Page->MoveWindow(&rcClient);
m_Page->ShowWindow(SW_SHOW);

The result is this:

在此处输入图片说明

Do not try to get the Window position in OninitDialog() function. It will show 0,0 location instead of actual position of dialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM