Getting a Window to Work

Started by
7 comments, last by Lordimm 15 years, 4 months ago
I just bought a book about game development and it's talking about how to create a window in Windows, but it's just not specific enough and I have no clue how to get it to work. I looked around on the internet and it's just as confusing as this book. Here's what I got from it. WinMain.cpp #include "Library.h" ATOM RegisterClassEx(CONST WNDCLASSEX *lpwcx); BOOL UnregisterClass( LPCTSTR lpClassName, HINSTANCE hInstance); int WINAPI WinMain(HINSTANCE hInstace, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex = { sizeof(WNDCLASSEX), CS_CLASSDC, WindowProc, OL, OL, hInstance, NULL, NULL, NULL, NULL, "GameClass", NULL }; //Register the class and exit on error if(!RegisterClassEx(%wcex)) return FALSE; //Do other application stuff here //Unregister class UnregisterClass("GameClass", hInstance); //Exit application return 0; } Window.h #include "Library.h" typedef struct _WNDCLASSEX { UINT cbSize; //Size of this structure UINT style; //style of window WNDPROC lpfnWndProc; //Window message procedure int cbClsExtra; //0 int cbWndExtra; //0 HANDLE hInstance; //Instance handle from WinMain HICON hIcon; //Handle to application icon HCURSOR hCursor; //Handle to application cursor HBRUSH hbrBackground; //Handle to background brush LPCTSTR lpszMenuName; //Handle to application menu LPCTSTR lpszClassName; //Class name HICON hIconSm; //Handle to small application icon } WNDCLASSEX; Library.h just holds #include <windows.h> Can someone help me with this, I'm getting a little frustrated =P.
Advertisement
It's been a very long time since I've done much windows-ing in Win32 - lately I've confined myself to the details of the win32 console - but I don't think you need to re-declare the function prototypes of RegisterClassEx and UnregisterClass. They're already declared in windows.h, which you're including in Library.h. For that matter, you don't need to redefine the WNDCLASSEX struct either, if I'm not mistaken. It's all already defined in windows.h. I suspect the book you're using only posted the prototypes and definitions to show you what the functions take as parameters and return, and what members are in the struct when you use it.

Disclaimer: Like I said, it's been a long time since I've done windows in Windows.
I deleted everything but

#include "Library.h"

int WINAPI WinMain(HINSTANCE hInstace,
HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcex = { sizeof(WNDCLASSEX), CS_CLASSDC,
WindowProc, OL, OL, hInstance,
NULL, NULL, NULL, NULL,
"GameClass", NULL };

//Register the class and exit on error
if(!RegisterClassEx(%wcex))
return FALSE;

//Do other application stuff here

//Unregister class
UnregisterClass("GameClass", hInstance);

//Exit application
return 0;
}

library still has #include <windows.h>

These are the errors I got:

Compiling...
WinMain.cpp
c:\documents and settings\owner\my documents\visual studio 2008\projects\rpg3\rpg3\winmain.cpp(8) : error C2065: 'WindowProc' : undeclared identifier

c:\documents and settings\owner\my documents\visual studio 2008\projects\rpg3\rpg3\winmain.cpp(8) : error C2065: 'OL' : undeclared identifier

c:\documents and settings\owner\my documents\visual studio 2008\projects\rpg3\rpg3\winmain.cpp(8) : error C2065: 'OL' : undeclared identifier

c:\documents and settings\owner\my documents\visual studio 2008\projects\rpg3\rpg3\winmain.cpp(8) : error C2065: 'hInstance' : undeclared identifier

c:\documents and settings\owner\my documents\visual studio 2008\projects\rpg3\rpg3\winmain.cpp(13) : error C3071: operator '%' can only be applied to an instance of a ref class or a value-type

c:\documents and settings\owner\my documents\visual studio 2008\projects\rpg3\rpg3\winmain.cpp(13) : error C2664: 'RegisterClassExA' : cannot convert parameter 1 from 'WNDCLASSEX' to 'const WNDCLASSEXA *'

No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

c:\documents and settings\owner\my documents\visual studio 2008\projects\rpg3\rpg3\winmain.cpp(19) : error C2065: 'hInstance' : undeclared identifier
Quote:'WindowProc' : undeclared identifier

When you initialize your WNDCLASSEX, you set the member lpfnWndProc to a function whose signature matches. That function is your WindowProc, but you haven't actually defined one. In C and C++, you have to at least declare a variable, class or function before you can refer to it.

Also, it's really not a great idea to perform single-line member-wise initialization.

Quote:'OL' : undeclared identifier

That 'O' is supposed to be a zero - 0L.

Quote:'hInstance' : undeclared identifier

You have a typo in your WinMain: you name the parameter hInstace (note the missing 'n').

Quote:operator '%' can only be applied to an instance of a ref class or a value-type

You wanted &wcex, not %wcex.

Quote:'RegisterClassExA' : cannot convert parameter 1 from 'WNDCLASSEX' to 'const WNDCLASSEXA *'

The previous solution will fix this.
Check out YouTube for an okay guide to getting a window, then D3D moving.
Search for "DirectX tutorial" or the user "BenWells3D".

I've changed my structure a lot compared to his, but he gives clear, step-by-step, guidance... on video.
Ok, thanks for pointing out all the things wrong with my code. I probably shoulda read through it more thoroughly...heh. But can you elaborate on the first error you pointed out to me? Specifically:

"When you initialize your WNDCLASSEX, you set the member lpfnWndProc to a function whose signature matches. That function is your WindowProc, but you haven't actually defined one. In C and C++, you have to at least declare a variable, class or function before you can refer to it.

Also, it's really not a great idea to perform single-line member-wise initialization."

Thanks in advance =)
The windows procedure is the function that is called for every single message that windows recieves form your window, resizeing, clicks, menus, etc, the windows procedure(usually called WndProc) looks liek this


LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);


the WNDCLASSEX struct has a member that is a pointer to a function that has the same "format" or signature as the one i showed, so you create a function liek the one above, and give that member the name of the function
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
and what he meant by not initializing a struct in a simple line is, that it gets really confusing when you do this

WNDCLASSEX wc = {0,0,0,0,0,0,0,0,0}


because there are so many members, it's a lot clearer to do it member by member like this


WNDCLASSEX wc; //declare a WNDCLASSEX
SecureZeroMemory(&wc,sizeof(WNDCLASSEX))
//initializes all members to 0, this is good in case you forget an optional
//member, it won't try to work with garbage vlaues




wc.size = sizeof(WNDCLASSEX);
wc.lfpWndProc = WndProc;
wc.style = etc....


--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Lol, I'm sorry but...I'm still not getting it...what should the entire thing be without any extra stuff be to just get this to work without any errors? =P I'm getting a little confused...

This topic is closed to new replies.

Advertisement