Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Consider a model dialog box open in an application and this model dialog box has a button. After clicking on that button, a small self-closing modeless dialog box opens. If I click anywhere on the model dialog box this self-closing modeless dialog gets destroyed but the application window goes out of focus i.e. some other active window appears on the screen e.g. web browser running in the backgroud. Works fine if no other active application is running in the backgroud. Why does the active application window go out of focus after clicking anywhere on the model dialog box?

What I have tried:

I think, modeless dialog box is lost focus on owner. So, I have tried to set focus on owner dialog box but it has not worked.
Updated 16-Jan-23 7:40am
Richard MacCutchan 16-Jan-23 5:40am    
Without seeing your code it is impossible to make a useful suggestion. Why does the modeless dialog close when you click on its parent? What should happen is that the parent should just gain the focus.
[no name] 17-Jan-23 4:55am    
For me this implies that you might have created the modeless dialog box with the Desktop as the owner. I believe passing NULL also results with the Desktop as the owner. Could you show the code that you are using to create the dialog box?
Karan Gupta 2023 18-Jan-23 3:37am    
I have a Multiple document application.

From a menu / toolbar I open a Modal Dialog.

From this Modal Dialog , I open a Self Closing Modeless dialog.

Self Closing Modeless dialog : When the dialog is made inactive : handle WA_INACTIVE message : then I call DestroyWindow().

Thus the Modeless dialog gets auto destroyed.

After this focus does not again go in my Modal Dialog.

But, I observed that Active Window gets switched to another Window open in Windows.

I tried:

After destroying Self Closing Modeless dialog, I tried activating my Application window using AfxGetMainWnd.

CWnd* pMainWnd = AfxGetMainWnd();

This helped me to avoid switching. But I am facing another issue is that My Modal Dialog is not in focus.

I tried setting Focus with SetFocus() method. But its of no use.

Please help me how can I activate & have focus in my Modal Dialog after auto destroying Modeless dialog.

I tried creating Self Closing Modeless dialog with

this->Create(MWPartImageOrientationDlg::IDD, pParent);

where pParent is the Modal Dialog.

I even tried

this->Create(MWPartImageOrientationDlg::IDD, AfxGetMainWnd());

But no benefit.

1 solution

What you need to do is catch the WM_DESTROY message in your modeless dialog. When you receive it that means the modeless dialog is being destroyed. At that point you can call SetFocus and pass it a handle to the app's main window or you can pass the handle to the modal dialog (or a pointer to its object) that invokes your modeless dialog and call SetFocus on it. When I invoke a modeless dialog I always pass it a pointer to its parent window and in your case that will be the modal dialog so that object will be accessible to the modeless dialog.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900