Why does WndProc() have to be global ??

Started by
10 comments, last by Sir_Spritely 20 years ago
You can make the callback a friend of the class. Doing so DOES NOT have the exact same limitations as a static member function. However doing so still requires the callback getting the instance of the class like a static member function would need to do, since the only instance info the callback gets is the HWND. This "problem" is not a big issue, since you can store the class instance in the window's user data field when the window is created. Then when handling any message other than WM_NCCREATE, get the instance from the window's user data field, and pass the callback parameters to the class's own wndproc. Of course, you can still call the classes own wndproc even when you get the WM_NCCREATE message, you just need to store the instance in the user data field before doing so.


[edited by - Mastaba on March 27, 2004 12:12:19 AM]
.
Advertisement
No, it''s very straightforward:
  1. Make your class provide a static MessageRouter function that will retrieve the appropriate instance and call that instance''s own window procedure.

  2. Inside the MessageRouter, handle the WM_NCCREATE message and store a pointer to the class instance - passed via the extra parameter (void *) afforded by CreateWindowEx - in the window userdata space (use either SetWindowLong or SetWindowPtr). For all other messages, retrieve the instance pointer using GetWindowLong or GetWindowPtr and call that instance''s window procedure.

  3. In the class constructor, fill the lpWndProc field of the WNDCLASS[EX] structure with the address of the (static) MessageRouter function.

Sit back and congratulate yourself for following Uncle ''Seyi''s 3-Step Program.

This topic is closed to new replies.

Advertisement