win32 modeless dialog hides main window behind other apps on close

The modeless dialog has a two buttons and a window-frame close button. The two buttons within the frame each open a MessageBox. If I click one of the buttons, exit the resulting messagebox, and then close the dialog, the main window disappears behind any other open applications.


I changed one of the buttons to hide the window, and the result is the same, so it's not the frame close button. (this issue occurs with both ShowWindow and DestroyWindow functions)

It seems like opening a child window, then opening a child window within that child window(assuming that dialogs, messageboxes, and windows behave the same), and then exiting both, causes the main window to be hidden.

I tried to fix the issue with SetFocus, SetActiveWindow, and SetForgroundWindow. None had any effect, but I'm not really sure how to point to the main window. I think I need a global handle substitute by which to refer to the main window, but I'm not sure how to make one. Maybe I could forgo any main window and just create one in WndProc?

I did find that calling a custom message to a second function that opens a MessageBox when closing the modeless dialog keeps the main window in focus, but only with ShowWindow. DestroyWindow still hides the main window.

Has anyone experienced this issue? Also, is win32 obsolete? And is c++, for that matter? Trying to troubleshoot this issue turns up a lot of results for c# and QT.

I can post my code, but it's slightly long, and I'm not sure whether to post all of it or to omit some of it; or what to omit. most questions in the forum seem to post short snippets of code or none at all. Also, I already asked this in the beginners forum, and was referred here.
I found a solution. I figured out how to set up the main window with a global variable, so I can call the main window in other functions. I still have to send a separate message, but SetForgroundWindow works now and I don't need to use a MessageBox.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <windows.h>
#include "resource.h"

//g_ = global
//sz_ = string zero I guess?

//Register Window Class
const char g_sz_myClass [] = "window1";

//forward declare toolbar id
HWND g_MainWindow = NULL;
HWND g_hToolbar = NULL;

//Toolbar Process >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
LRESULT CALLBACK ToolDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
                case IDC_PRESS:
                    MessageBox(hwnd, "This does nothing.", "NULL", MB_OK | MB_ICONINFORMATION);
                break;
                case IDC_OTHER:
                    MessageBox(hwnd, "This also does nothing.", "NULL", MB_OK | MB_ICONINFORMATION);
                break;
            }
        break;
        }
        case WM_CLOSE:{
            SendMessage(hwnd, WM_FOCUS, 0, 0);
            DestroyWindow(g_hToolbar);
            g_hToolbar = NULL;
        break;
        }
        case WM_FOCUS:{
            SetForegroundWindow(g_MainWindow);
                break;
        }
        default:
            return FALSE;
    }
    return TRUE;
}
//Toolbar Process <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//Main Window Process >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_COMMAND:{
            switch(LOWORD(wParam)){
                // MenuBar Command >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
                case ID_FILE_EXIT:{
                    PostMessage(hwnd, WM_CLOSE, 0, 0);
                break;
                }
                //MenuBar Command <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

                //Toolbar Command >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
                case ID_TOOLBAR_ON:
                    if(g_hToolbar == NULL){
                        g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TOOLBAR), hwnd, ToolDlgProc);
                        ShowWindow(g_hToolbar, SW_SHOW);
                    }
                    else{
                        ShowWindow(g_hToolbar, SW_SHOW);
                    }
                break;
                case ID_TOOLBAR_OFF:
                    ShowWindow(g_hToolbar, SW_HIDE);
                break;
                //Toolbar Command <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            }
        break;
        }

        //Close Command >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        case WM_CLOSE:{
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                DestroyWindow(g_hToolbar);
                PostQuitMessage (0);
            break;
        break;
        }
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        //Close Command <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    return 0;
}
//Main Window Process <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//Main Window Register >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{
    WNDCLASSEX test;
    MSG msg;

    test.cbSize = sizeof (WNDCLASSEX);
    test.style = 0;
    test.lpfnWndProc = WndProc;
    test.cbClsExtra = 0;
    test.cbWndExtra = 0;
    test.hInstance = hInstance;
    test.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    test.hCursor = LoadCursor(NULL, IDC_ARROW);
    test.hbrBackground = CreateSolidBrush(RGB(24, 15, 56));
    test.lpszMenuName = MAKEINTRESOURCE(IDR_MENUBAR);
    test.lpszClassName = g_sz_myClass;
    test.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    if(!RegisterClassEx(&test))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    //Create Window >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    if (g_MainWindow == NULL){
    g_MainWindow = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_sz_myClass,
    "Main Window",
    WS_OVERLAPPEDWINDOW,
    15, 15, 1200, 700,
    NULL, NULL, hInstance, NULL);
    }
    if(g_MainWindow == NULL)
    {
        MessageBox(NULL, "Main Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    // Create Window <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    ShowWindow(g_MainWindow, CmdShow);
    UpdateWindow(g_MainWindow);

    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
		//dialog partially encloses message loop
		if(!IsDialogMessage(g_hToolbar, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
    }
    return msg.wParam;
}
//Main Window Register <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 


resource.h:
1
2
3
4
5
6
7
8
9
10
11

#define IDR_MENUBAR 10
#define IDD_TOOLBAR 11

#define ID_FILE_EXIT 12
#define IDC_PRESS 13
#define IDC_OTHER 14
#define ID_TOOLBAR_ON 15
#define ID_TOOLBAR_OFF 16

#define WM_FOCUS (WM_USER + 0x0001) 


resources.rc:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <windows.h>
#include "resource.h"


IDR_MENUBAR MENU {
    POPUP "File"
    {
        MENUITEM "Exit", ID_FILE_EXIT
    }
    POPUP "Toolbar"
    {
        MENUITEM "On", ID_TOOLBAR_ON
        MENUITEM "Off", ID_TOOLBAR_OFF
    }
}

IDD_TOOLBAR DIALOGEX 665, 10, 100, 60
STYLE DS_MODALFRAME | WS_SYSMENU | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "Toolbar"
FONT 8, "MS Sans Serif"
{
    PUSHBUTTON "Press This Button",IDC_PRESS,10,5,80,20
    PUSHBUTTON "Or This One",IDC_OTHER,10,30,80,20
}
Last edited on
Topic archived. No new replies allowed.