[WIN32 API] Multiple Window Creation.

Messing with the WINAPI and trying to get 2 separate windows to load in the event I ever create a GUI app where say a menu item might load up another window (options window or something).

Anyways I'm having trouble getting a second window on screen. Loads the first window just fine. Any clues as to why would be greatly appreciated.

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
#include <Windows.h>
#include "globals.h"

LPCTSTR clsName = "First Class Name";
LPCTSTR wndName = "First Window";
LPCTSTR clsName2 = "Second Class Name";
LPCSTR wndName2 = "Second Window";

LRESULT CALLBACK wndProc2(HWND hWnd2, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	LRESULT Result = 0;

	switch (Msg)
	{
	case WM_CREATE:
		break;

	case WM_CLOSE:
		break;

	case WM_DESTROY:
		break;

	default:
		Result = DefWindowProc(hWnd2, Msg, wParam, lParam);
	}

	return(Result);
}

LRESULT CALLBACK wndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	LRESULT Result = 0;

	switch (Msg)
	{
		case WM_CREATE:
			break;

		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case ID_FILE_EXIT:
				{
					PostQuitMessage(0);
				} break;

				case ID_TOOLS_OPTIONS:
				{
					MessageBox(NULL, "Options will be here", "Options", MB_OK | MB_ICONINFORMATION);
				} break;

				case ID_HELP_ABOUT:
				{
					MessageBox(NULL, "About information will be here", "About", MB_OK | MB_ICONINFORMATION);
				} break;

			} break;
		}

		case WM_CLOSE:
		{
			PostQuitMessage(0);
		} break;

		case WM_DESTROY:
		{
			DestroyWindow(hWnd);
		} break;

		case WM_PAINT:
		{
			PAINTSTRUCT Paint;
			HDC DeviceContext = BeginPaint(hWnd, &Paint);
				int X = Paint.rcPaint.left;
				int Y = Paint.rcPaint.top;
				int Width = Paint.rcPaint.right - Paint.rcPaint.left;
				int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
				PatBlt(DeviceContext, X, Y, Width, Height, WHITENESS);
			EndPaint(hWnd, &Paint);
		}

		default:
		{
			Result = DefWindowProc(hWnd, Msg, wParam, lParam);
		}
	}

	return(Result);
}

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG Msg;
	HWND hWnd;
	HWND hWnd2;
	WNDCLASS wc = { 0 };
	WNDCLASS wc2 = { 0 };

	ZeroMemory(&hWnd, sizeof(hWnd));

	//First Window Class
	wc.hInstance = hInstance;
	wc.lpfnWndProc = wndProc;
	wc.lpszClassName = clsName;
	wc.lpszMenuName = IDR_MAINMENU;
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

	//Second Window Class
	wc2.hInstance = hInstance;
	wc2.lpfnWndProc = wndProc2;
	wc2.lpszClassName = clsName;
	wc2.lpszMenuName = IDR_MAINMENU;
	wc2.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

	if (!RegisterClass(&wc) && !RegisterClass(&wc2))
	{
		MessageBox(NULL, L"Error Registering Class", L"ERROR", MB_OK | MB_ICONERROR);
	}

	hWnd2 = CreateWindow(clsName2, wndName2, WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
	hWnd = CreateWindow(clsName, wndName, WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{
		MessageBox(NULL, L"Error Creating Window", L"Error", MB_OK | MB_ICONERROR);
	}

	ShowWindow(hWnd2, nCmdShow);
	UpdateWindow(hWnd2);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	while (GetMessage(&Msg, 0, 0, 0))
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	return Msg.wParam;
}
I've covered that topic quite exhaustively at these links...

http://www.jose.it-berater.org/smfforum/index.php?topic=3390.0

http://www.jose.it-berater.org/smfforum/index.php?topic=3392.0

At the top link are examples of creating multiple instances of the same Window Class - and keeping instance data seperate.

At the 2nd link (reply #5!) is an example of a main start up Window Class, which launches windows of seperate but different Window Classes, which I think is what you are trying to do.

Topic archived. No new replies allowed.