window creation fails

Hi,

I'm trying to create a simple window in using the Win32 API, but the window creation fails, and I don't know why.

The program handles the following messages:
36 - getminmax
129 - nccreate
130 - ncdestroy (I think this gets sent, because window creation failed)

What am I missing?

This is the 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "common.h"

namespace window
{
	class base
	{
	private:
	protected:
		HWND handle;
	public:
		static LRESULT CALLBACK message_forwarder(HWND, UINT, WPARAM, LPARAM);
		virtual LRESULT CALLBACK message_handler(HWND, UINT, WPARAM, LPARAM) = 0;
		virtual bool create(HINSTANCE, 
							LPCTSTR, 
							LPCTSTR, 
							HWND, 
							DWORD, 
							HMENU, 
							int, 
							int, 
							int, 
							int, 
							DWORD);
		base(){}
	};

	bool base::create(HINSTANCE instance, 
					  LPCTSTR window_name, 
					  LPCTSTR class_name, 
					  HWND parent = 0, 
					  DWORD style = WS_OVERLAPPEDWINDOW, 
					  HMENU menu = 0, 
					  int x = CW_USEDEFAULT, 
					  int y = CW_USEDEFAULT, 
					  int w = CW_USEDEFAULT, 
					  int h = CW_USEDEFAULT, 
					  DWORD exstyle = WS_EX_CLIENTEDGE)
	{
		handle = CreateWindowEx(exstyle, 
								class_name, 
								window_name, 
								style, 
								x, 
								y, 
								w, 
								h, 
								parent, 
								menu, 
								instance, 
								this);
		handle_error();

		if(handle)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	LRESULT CALLBACK base::message_forwarder(HWND handle, UINT message, WPARAM wparam, LPARAM lparam)
	{
		base* base_ptr = 0;

		if(message == WM_NCCREATE)
		{
			SetWindowLong(handle, GWL_USERDATA, (long)LPCREATESTRUCT(lparam)->lpCreateParams);
			handle_error();
			return 0;
		}

		base_ptr = (base*)GetWindowLong(handle, GWL_USERDATA);
		handle_error();

		if(base_ptr)
		{
			base_ptr->message_handler(handle, message, wparam, lparam);
			return 0;
		}
		else
		{
			return DefWindowProc(handle, message, wparam, lparam);
		}
	}

	//--------------------------------------------------------

	class main : public base
	{
	private:
	protected:
	public:
		virtual LRESULT CALLBACK message_handler(HWND, UINT, WPARAM, LPARAM);
		void show();
		main(){}
	};

	LRESULT CALLBACK main::message_handler(HWND handle, UINT message, WPARAM wparam, LPARAM lparam)
	{
		switch(message)
		{

		case WM_CREATE:
			MessageBox(handle, L"Window created.", L"", MB_OK);
			return 0;
		case WM_DESTROY:
			PostQuitMessage(0);
		case WM_LBUTTONDOWN:
			MessageBox(handle, L"Left button pressed", L"", MB_OK);
			return 0;
		default:
			return DefWindowProc(handle, message, wparam, lparam);
		}
	}

	void main::show()
	{
		ShowWindow(handle, SW_SHOW);
		UpdateWindow(handle);
		handle_error();
	}

	//--------------------------------------------------------

	class classex : protected WNDCLASSEX
	{
	private:
	protected:
	public:
		virtual bool register_ex();
		virtual void create(HINSTANCE, LPCTSTR);
		classex(){}
	};

	void classex::create(HINSTANCE instance, LPCTSTR class_name)
	{
		hInstance = instance;
		lpfnWndProc = base::message_forwarder;
		lpszClassName = class_name;
		lpszMenuName = 0;
		cbSize = sizeof(WNDCLASSEX);
		cbClsExtra = 0;
		cbWndExtra = 0;
		style = 0;
		hIcon = LoadIcon(0, IDI_APPLICATION);
		hIconSm = LoadIcon(0, IDI_APPLICATION);
		hCursor = LoadCursor(0, IDC_ARROW);
		hbrBackground = (HBRUSH)GetStockObject(COLOR_BTNFACE);
	}

	bool classex::register_ex()
	{
		if(RegisterClassEx(this))
		{
			return true;
		}
		else
		{
			handle_error();
			return false;
		}
	}
}

INT WINAPI WinMain(HINSTANCE instance, 
				   HINSTANCE previous_instance, 
				   LPSTR command_line_options, 
				   int show_style)
{
	window::classex the_classex;
	the_classex.create(instance, L"main");
	if(!the_classex.register_ex())
	{
		MessageBox(0, L"Failed to register window class.", L"Error", MB_OK);
		return -1;
	}

	window::main the_window;
        //this triggers the error
	if(!the_window.create(instance, L"Main window.", L"main"))
	{
		MessageBox(0, L"Failed to create window.", L"Error", MB_OK);
		return -1;
	}
	the_window.show();

	MSG message;
	int result = 0;
	while((result = GetMessage(&message, 0, 0, 0)) != 0)
	{
		if(result == -1)
		{
			handle_error();
			break;
		}
		TranslateMessage(&message);
		DispatchMessage(&message);
	}

	return message.wParam;
}
You are returning FALSE (0) when processing WM_NCCREATE. This aborts the window creation. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms632635(v=vs.85).aspx for details. You must return TRUE for the process to continue.
thank you webJose, that solved it.
Topic archived. No new replies allowed.