I am trying to program my own custom controls in C++ in the old, traditional style (using WNDPROCS, etc.), and I don’t want to use ActiveX. I’d like to encapsulate my control code into a class, but I can’t make the window procedure a member function of my class! How do I do this?
You can’t make your member functions a window procedure because member functions need to know what object they are being called for. C++ automatically passes this information with each call to a member function, but Windows won’t send this information when calling WinProc.
There is an easy solution, however. Make your WinProc member function static, or even a non-class function. Static member functions can be called like regular functions.
The only disadvantage to this is that static member functions cannot directly access object data. To handle this, your WinProc must either somehow look up an object pointer associated with the window handle, or store the pointer as part of the window’s data.
Yet another approach is simply to base your window class on the MFC CWnd class and have the framework handle the above logic for you. However, this approach does have limitations. For example, MFC allows only one class to be associated with a given window. If you were to use your control class and a class that interfaces with the control in the same EXE file, you would be attempting to associate two CWnd classes with the same window and the result would be a conflict.