• Advertisement

Archived

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

Help with an object oriented window class

This topic is 5525 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 am making myself a window class as part of an engine, but it gives a runtime error. From debug information, it screws up at CreateWindowEx(). Does anyone know what I have done wrong? Thanks for your consideration.
    
	/////////////////////////////////////////////////////////////////////////

	// Name: Create()

	// Desc: Create() is a function to create a window.

	// Inpt: Width, height, fullscreen?, icon, handle to instance

	/////////////////////////////////////////////////////////////////////////

	int CWindow::Create(int width, int height, bool fullscreen, char *icon, HINSTANCE hInstance)
	{
		/////////////////////////////////////////////////////////////////////////

		// The style variable declared here will make the window a popup window

		// if the fullscreen variable is selected; if not, it will make an

		// overlapped window.

		/////////////////////////////////////////////////////////////////////////

		int style;

		if(fullscreen)
		{
			style = WS_POPUP;
		}
		else
		{
			style = WS_OVERLAPPEDWINDOW;
		}

		/////////////////////////////////////////////////////////////////////////

		// Fill in the window class with the values desired to make the window.

		/////////////////////////////////////////////////////////////////////////

		m_cWC.cbSize	    = sizeof(WNDCLASSEX);
		m_cWC.style		    = CS_HREDRAW | CS_VREDRAW;
		m_cWC.lpfnWndProc   = (WNDPROC)WndProc;
		m_cWC.cbClsExtra    = 0;
		m_cWC.cbWndExtra    = 0;
		m_cWC.hInstance     = hInstance;
		m_cWC.hIcon         = LoadIcon(NULL, icon);
		m_cWC.hCursor       = LoadCursor(NULL, IDC_ARROW);
		m_cWC.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
		m_cWC.lpszMenuName  = NULL;
		m_cWC.lpszClassName = "CWINDOW";
		m_cWC.hIconSm       = LoadIcon(NULL, icon);

		/////////////////////////////////////////////////////////////////////////

		// Register the class.

		/////////////////////////////////////////////////////////////////////////

		if(!RegisterClassEx(&m_cWC))
		{
			MessageBox(m_hWnd, "Could not register the window class.", "Error", MB_OK);

			return 1;
		}

		/////////////////////////////////////////////////////////////////////////

		// Create the window.

		/////////////////////////////////////////////////////////////////////////

		if(!(m_hWnd = CreateWindowEx(NULL,
								     "CWINDOW",
									 "CWindow",
									 style,
									 0, 0,
									 width, height,
									 NULL,
									 NULL,
									 hInstance,
									 NULL)))
		{
			MessageBox(m_hWnd, "Could not create the window.", "Error", MB_OK);

			return 1;
		}

		return 0;
	}
    
P.S. The problem is not from the whole window procedure thing, I have that cleared up. [edited by - elendil67 on December 1, 2002 5:09:25 PM]

Share this post


Link to post
Share on other sites
Advertisement
I don''t think it is returning - he said he gets a runtime error, and the debugging screws up at CreateWindowEx (i assume he means "it crashes there")

Share this post


Link to post
Share on other sites
Yes, I know my sig sucks. I''m trying to figure out how to replace it with a sig half the size.

I get an access violation right in CreateWindowEx(). I have no idea what it is. Thanks for your consideration.

Share this post


Link to post
Share on other sites
Does it work if you set the WndProc to DefWindowProc ? (I know you said its not, but thats all i can see could cause the problem)
I'd have to say that the suspect here if your WinProc - BTW, you shouldn't cast function pointers, if the compiler objects to it without the cast, then theres a 99% chance thats what the problem is.




[edited by - Evil Bill on December 1, 2002 7:16:46 PM]

Share this post


Link to post
Share on other sites
"m_cWC.hIcon = LoadIcon(NULL, icon);"

Is the first parameter really supposed to be NULL
if you want to load an icon different then the system ones ...

... don''t want to look it up right now, mainly
i''m under linux right now, and looking up windowsbecause functions under linux isn''t very good for my mood

Share this post


Link to post
Share on other sites
Yeah, the first parameter should be NULL for system icons, but if hes using a non-system icon, LoadIcon will fail, and return NULL - which is a valid value for the hIcon member of WNDCLASSEX

Share this post


Link to post
Share on other sites
About the sig, just use the Width= and Height= values of your image.
Here: <img src="http://www.gras-wien.gruene.at/pics/napster.gif" border=0 width=100 height=135> =

Edit: oops, i broke it :s




[edited by - Evil Bill on December 1, 2002 7:51:11 PM]

Share this post


Link to post
Share on other sites
Forget the sig... do you guys no whats wrong??? Yes, I am using system icons. Thanks so far for your help

Share this post


Link to post
Share on other sites
quote:
Original post by elendil67
do you guys no whats wrong?

Of course not, you''ve given us nothing with which to diagnose your problem. What is the exact error ("a runtime error" is not exact)? What are the values of the various parameters when the error occurs?

Ask a good question if you want good answers.

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
Does it work if you set the WndProc to DefWindowProc? (I know you said its not, but thats all i can see could cause the problem)...



Edit: "Forget the sig..." I wish we could



[edited by - Evil Bill on December 3, 2002 10:27:55 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
listen to Evil Bill and Oluseyi and everyone else.

* post the error message. you should be using a call to FormatMessage with GetLastError to get a meaningful error message rather than your generic and useless error message. see the MSDN docs on FormatMessage for an example.
* post the code of the calling function.
* post the code to your static WndProc. why are you type casting your WndProc to a WNDPROC?
* post the code to your class non-static WndProc.

are you using a singleton class instance or not? if not, post the code that attempts to get your class instance pointer to your static WndProc because your not using the "standard" method for doing so.

there''s a lot that goes on behind the scenes when creating a window. and not all of it happens in the CreateWindow/Ex function. there are window messages that are queued up and handled, particularly WM_NCCREATE and WM_CREATE, which also could be part of the problem.

Share this post


Link to post
Share on other sites
Well I can't answer the question either, but the signature drama playing out here is irresistable.

Might I recommend:

Reduced Communism

I'm no Photoshop god, but still.

[edited by - Reaptide on December 3, 2002 1:06:33 PM]

Share this post


Link to post
Share on other sites

  • Advertisement