Create window and close existing one

Hello again
I have a window created by the usual way
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
HINSTANCE hInstance;
WND splash_window, main_window;
MSG msg;

LRESULT CALLBACK splash_proc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK main_proc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nCmd) {
    start_splash_window(hInst, lpCmd, nCmd);                                     

    while (GetMessage(&msg, NULL, 0, 0) > 0) {                                     
        if ((!IsDialogMessage(splash_window, &msg))) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return (int) msg.wParam;
}

int start_splash_window(HINSTANCE hInst, LPSTR lpCmd, int nCmd) {
    WNDCLASSEX wcex;
    wcex.cbSize         = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = splash_proc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    .
    .
    .
    // Well, here I create the first window
}

int start_main_window(HINSTANCE hInst, LPSTR lpCmd, int nCmd) {
    WNDCLASSEX wcex;
    wcex.cbSize         = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = main_proc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    .
    .
    .
    // Well, here I create the second window
}
//Of course, here I have splash_proc() and main_proc() 


What I want is to create and open the second window (main_window) from the splash_proc, when a loading process is finished. At the same time I want to close the splash_window. So, I don't want main_window to be a child window of splash_window. Anyway, I am a little stuck. I suppose that I should call start_main_window() from splash_proc() then destroy splash_window but I don't know if I am right. I am confused, especially because of this code
1
2
3
4
5
6
    while (GetMessage(&msg, NULL, 0, 0) > 0) {                                     
        if ((!IsDialogMessage(splash_window, &msg))) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

I suppose that on deleting splash_window if ((!IsDialogMessage(splash_window, &msg))) should be if ((!IsDialogMessage(main_window_window, &msg))).
Some clues, please!?
Thanks
Last edited on
You should drop that message pump and use a standard one, and then use the DialogBox() function inside start_splash_window(). This way you just call start_main_window() from winMain() right after start_splash_window().
I go with webJose's suggestion about the message pump, but I would prob go with CreateDialog rather than DialogBox, so the dialog doesn't block the app.

I would usually create the (modeless) splash dialog/screen in the WM_CREATE handler of the main window, and then kick off the time consuming initialization (ideally on a worker thread). When it's finished, the thread posts a custom message to the message loop, the handler for which destroys the dialog and makes the main window visible (in this case you do not call ShowWindow in WinMain after creating the window. So it's invisible until you reveal it after inialization has finished)
Last edited on
The splash_window is not just a splash window. It allows users to change some settings (like <don't show this next time> checkbox).

So, should I create a separate handler for the splash dialog and make the splash_window a child dialog of the main_window (which is created but not shown up until user clicks on a CONTINUE button on the splash_window?

It will be like this

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
HINSTANCE hInstance;
WND splash_window, main_window;
MSG msg;

LRESULT CALLBACK main_proc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK splash_proc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nCmd) {
    start_main_window(hInst, lpCmd, nCmd);                                     

    while (GetMessage(&msg, NULL, 0, 0) > 0) {                                     
        if ((!IsDialogMessage(main_window, &msg))) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

   // Why should I remove the messages loop???

    return (int) msg.wParam;
}

int start_main_window(HINSTANCE hInst, LPSTR lpCmd, int nCmd) {
    WNDCLASSEX wcex;
    wcex.cbSize         = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = main_proc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    .
    .
    .
    // Create it but don't ShowWindow yet
}

LRESULT CALLBACK main_proc(HWND, UINT, WPARAM, LPARAM) {
    case WM_CREATE:
         //Create splash window and all its elements here
    break;
    case WM_COMMAND:
        //When user clicks on a Continue button the splash_window is destroyed
        //and the main_window is destroyed
    break;
       //Anything else
}

LRESULT CALLBACK splash_proc(HWND, UINT, WPARAM, LPARAM) {
    case WM_COMMAND:
        //When user clicks on a Continue button the splash_window is destroyed
        //and the main_window is destroyed
    break;
       //Anything else
}


Did I get it right? Should I use the same handler for both windows?
Thanks
Last edited on
I'd more or less go with that. I would definitely keep the splash_proc separate from the main windows.

But I assume you mean

#1 start_main_window at the beginning of WinMain
#2 (comment for WM_COMMAND) When user clicks on a Continue button the splash_window is destroyed and the main_window is shown

Andy

PS And you should not remove the message loop. If you do, your program won't function!
Last edited on
Thanks to everybody. Let's get to work!
Topic archived. No new replies allowed.