简体   繁体   English

Windows MFC:将子对话框调整到选项卡控件显示区域

[英]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.我正在创建一些基于对话框的 MFC 应用程序(C++)并且需要使用选项卡控件。 Here is the code where I try to adjust child dialog to a tab control display area (Visual Studio 2015):这是我尝试将子对话框调整到选项卡控件显示区域(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.不要尝试在 OninitDialog() 函数中获取窗口位置。 It will show 0,0 location instead of actual position of dialog.它将显示 0,0 位置而不是对话框的实际位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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