Jump to content

  • Log In with Google      Sign In   
  • Create Account


Non static winproc function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 lukesmith123   Members   -  Reputation: 153

Like
0Likes
Like

Posted 12 March 2012 - 05:18 PM

I'm trying to create a winproc function like this:

LRESULT CALLBACK Engine::WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(bStarted)
return MsgProc(hWnd, msg, wParam, lParam);
else
return DefWindowProc (hWnd, msg, wParam, lParam);
}

So that the function checks if a bStarted boolean in the Engine class is true before returning the MsgProc function.

The problem is that when I make WindowProc a function in the Engine class I get an error that the argument list is missing when I try to create the window. But if I make the WindowProc class static I dont have access to the bStarted var or the MsgProc function.

I looked in a couple of books but the books all make the main class a global singleton to make things simpler so I'm pretty stumped on how to do this.

Sponsor:

#2 Madhed   Crossbones+   -  Reputation: 2521

Like
2Likes
Like

Posted 12 March 2012 - 05:24 PM

You have to make it static or a free function. You can however pass the "this" pointer to the window when calling CreateWindow() (the lpParam)
Then when processing WM_CREATE set the pointer via SetWindowLongPtr().
When processing subsequent messages, you can get the pointer via GetWindowLongPtr().

#3 Ravyne   Crossbones+   -  Reputation: 6779

Like
1Likes
Like

Posted 12 March 2012 - 05:27 PM

Basically, you can't have a non-static WndProc by design. To get around this, its common practice to have a simple static WndProc that simply forwards to the member WndProc of the owning class.

There is plenty of coverage of this online, but the gist of it is that when you create your window, there's an parameter in the window (possibly the Ex version) data structure that you can use to pass around user-defined data, and that data is provided to the first message that your WndProc receives -- If you pass a pointer to the class that has the member WndProc, then the static WndProc function can cache this pointer and use it to call the member WndProc.

EDIT - MadHead nicely fills in the rest of the details :)

#4 Chris_F   Members   -  Reputation: 1989

Like
1Likes
Like

Posted 12 March 2012 - 05:27 PM

The pointer you pass to your window class (the WndProc) has to be to a free function. Member function pointers are not the same thing and are incompatible.

You can get around this by having one static WndProc that receives a pointer to your class and then re-dispatches to a second non-static WndProc inside your class instance.

#5 lukesmith123   Members   -  Reputation: 153

Like
0Likes
Like

Posted 12 March 2012 - 06:07 PM

Ah thats brilliant thank you!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS