Creating a Property Sheet Inside a Form View

This topic was contributed by Asaf Levy.

Form views (CFormView) can do almost anything you want: you can easily shape and modify it, process its control's messages, and communicate with the document. It's like creating a very important dialog box.

Property sheet (CPropertySheet) is also a very convinient and simple way to gather and display information. You create property pages using the dialog template editor, and simply add them to the propery sheet's object. Most importantly, the property sheet takes care of all the page's notification messages for you. All you have to be worried about is what your code will do, not where it is routed to, and who is actually getting it (each page intercepts its own IDC_blahblah's messages).

But, have you ever tried putting a propery sheet inside a form view, together with other controls like buttons, check boxes, list boxes, ect...? Sure, you can use the tab control (CTabControl), given in the dialog template editor. But who will take care of all the notification messages? YOU?? no way!! And how will you create the page's interfaces? BY HAND?? no way!!

To my opinion, these two reasons if far from enough to convince a sane programmer to look for a way out: property sheets inside a form view! It handels your notification messages and lets you add editable dialog templates to the sheet - what more can you possibly ask??

Now, lets look at how you do this. This task has a few simple steps:

Step 1: Create a "place holder" control in your form view's template

You have to create a control that will function as a place holder for the property sheet control. The most suitable control is the picture control: it will be the sheet's parent window. Make sure the control is Visible, and that it's on type Frame. Make it big enough to include the pages in it. Lets suppose that its ID of the picture control is IDC_PLACEHOLDER.

Step 2: Create a derived CPropertySheet class

Create a class derived from CPropertySheet using ClassWizard (lets call it CMyPropertySheet), and leave only one constructor with the parent window as its parameter:
	CMyPropSheet(CWnd* pParentWnd = NULL);

Step 3: Add a pointer to the derived property sheet class in your form view class

In your derived form view class, add a pointer to the newly created derived property sheet class, so you can refer to it and use it later:
	CMyPropSheet* m_pMyPropSheet;

Step 4: Add code in CMyFormView::OnInitialUpdate() to create your property sheet

Create a handler to WM_INITIALUPDATE in CMyFromView, and after the call to CFormView::OnInitialUpdate(), add code to create a Modeless Property Sheet and associate it with the place holder control.
	// create and asociated the property sheet with the "place holder" window
	CWnd* pwndPropSheetHolder = GetDlgItem(IDC_PLACEHOLDER);
	m_pMyPropSheet = new CMyPropSheet(pwndPropSheetHolder);
	if (!m_pMyPropSheet->Create(pwndPropSheetHolder, 
		WS_CHILD | WS_VISIBLE, 0))
	{
		delete m_pMyPropSheet;
		m_pMyPropSheet = NULL;
		return;
	}

	// fit the property sheet into the place holder window, and show it
	CRect rectPropSheet;
	pwndPropSheetHolder->GetWindowRect(rectPropSheet);
	m_pMyPropSheet->SetWindowPos(NULL, 0, 0,
		rectPropSheet.Width(), rectPropSheet.Height(),
		SWP_NOZORDER | SWP_NOACTIVATE);

Step 5: Create the pages by deriving from CPropertyPage

Create as many classes as you wish which are derived from CPropertyPage using ClassWizard, and leave them as they are (you will later handle the page control's specific messages there). Associate a different IDD_blahblah to every class.

Step 6: Add the page's objects as member variables to the propery sheet class

Declare objects of your page's derived types (as was created in step 5), and put them in the class declaration of your derived property sheet:
	CMyPage1 m_pageMy1;
	CMyPage2 m_pageMy2;
	......

Step 7: Add page insertion code in the derived property sheet's constructor

Now you will associate the page's objects to your property sheet, by adding them to the sheet, using the CPropertySheet::AddPage(...) function.

Note: Here i put the code in the sheet's constructor, so it will be done automatically, but you can put this code anywhere you want in the derived property sheet class.

CMyPropSheet::CMyPropSheet(CWnd* pParentWnd)
	:CPropertySheet(AFX_IDS_APP_TITLE, pParentWnd)
{
	AddPage(&m_pageMy1);
	AddPage(&m_pageMy2);
	......
}
That's it. If you compile and run your application, you will now get a fully functional property sheets with pages you can edit easily!

All you have to do now is add message handlers in each page to the page's own control. Have Fun!!