If you ever tried wrapping a window's functionality I bet the first
thing you asked yourself was: "What am I going to do with the window
procedure?". I also bet this was the first answer that crossed your
mind: "I'm just going to declare it as a member function within my
window wrapper class and then I'm gonna pass it to the WNDCLASSEX
structure as the lpfnWndProc
field."
Well, unfortunately that won't work. Why? Because member functions have an additional hidden parameter, the this
pointer, so instead of LRESULT CALLBACK wndProc (HWND, UINT, WPARAM, LPARAM)
your member function will be LRESULT CALLBACK memberWndProc (YourClassName *this, HWND, UINT, WPARAM, LPARAM)
.
Obviously you won't be able to do the assignment wcx.lpfnWndProc = memberWndProc
because the two function pointers are of different types.
So how do we solve this? We'll use a static member function that will act as a message router. A static member function can be called without creating an instance of the class, therefore it doesn't have the this pointer in its parameter list, allowing us to pass it as the lpfnWndProc member in WNDCLASSEX .
Message router you say? Yes, because the window procedure is going to be a static member function, we can't redefine its behavior in derived classes so we're gonna use a little hack. We're gonna declare another window procedure function that will be implemented in derived clasess and will do the actual message processing particular to each window. The static message router is going to determine which instance's window procedure to call by associating the window's HWND handle with a pointer to the derived class instance. This will be explained later.
4132




被折叠的 条评论
为什么被折叠?



