Solved

A modeless PropertySheet

Posted on 1998-03-16
4
1,414 Views
Last Modified: 2008-02-01
Hi there!

How can I change a Modal propertysheet into a Modeless one?

I have been looking in some books but the don't mention this  problem therefore I looked on the OnLine-help (MFC VC++ 4.0).

Their is a help on 'Creating a modeless property sheet' but after reading it I still can't make my program to work. What bothers me is the second paragraph when they talk about changing from DoModal to CPropertySheet::Create().
Under CPropertySheet::Create()'s REMARK section they say that it can be inside they constructor or afterwards, what do they actually mean? Where shall I put the Create()...?

I would be very greatful if someone could help me with some tip on how to create a Modeless Property sheet.

Thank you in advance.

0
Comment
Question by:parmesan
4 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1183535
For the modal property sheet, you do something like this:

CMyModalPropertySheet myModalPS;
myModalPS.DoModal();  // The DoModal does not return until the property sheet closes.


Then, for modeless property sheet, you do something like this:
CMyModelessPropertySheet myModelessPS;
myModelessPS.Create(...);  // The Create returns immediately after creating the property sheet.


If you call Create inside the constructor like this:
CMyModelessPropertySheet::CMyModelessPropertySheet()
{
...
this->Create(...);
...
}

you don't call it after the object is created. You just write:
CMyModelessPropertySheet myModelessPS;

0
 
LVL 1

Accepted Solution

by:
Daniel_E earned 50 total points
ID: 1183536
I don't know why you rejected the correct answer by chensu, but
let me try another approach (step-by-step):

1. Create a new CDialog-based project (called PP here).
2. Add your property pages with the resource editor (called
   IDD_PP1 and IDD_PP2 here).
3. Create a new CPropertPage class for each dialog/page (called
   CPP1 and CPP2 here).
4. Add three member variables to your CPPDlg class:
   CPP1 m_pp1;
   CPP2 m_pp2;
   CPropertySheet m_ps;
5. Initialize the propert sheet (from f.e. CPPDlg::OnInitDialog()):
   m_ps.AddPage(&m_pp1);
   m_ps.AddPage(&m_pp2);
6. Show the property sheet (can also be done from CPPDlg::OnInitDialog()):
   m_ps.Create();  // Creates modeless propertysheet
   or
   m_ps.DoModal(); // Creates modal propertysheet

You can of course do the same from a any kind of MFC project - it doesn't need to be CDialog-based.

BTW, I suggest you don't pay too much attention to the REMARK section.

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1183537
>How can I change a Modal propertysheet into a Modeless one?

There is no great difference in the code between a modal and modeless property sheet class.  The main difference is in how you call up the property sheet.

However, in practice, you wlll probably need to handle things a little differently when the sheet is modeless.  For example, changes should be applied immediately (not when the user hits Apply or OK).  Also you may need to override the PostNCDestory function so that the sheet gets destructed properly

>I have been looking in some books but the don't mention this  >problem therefore I looked on the OnLine-help (MFC VC++ 4.0).
>
>Their is a help on 'Creating a modeless property sheet' but >after reading it I still can't make my program to work. What >bothers me is the second paragraph when they talk about >changing from DoModal to CPropertySheet::Create().

This is the whole crux of the matter.

When you use DoModal, MFC creates a modal property sheet (with OK, Cancel, Apply buttons) and then lets the user interact with it until the user closes the property sheet (eg. presses the OK button).  Then the DoModal function then returns and tells you how the user closed it (eg. IDOK means the user pressed OK to close).  SO the property sheet window only lasts for the duration of the DoModal call and is already gone by the time DoModal returns.

For modeless property sheets, you need to manage the creation and destruction of the property sheet yourself.  You create it by calling the Create function instead of DoModal.  You can later destroy it with a DestroyWindow or let the user close it with the [X] button.

Usually you will create it and wait for the user to close it himself (rather than close it under program control).

Depending on how you define the variable for the property sheet, you may need to override PostNCDestroy.  If you create the variable with 'new', then override PostNCDestroy to do a 'delete this;'

>Under CPropertySheet::Create()'s REMARK section they say that >it can be inside they constructor or afterwards, what do they >actually mean? Where shall I put the Create()...?

You need to understand the difference between a C++ object and a windows object - eg. a CPropertySheet object is a C++ object and is a lump of data and some member functions, the property sheet window that appears on the screen is a window object.

Usually the C++ object creates the windows object in its Create function and attaches itself to the windows object (vice versa actually).  When the windows object is destroyed, it (may) also destroy the C++ object.  However, although a C++ and windows object are closely linked, they are distinct.

MFC (almost) always uses two-stage construction - first you construct a C++ object (the data) and then call its Create member function to create the windows object and attach it to the C++ object.

For dialogs and property sheets, you can call DoModal instead of Create.  This function handles the creation, interaction and destruction of the windows object for you in one call.

If you want modeless dialogs or property sheets, you directly call Create yourself.

However, it is possible to do this in a single step by having the Create call within the constructor for C++ object.  However, this is not usually recommended - but it does simplify the code a bit in some situations - in particular when a modeless property sheet is to last for the duration of the program, rather than be created when actually needed.

One thing you need to be careful of is the SCOPE of the C++ object.  If you make it a local variable in some member function - remember that when the function exits, then variable will go out of scope and kill the property sheet window - it will just flash up and disappear almost instantly (you may not even get to see it)

It is usually best to make the variable a member variable of, say, your main frame or view class so that the C++ object doesn't disappear.  Then just call Create when you want the sheet to appear.

Alternatively you can use a pointer and 'new' to dynamically create the C++ object - in this case, you won't explictily call 'delete', but will override PostNCDestroy for the sheet to do a 'delete this;'.

>I would be very greatful if someone could help me with some tip >on how to create a Modeless Property sheet.

See my comments above especially regarding the scope of the C++ object and also the previous answers for the mechanics of creating one

0
 

Author Comment

by:parmesan
ID: 1183538
Daniel E., thank you for your help, together with chensu's answer and Ronslow's comment I'm sure that I'll have my modeless propertysheet working.

Thank you.


0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now