-
this pointer to access
in my window creation class, i have a static WndProc which will be used for adding events. i read here http://www.mvps.org/windev/cpp/class.html that by passing the this pointer to the lParam of CreateWindow, i can then retrieve that pointer when i handle WM_CREATE in my WndProc, and store it.. after i can cast it back to the class, and use the pointer to access and modify members...the problem is that everytime i try to actually modify a member, the program crashes..could anybody help me?
thanks
Amon Ra
-
ok so here is my wndproc, which is a static member of the WINDOW class
PHP Code:
//WindowProc: this is the real one...the one that does all the work
LRESULT CALLBACK WINDOW::WindowProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
WINDOW *const ptThis = (WINDOW*) lParam; //store "this" in variable;
switch (Msg)
{
case WM_CREATE:
SetWindowLong (hWnd, GWL_USERDATA, (LONG) lParam); //stores "this" on window data just in case
return 0;
case WM_LBUTTONDOWN:
MessageBox (hWnd, ptThis->m_pcText , "BLAH", MB_OK | MB_ICONINFORMATION);
return 0;
case WM_PAINT: // this must be handled
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY: // must also be handled in the main window
PostQuitMessage(0);
return 0;
}
return DefWindowProc (hWnd, Msg, wParam, lParam);
} //WindowProc
to have the "this" pointer stored in the lParam, i used:
PHP Code:
hWnd = CreateWindowEx ( m_dwExStyle,
m_pcClassName,
m_pcText,
m_dwStyle,
m_XPos, m_YPos, m_Width, m_Height,
m_hParent,
NULL,
m_hInstance,
this );
The problem is that when i get to
PHP Code:
MessageBox (hWnd, ptThis->m_pcText , "BLAH", MB_OK | MB_ICONINFORMATION);
it crashes the program. anyone have any idea? thanks in advance
Amon Ra
-
any body please? basically i want a class with a windowproc in it, so that the user has no acces to it
-
great! i got it..after adjusting a few things.. =)
-
Not sure if you already have the solution...
the lParam passed to the WM_CREATE message is not the lParam you passed to CreateWindow, but rather the address of a CREATESTRUCT.
-
yea exactly..i found that out in the msdn... i did this
PHP Code:
pThis = (WINDOW*) ((LPCREATESTRUCT)lParam)->lpCreateParams;
and i passed the "this" to the lParam of CreateWindow
-
You didn't notice the code I posted ages ago about this then? :D
Everyone and his dog seems to be writing window-wrapper classes at the moment, strange really.
I want .NET *sigh* Was so much easier to use :D
-
lol..no i didn't see it..i ma writing this for the info tech class at our school (high school)... =)
-
The problem of passing the class as create parameter and saving it in a static variable of the WndProc is that every new class will replace the last! Keep that in mind! (someone here did it that way)
-
Use a method like the SetWindowLong(hWnd, GWL_USERDATA, this) one.
I've posted about that one a LOT now...so I won't repeat it ;)
-
ok apparently, as you said CornedBee, when everything is created , and i use the pointer to this, i get an error..
i am gonna see how i can fix, and try to find yours Parksie..
thanks =
Amon Ra
-
i fixed it and it works beautifully now :)
Amon Ra