Self closing Modeless dialog open from Modal Dialog switches Active Window in Windows.

Swapnil Prajapati 20 Reputation points
2023-01-17T08:57:36.43+00:00

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();
		pMainWnd->SetActiveWindow();

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.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
1,906 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,637 questions
{count} votes

Accepted answer
  1. RLWA32 28,676 Reputation points
    2023-01-23T09:31:44.2033333+00:00

    @David Lowndes had the right idea. Make the following change to your code and the focus will be properly returned to the button when the self-closing dialog is closed.

    
    LRESULT My1ClosingDlg::OnActivate(WPARAM wParam, LPARAM lParam)
    {
        WORD fActive = LOWORD(wParam);
    
        if (fActive == WA_INACTIVE)
        {
            /*DestroyWindow()*/PostMessage(WM_CLOSE);
        }
        return 0L;
    }
    
    

0 additional answers

Sort by: Most helpful