Page 1 of 1

Problems with window creation

Posted: July 17th, 2017, 1:08 pm
by MeleeMaster
Hi everyone. I was trying to write a code that would create a window with "Hello World" on the title bar, but I ran in some unexpected problems. It would be pretty helpful if someone could clarify this.

The errors are as follows:

1° - Line 7 & Line 51, it shows an error in the declaration for the function that will process Windows messages(WinProc) and when I put the cursor on the word it says: "expected a ';' "

2° - Line 109, the "if" statement that checks the handle(hwnd) to see if the "CreateWindow" function was successful, it says: "expected a declaration"

3° - Line 118, the "UpdateWindow" function says that "this declaration has no storage class or type specifier", and also that the identifier (hwnd) is undefined.

There were more errors but they "disappeared" after some time.

By the way, I took this example from a book called "Programming 2D Games" by Charles Kelly. The program is on the pages 24-26. Thanks in advance.

Re: Problems with window creation

Posted: July 17th, 2017, 2:01 pm
by xu2201g
Hi,
i usually write console apps, but i was able to figure out what was necessary to make it compile and run for me at least.

Code: Select all

LRESULT WINMAIN WinProc(HWND, UINT, WPARAM, LPARAM);
In this line you got a function called WinProc. A function usually consists of a return type, functionname, functionparameters(those within these parenthesis) and a function body.

So for your function you got the return type = LRESULT, name = WinProc, those parameters, but that WINMAIN is needless (correct me if iam wrong). I deleted it and a bunch of errors disappeared.

Another thing i had to do was to cast WinProc as (WNDPROC) here:

Code: Select all

bool CreateMainWindow(HINSTANCE hInstance, int nCmdShow)
{
	WNDCLASSEX wcx;
	HWND hwnd;

	//Fill in the window class structure with parameters that describe the main window

        wcx.cbSize = sizeof(wcx);
	wcx.style = CS_HREDRAW | CS_VREDRAW;
	wcx.lpfnWndProc = (WNDPROC) WinProc;    //<
	wcx.cbClsExtra = 0;
	wcx.cbWndExtra = 0;
	wcx.hInstance = hInstance;
	wcx.hIcon = NULL;
	wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcx.lpszMenuName = NULL;
	wcx.lpszClassName = CLASS_NAME;
	wcx.hIconSm = NULL;

...

Re: Problems with window creation

Posted: July 17th, 2017, 5:28 pm
by albinopapa

Code: Select all

//function prototypes
// This isn't necessary as it's already declared by the Win32 API
// int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
bool CreateMainWindow(HINSTANCE, int);

// I don't know what WINMAIN is but the standard is CALLBACK.  CALLBACK is a macro for _stdcall so you could also just use that.
//LRESULT WINMAIN WinProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);

//==============================================
//Window event callback function
//==============================================

//LRESULT WINMAIN WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_DESTROY:
		//Tell windows to kill this program
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

Re: Problems with window creation

Posted: July 17th, 2017, 5:40 pm
by albinopapa
@xu2201g
I believe I've read that it's never a good idea to cast the function pointer for WNDPROC. If it doesn't work for some reason it's because the signature of the function you are using doesn't match the signature of the function expected. In this case it's the missing _stdcall or CALLBACK calling convention. There's _cdecl and _stdcall among others.

Check this out for more info. I usually don't worry about it at all, except when dealing with the WinAPI.

Re: Problems with window creation

Posted: July 17th, 2017, 5:46 pm
by albinopapa
Might not matter too much, but you are missing the UnregisterClass function call to release the Window resources before you return msg.wParam.

Re: Problems with window creation

Posted: July 18th, 2017, 12:16 am
by MeleeMaster
Thanks for the help @xu2201g and @albinopapa, I've made the modifications and the program ran just fine. And thanks for the heads up on the _stdcall, CALLBACK calling conventions, I was clueless about that.