Archived

This topic is now archived and is closed to further replies.

kavalants

Can get window up and running.......

Recommended Posts

Hi! I''ve been trying to get a plain win32 window to work, but w/o success. I''ve done this before and it''s happened sometimes that there''s an extremely stupid mistake I''ve made, but this time I really don''t know. It compiles fine, but then gives me the "Error creating window." message right after CreatWindowEx(...). For compilation I use MinGW with: g++.exe -o name_of_exe name_of_source_file -mwindows Could anyone please help?
  
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

HINSTANCE hInst = NULL;
HWND hWnd = NULL;
const char WndClass[] = "WndClass";
const char WndTitle[] = "Alpha 1";
bool done = false;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	MSG msg;

	ZeroMemory(&wc, sizeof(wc));
	ZeroMemory(&msg, sizeof(msg));

	hInst = hInstance;

	wc.cbSize = sizeof(wc);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInst;
	wc.hIcon = LoadIcon(hInst, IDI_APPLICATION);
	wc.hIconSm = LoadIcon(hInst, IDI_APPLICATION);
	wc.hCursor = LoadCursor(hInst, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(COLOR_WINDOW);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = WndClass;

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Error registering window class.", "ERROR", MB_OK);
		return 0;
	}

	hWnd = CreateWindowEx(0, WndClass, WndTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
							NULL, NULL, hInst, NULL);

	if(hWnd == NULL)
	{
		MessageBox(NULL, "Error creating window.", "ERROR", MB_OK);
		return 0;
	}

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

	while(!done)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else if(msg.message == WM_QUIT)
		{
			done = true;
		}
		else
		{
			continue;
		}
	}

	UnregisterClass(WndClass, hInst);
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_QUIT:
		{
			DestroyWindow(hWnd);
		}
		break;
		case WM_DESTROY:
		{
			PostQuitMessage(0);
		}
		break;
		default:
		{
			DefWindowProc(hWnd, msg, wParam, lParam);
		}
	}

	return 0;
}
  
Thanks!

Share this post


Link to post
Share on other sites
Simple - your return value from WndProc is incorrect.

From WM_NCCREATE dox:
quote:

If an application processes this message, it should return TRUE to continue creation of the window. If the application returns FALSE, the CreateWindow or CreateWindowEx function will return a NULL handle.


Always return DefWindowProc(...); .

Share this post


Link to post
Share on other sites