Second window not appear

Hi, I have modify this code to appear 2 windows at one time. It does not show error but when compiled, it only shows the first window. Can anyone tell me where I'm going wrong with this code? I have search through the internet but still not clear to me. This is my first time with windows programming.

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
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc2(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
					LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Menu 1" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0, IDC_ARROW);

  
  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, TEXT("Menu 1"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                550, 150, 270, 350, 0, 0, hInstance, 0);  

  while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
  
  MSG  msg1 ;
  WNDCLASS wc1 = {0};
  wc1.lpszClassName = TEXT( "Menu 2" );
  wc1.hInstance     = hInstance ;
  wc1.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc1.lpfnWndProc   = WndProc ;
  wc1.hCursor       = LoadCursor(0, IDC_ARROW);

  
  RegisterClass(&wc1);
  CreateWindow( wc1.lpszClassName, TEXT("Menu 2"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                550, 150, 300, 350, 0, 0, hInstance, 0);  

  while( GetMessage(&msg1, NULL, 0, 0)) {
    TranslateMessage(&msg1);
    DispatchMessage(&msg1);
  }
  return (int) msg1.wParam;
}
Last edited on
Once the message loop on line 22-25 ends your program will end on line 26 so it never gets to line 28 creating a second window.
BTW what's your plan? Normally you have one main window. If you need child windows normally you create dialogs and call them inside your Windows proc.
Thank u.

I'm just experimenting. It works. If I want to make a new window appear when I click a button, do I have to put entire Menu2 coding under WM_COMMAND?

1
2
3
4
5
case WM_COMMAND:
      {
           if (LOWORD(wParam) == 3) {
                
	   }
About WM_COMMAND
Sent when the user selects a command item from a menu, when a control sends a notification message to its parent window, or when an accelerator keystroke is translated.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms647591(v=vs.85).aspx

Have also a look at Charles Petzold's "Windows bible"
https://www-user.tu-chemnitz.de/~heha/petzold/petzold.htm
//Register both window classes and create both windows and then use only the main message loop with both callback procedures
#include <tchar.h>
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc2(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "Menu 1" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0, IDC_ARROW);


RegisterClass(&wc);
CreateWindow( wc.lpszClassName, TEXT("Menu 1"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
550, 150, 270, 350, 0, 0, hInstance, 0);
MSG msg1 ;
WNDCLASS wc1 = {0};
wc1.lpszClassName = TEXT( "Menu 2" );
wc1.hInstance = hInstance ;
wc1.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc1.lpfnWndProc = WndProc ;
wc1.hCursor = LoadCursor(0, IDC_ARROW);


RegisterClass(&wc1);
CreateWindow( wc1.lpszClassName, TEXT("Menu 2"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
450, 150, 300, 350, 0, 0, hInstance, 0);

while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_CREATE:

break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}
LRESULT CALLBACK WndProc2 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_CREATE:

break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}
Topic archived. No new replies allowed.