Jump to content
  • Advertisement
Sign in to follow this  
jdub

Help me with my Win32 code

This topic is 3689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I cant seem to get any of my win32 code to work. I am trying to write a Game Window class that encapsulates some win32 window functions and window initiallization. I am very new to win32 and I am having a ton of problems I have a function in my GameWindowManagerClass called CreateGameWindow: Header: HRESULT GameWindowManager::CreateGameWindow(HINSTANCE instance, BaseGameWindow** window, struct GameOptions* const options) the function is bug filled and I have managed to debug part of it but now I am stuck on a section that looks like this:
WNDCLASSEX wndClass;
		memset( &wndClass, 0, sizeof(WNDCLASSEX) );

		wndClass.hInstance = instance;
		wndClass.cbSize = sizeof(WNDCLASSEX);
		wndClass.lpfnWndProc = &(BaseGameWindow::WndProc);
		wndClass.lpszClassName = GameLib::WindowClassName;
		wndClass.style = CS_VREDRAW | CS_HREDRAW;
		
		if( FAILED(RegisterClassEx( &wndClass )))
		{
			throw("failed to register window class");
		}
		
		memset( &((*window)->windowProps) , 0, sizeof( (*window)->windowProps) );

		HWND hWnd = CreateWindowEx(0,
								   GameLib::WindowClassName,
								   _T("Window Name"),
								   WS_OVERLAPPEDWINDOW, 
								   CW_USEDEFAULT,
								   CW_USEDEFAULT, 
								   640,
								   800,
								   GetDesktopWindow(),
								   NULL,
								   instance,
								   0);

		if(!(*window)->windowProps.HWnd)
		{
			throw("HWnd not valid");
		}


I am pretty sure that the problem lies in the part where I call CreateWindowEx(). What is wrong with my code? EDIT: also, are there any ways to improve my style? I don't think that this line: memset( &((*window)->windowProps) , 0, sizeof( (*window)->windowProps) ); looks very good but I dont know how to avoid the nasty looking syntax.

Share this post


Link to post
Share on other sites
Advertisement
sorry I am really stupid. the real code is this:



(*window)->windowProps.HWnd = CreateWindowEx(0,
GameLib::WindowClassName,
_T("Window Name"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
800,
GetDesktopWindow(),
NULL,
instance,
0);



not


HWND hWnd = CreateWindowEx(0,
GameLib::WindowClassName,
_T("Window Name"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
800,
GetDesktopWindow(),
NULL,
instance,
0);




I was trying to debug it and forgot to destroy the temporary HWND. The problem still exists

Share this post


Link to post
Share on other sites
Encapsulating a Win32 window is not that complicated.
  1. Place an HWND in your class as a private member.

  2. Define an instance-specific window message handler as a normal object method.

  3. In your class constructor, fill out a WNDCLASS (or, preferably, a WNDCLASSEX) with the lpfnWndProc member assigned to a static function of your class. We will call this static function your router function. Then call CreateWindow (or, preferably, CreateWindowEx), passing this as the lpParam argument.

  4. In your router function, check if the current message is, say, WM_NCCREATE - really, any of the messages sent before WM_CREATE will do - and associate the pointer to your window class instance, the one passed in as the lpParam argument, with the HWND by calling SetWindowLongPtr. For all other messages, check if there is an associated handle (if the result of GetWindowLongPtr is not null, cast it to a pointer to your window class), and call the instance-specific message handler on it.


Here's an article covering one approach.
Here's another.

Share this post


Link to post
Share on other sites
Quote:
Original post by jdub
I am pretty sure that the problem lies in the part where I call CreateWindowEx().

What does CreateWindowEx return?
What does you window callback procedure look like?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!