Archived

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

xDS4Lx

window creation in a class

Recommended Posts

Ok im having a little trouble here. First I got my class up and running, CWindow (so origional huh?). I made WndProc static and it ran, but now when i try to call a method in WndProc using this->somefunction() it gives me 2 errors, the first: error C2671: ''WindowProc'' : static member functions do not have ''this'' pointers and the second one i know what it is but thats wont be cleared up till this is. Thanks in advance.

Share this post


Link to post
Share on other sites
Exactly as it says, static functions do not have a ''this'' pointer. Only a non-static member function does. There are various ways to get access to a class in a static member. One is to pass in a pointer to the class as an argument to the static function and another is to use a global variable for the object you wish to access.

Share this post


Link to post
Share on other sites
''this'' is a pointer to a CWindow object for which your CWindow method was called. Since the method you called is static, meaning it can be called without first instantiating a CWindow object, there is no ''this''.

Share this post


Link to post
Share on other sites
Ive searched the board looking for the solution to this and it doesnt work. I tried using SetWindowLong(hWnd,GWL_USERDATA,(long)this); after calling CreateWindowEX(........), and in WndProc(......) calling CWindow = (CWindow*)GetWindowLongPtr(hWnd,GWL_USERDATA); but it doesnt seem to be working. Ill post the code:

  
CConvWindow::CConvWindow(const HINSTANCE& hInstance, const UINT& style, const HBRUSH& hBackground,
const HICON& hIconSmall, const HICON& hIconLarge, const HCURSOR& Cursor)
{ bInitFail = false;
hInst = hInstance;

wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = (HBRUSH)hBackground;
wndClass.hIcon = hIconLarge;
wndClass.hIconSm = hIconSmall;
wndClass.hInstance = hInstance;
wndClass.hCursor = Cursor;
wndClass.lpfnWndProc = &(this->WindowProc);
wndClass.lpszClassName = "CBWindow";
wndClass.lpszMenuName = NULL;
wndClass.style = style;

if(!RegisterClassEx(&wndClass))
{ MessageBox(NULL,"Error in application startup","Error In Registering Window Class",MB_OK);
bInitFail = true;
}

if(!(bInitFail) && !(hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,"CBWindow","CBWindow Window",WS_OVERLAPPED | WS_SYSMENU, 50, 50, 300, 200, NULL, NULL,
hInstance, NULL)))
{ MessageBox(NULL,"ERROR IN APPLICATION STARTUP","Error In Creating Window",MB_OK);
bInitFail = true;
}

SetWindowLongPtr(hWnd,GWL_USERDATA,(long)this);

ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
}



and

  
LRESULT CALLBACK CConvWindow::WindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{ static CConvWindow* cWnd;

switch(Msg)
{ case WM_CREATE:
{ cWnd = (CConvWindow*)GetWindowLongPtr(hWnd,GWL_USERDATA);
if(!cWnd)
{ MessageBox(NULL,"ERROR","Error Passing this pointer",MB_OK);
//PostMessage(hWnd,WM_CLOSE,0,0);

}
break;
}
case WM_CLOSE:
{ DestroyWindow(hWnd);
break;
}
case WM_DESTROY:
{ PostQuitMessage(0);
break;
}
default:
{ return(DefWindowProc(hWnd,Msg,wParam,lParam));
}
} // end of switch(Msg)

return(0);
}



This is what i found on other posts but it fails and the message box pops up letting me know it didnt work. Anyone know how to pass the this pointer to my WindowProc?

Edited by - xds4lx on February 19, 2002 12:25:13 AM

Share this post


Link to post
Share on other sites
In the MFC classes, which do essentially what you''re trying to do, all of the Windows wrapper classes have a constructor and then a separate Create method. I''m not sure, but I think that the way you''re doing it is causing Windows messages to be sent before your ''this'' pointer is valid.

Take care,
Bill

Share this post


Link to post
Share on other sites