Jump to content
  • Advertisement
Sign in to follow this  
dave

MsgProc() problem

This topic is 4844 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

This is something that has bothered me on many occasions. What i want to do is have the MsgProc function as a member function of my kernel class, so that on certain messages the kernel can do what it needs to. I obviously cant call kernel methods unless they are static (which theyre not) or the method is from an object. How do i do this? ace

Share this post


Link to post
Share on other sites
Advertisement
The way to properly encapsulate the window procedure is a tad tricky,
but here is the best way I know of to do it, using the source code I have.

this is in the function of my window class (Window) that creates the window:

HWND hWnd = CreateWindow(WindowClass.lpszClassName, WindowTitle, WS_OVERLAPPEDWINDOW|WS_VISIBLE,
0,0, 800, 600, 0,0, InstanceHandle, static_cast<LPVOID>(this));


What this does is store a pointer to the Window class object in the
createstruct passed to the wndproc when the WM_CREATE message is sent

now in the static Window Procedure function you have:


LRESULT CALLBACK Window::WndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
if( uMessage == WM_CREATE )
{
CREATESTRUCT* cs = (CREATESTRUCT*)lParam;

// I originally had pragmas around the two reinterpret_casts
//to disable warnings, I don't remember if they are needed
if( SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(cs->lpCreateParams)) )
throw C3DGblObj::Exception("Could not set window user data");
}

Windows::Window* LongWindow = reinterpret_cast<Windows::Window*> GetWindowLongPtr(hWnd, GWLP_USERDATA));

if (LongWindow)
{
switch( uMessage )
{
case WM_MOUSEMOVE:

LongWindow->OnMouseMove(wParam, lParam);

break;
}
}

return DefWindowProc(hWnd, uMessage, wParam, lParam);
}





What this code does is when WM_CREATE is handled, it retrieves the Window
object pointer and stores it in the user data section of the window, then
each time the window procedure is called, it retrieves the window object
pointer for the window. Now you can call any functions you need from within
the static window procedure, while guaranteeing you have the correct window
object pointer for the window you are handling.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!