Sign in to follow this  
blaklite

Simple CreateWindow question

Recommended Posts

I am really sorry for posting this but, I have been tearing my hair out wondering why it wont work for the past 2 days and i'm going round in circles. Im using a bit of code I found to help me with classes and using them in a "proer" program. neway, the code I have found will compile no problem with all its super duper thread handling and wizzy gfx bits. BUT all I am interested to get workng is the &*%$'ing bit where the main window is in its own class. My CreateWindow just keeps returning NULL, and I cant figure out why. I have uploaded the solution/files if anyone can be bothered. http://www.sendspace.com/file/8a5i2t The bit I cant understand is why the dude is using 2 WndProc's ? And what is all the fiddling around with: [CODE} if(message != WM_NCCREATE) return DefWindowProc(hWnd, message, wParam, lParam); pCS = (CREATESTRUCT*)lParam; mypThis = (cMainWindow*)pCS->lpCreateParams; mypThis->m_hWnd = hWnd; //WHY???!!! SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)mypThis); return mypThis->WndProc(message, wParam, lParam); [/CODE] Of course his code will work fine while mine won't when I have basically copy pasted most of it ( well the first 1% of the full thing neway). My RegisterClassEx returns a value (which I assume means it didnt fail). The only thing I can think of is that im not seeing some sort of setup going on in some other class, that might pass back some value I may need, but like I say iv'e been thinking about it for 2 days and im running out of ideas. Many thanks, B. BTW I have also put up the original code :- http://www.sendspace.com/file/7rjc5n Just if ur interested.

Share this post


Link to post
Share on other sites
The main problem is the default case in _WndProc.

PUT THE DefWindowProc call AFTER THE SWITCH. Always. No matter what a Win32 tutorial says.

Also, you must return the return value of DefWindowProc from your _WndProc.


LRESULT cMainWindow::WndProc( UINT message, WPARAM wParam, LPARAM lParam )
{
bool bOk;
PAINTSTRUCT ps;
HDC hdc;

switch( message )
{
case WM_CREATE:
{
// Kick off tasks
// open a gfx window m_gfx.Open(hWnd); or similar ?
// init sound
// open frame ??

// start timer for window
SetTimer( m_hWnd, 100, USER_TIMER_MINIMUM, NULL ); // A NULL specifies a WM_TIMER message be sent with the
// HWND portion of the message struct containing out hWnd
} break;

case WM_PAINT:
{
hdc = BeginPaint(m_hWnd, &ps);
// Ignore draw ???
EndPaint(m_hWnd, &ps);

} break;

case WM_ERASEBKGND:
{ /* Ignore background erasing */
} break;


case WM_SETCURSOR:
{
SetCursor(NULL);
} return true;

case WM_TIMER:
case WM_SIZING: // Keeps window updated nicely while sizing
{
DefWindowProc(m_hWnd, message, wParam, lParam);
//bk = Frame(); // proccess a fram ??

if(GetKeyState(VK_ESCAPE) & 0x80)
DestroyWindow(m_hWnd);
} break;

case WM_DESTROY:
{
Destroy();
PostQuitMessage(0);
} break;
}

return DefWindowProc(m_hWnd, message, wParam, lParam);
}


The part you marked with why:

The _WndProc will already receive several messages during the CreateWindow call. He obviously wants to catch as much messages as possible with the classes WndProc. So he assigns the hwnd as soon as possible.

He uses two wndproc because:
The wndproc parameter passed to RegisterClass can't be a class member. So he uses a static _WndProc which then tries to retrieve a pointer to the class instance. With this he can call the class' WndProc.

Share this post


Link to post
Share on other sites
OMG, thanks you!

One change from a Return 0; to a DefWindowProc(..); and it worked =)

(Although his one still works, while I gotta change mine, nevermind IT WORKS!!)

Thanks for taking the time!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this