• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

Confused about CTabCtrl...

I wanted to ask advice about how to go about something...

I'm creating a dialog that requires tabbed pages.  I have chosen not to use Property Pages, so that I wouldn't spawn another dialog box.  Therefore, I picked CTabCtrl.  Looking at the Microsoft example, FIRE, I noticed that the single control (a picture box) is placed on the dialog (at design time) and shows up regardless of which tab page is showing.  I intend to display several controls per page and wondered how to go about this.  Must all the controls (at design time) sit on the dialog, rather than the tabbed page?  How best can I do this without making a mess?  How should the controls be link, so that they only show when the appropriate page is being displayed?

In VB there is extensive support for design time layout of controls and for grouping of controls (on the tabbed pages).  How best should I go about this in VC?

Thanks
- Don Schindler
0
donschin
Asked:
donschin
1 Solution
 
vesselCommented:
Hi, donschin.

First of all, consider using CPropertyPage and CPropertySheet classes (see MFC samples).

If for some reason you want to place tabbed pages to your dialog manually you should go this way:
- Create your dialog with controls you need and leave space for tabbed pages. Let the class name for your dialog be CYourDialog.
- Prepare pages. Use resource editor to create dialog templates for your pages. See "Using Property Sheets in Your Application" MSDN article in order to specify correct styles for dialog template resource. When you add class for new template use CPropertyPage as a parent class instead of CDialog. So you have CYourPage1, CYourPage2, ...
- Then insert new class CYourSheet inherited from CPropertySheet. Its constructor will be:
CYourSheet::CYourSheet() {
  AddPage(new CYourPage1);
  AddPage(new CYourPage2);
  // ...
}
- Override OnCreate method in CYourDialog and add m_pYourSheet member variable which is a pointer to CYourSheet:
int CYourDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (CDialog::OnCreate(lpCreateStruct) == -1)  return -1;

  m_pYourSheet = new CYourSheet();
  if (!m_pYourSheet->Create(this, WS_CHILD | WS_VISIBLE | DS_CONTROL, 0))  return -1;

  // Resize the dialog so that it fits around your tabbed pages
  // and other controls.
  CRect rectClient;
  m_pYourSheet->GetWindowRect(rectClient);
  CRect rectWindow = rectClient;
  CalcWindowRect(rectWindow);
  SetWindowPos(NULL, rectWindow.left, rectWindow.top,
  rectWindow.Width()/* add extra width here*/,
  rectWindow.Height() + 35, // extra height added
  0);
  // Set position for your tabbed pages - (0, 0)
  m_pYourSheet->SetWindowPos(NULL, 0, 0,
  rectClient.Width(), rectClient.Height(), SWP_NOZORDER | SWP_NOACTIVATE);

  return 0;
}

Note that you needn't to use CTabCtrl.

Note also that it's very important to use DS_CONTROL flag otherwise system crashes. This flag will make CYourSheet behave like a control rather than a dialog box.
0
 
donschinAuthor Commented:
Thanks!
I have followed your instructions and found a good reliable method for building the pages...  I built two blank pages and created CPropertyPage based classes for them.  Moved all the code (suggested for OnCreate) to OnInit, and built.  The dialog pops up with two tabs with pages containing static text ("Page 1" and "Page 2").  This works fine!  I need to peruse the CPropertyPage help pages to set the tab text, but that should be easy.

You might consider posting these recommendations as instructions for "Better Tab Controls."  This is really nice...

I must say all the other advice I have received depended on using some other custom control, or some horrendous collection of classes.  This is nice and simple.

Thank VERY much for your attention,

- Don Schindler
P.S.  Next time I'll post my questions to the *right* group (like the MFC group)!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now