Link to home
Start Free TrialLog in
Avatar of parmesan
parmesan

asked on

A modeless PropertySheet

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.

Avatar of chensu
chensu
Flag of Canada image

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;

ASKER CERTIFIED SOLUTION
Avatar of Daniel_E
Daniel_E

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RONSLOW
RONSLOW

>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

Avatar of parmesan

ASKER

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.