How to make a CFrameWnd modal - BeginModalState() not working

I'm trying to make a CFrameWnd modal. In this case, I have my main CFrameWnd for my app and I'm creating another CFrameWnd which I want to stay activated and at the top of the zorder until I close it.

The new window creates and shows up at the top of the zorder as expected. I tried BeginModalState(), but that does something wierd. It makes it so I can't move the new (supposedly modal) window, but I can still activate the main CFrameWnd of my app and it moves to the foreground. If I do this, then it won't let me click on the new window and activate it again. I have to click on it's icon in the taskbar and then I move my mouse over the new window for it to activate again. I also can't close the new window with the X button in the titlebar. I have to hit the X Button in the main CFrameWnd and it closes with the application.

Anyone know how to pull this off?
[922 byte] By [Yasoo] at [2007-11-19 6:40:54]
# 1 Re: How to make a CFrameWnd modal - BeginModalState() not working
Any reason for not using a Modal dialog for the purpose ?
kirants at 2007-11-11 0:30:17 >
# 2 Re: How to make a CFrameWnd modal - BeginModalState() not working
BeginModalState works as expected.
You are calling BeginModalState for a new window therefore it is disabled.
BeginModalState disables frame window. You want to put main window in modal state, not window that you create later.

Modal state means frame window and all descendants are disabled until EndModalState is called.

If you want second window to stay always on the top of main window even if restored after being minimized make main frame an owner of the second frame.

All of it can be achieved by creating owned frame and call EnableWindow for a main frame without using BeginModalState.

The only difference is that BeginModalState checks if all popup windows can be disabled.
JohnCz at 2007-11-11 0:31:17 >
# 3 Re: How to make a CFrameWnd modal - BeginModalState() not working
BeginModalState works as expected.
Modal state means frame window and all descendants are disabled until EndModalState is called.
disabled.
Oooooh! :) Makes sense now.

Thanks a bunch John.

Any reason for not using a Modal dialog for the purpose ?

Yes, I don't like Dialogs. :) They have things built into them that I don't need/like/want. The main one being how they change things around at different screen resolutions.
Yasoo at 2007-11-11 0:32:17 >
# 4 Re: How to make a CFrameWnd modal - BeginModalState() not working
Yes, I don't like Dialogs. :) They have things built into them that I don't need/like/want. The main one being how they change things around at different screen resolutions.Well, although I usually advocate for not using dialogs when not appropriate (dialogs are strongly overused, especially as application main windows): The "changing things around" is nothing "built into" dialogs, it's just the way dialog units are translated to device units when a dialog box (and its controls) are built from a dialog resource template. So if you just create an empty dialog and create all controls dynamically (as you would anyway with a non-dialog window), this will not happen. However, a point where dialog windows have a differing behaviour is keybord handling (for navigation between controls).

Note that I don't say that you should use a dialog in your case, use a frame window and the approach pointed out by John. I just wanted to clarify that the dependency on the system font size has nothing to do with a special runtime behaviour of dialogs, but happens once when a dialog is created from a resource template.
gstercken at 2007-11-11 0:33:22 >
# 5 Re: How to make a CFrameWnd modal - BeginModalState() not working
gstercken -

I appreciate that info. That is good to know. The first VC++ project that I presented to someone, I used the Wizard to create a Dialog based app. I spent hours and hours. It had a really nice bitmap background and I had text boxes lined up by pixel so it looked like the input boxes were part of the image. I was all excited to show off my work. The computer I ran my demo on was at a higher screen resolution. The text boxes weren't in the right place and the bitmap was, I think, resampled to stretch to fit. Anyway, it was a disaster and I've pretty much stayed away ever since. :)
Yasoo at 2007-11-11 0:34:25 >
# 6 Re: How to make a CFrameWnd modal - BeginModalState() not working
Anyway, it was a disaster and I've pretty much stayed away ever since. :)That's fine. :) If you want to read more about the limitations of "dialog-based" apps, also see this FAQ ( http://www.dev-archive.com/forum/showthread.php?s=&threadid=267664).
gstercken at 2007-11-11 0:35:16 >
# 7 Re: How to make a CFrameWnd modal - BeginModalState() not working
gstercken -

I checked out out that link. That is some good advice. I've yet to use the SDI or MDI Wizards. I've spent years using top-end classes (and now Wizards) and always had the same issue of running into a wall when I want it to do something that it can't do or it does something wierd that you don't expect and it doesn't have an explanation why. Using the Dialog was the last straw for me. I didn't even bother with the SDI or MDI Wizards. I do it all from scratch. I made my own MDI logic. The heck with it! :) Now I know where to go in my code to do what I want to do and I have more control. It takes a little longer at first, but the rewards down the road are worth it.
Yasoo at 2007-11-11 0:36:20 >
# 8 Re: How to make a CFrameWnd modal - BeginModalState() not working
It takes three stages to get into Windows programming in C++ using MFC:
1. Knowledge of the language.
2. Knowledge of Windows insides (raw API programming).
3. Knowledge of MFC and its environment.

After programming for windows in C I really felt good when M$ came up with Visual Workbench and MFC.

Having ability not to write everything from scratch and app wizard that generated preliminary code for different types of application was a blessing.
Couple of miles case long statements were gone as one example.
It is a matter of understanding code written by wizard to realize how to navigate through.

Since then I have always used app and class wizard to generate a code and insert window and message handlers as well as subclassing dialog controls.
JohnCz at 2007-11-11 0:37:25 >
# 9 Re: How to make a CFrameWnd modal - BeginModalState() not working
It is a matter of understanding code written by wizard to realize how to navigate through.

I agree that is one matter - not the only one, though. :) I really like the visual system for a lot of things. Having a project interface like that is awesome. The debugger is nothing short of great - especially the watch window with the different tabs, and the memory window. The file and class browser is great. I haven't been able to figure out how to get it to work, but the docs imply I can add an icon to the new project list and create my own wizard steps. That's about as good as it gets - except, like I said, I can't get it to work :lol: I'll use the Dialog Wizard to make quick little numbers I use for myself. For bigger projects, I like to start with the empty MFC project. I always seem to manage to reach a roadblock in Wizard created projects or a class that's supposed to do something and I want to hone in on a behaviour and it becomes more difficult to twist it into shape than make it from scratch. Not just in this language, either... every one I've used.
Yasoo at 2007-11-11 0:38:23 >