Henadzi Matuts
Henadzi Matuts

Reputation: 355

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:

enter image description here

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

Upvotes: 1

Views: 3762

Answers (2)

Murali Krishna M.
Murali Krishna M.

Reputation: 77

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

Upvotes: 0

Marius Bancila
Marius Bancila

Reputation: 16328

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:

enter image description here

Upvotes: 4

Related Questions