Win32 C++ Create a Window and Procedure Within a Class

后端 未结 5 1791
刺人心
刺人心 2020-12-09 11:16

Pre-Text/ Question

I am trying to make a fairly simple tool to help debug variable values. For it to be completely self contained within the class i

5条回答
  •  轮回少年
    2020-12-09 11:54

    Unfortunately using an instance method as a C-style callback function for the WndProc won't work. At least not in any straight-forward way.

    The reason it doesn't work like that is that an instance method requires the this pointer to be passed in (to point to an instance) and that won't be correctly set by the code calling the WndProc. The Win32 API was originally designed with C in mind so this is one area where you have to use some work-arounds.

    One way to work around this would be to create a static method to serve as the window proc and dispatch messages to your class instances. The class instances would have to be registered (read added to a static collection) so the static method would know to dispatch WndProc messages to the instances. Instances would register themselves with the static dispatcher in the constructor and remove themselves in the destructor.

    Of course all the registration and unregistration and dispatching overhead is only necessary if your WndProc handler needs to invoke other instance member functions, or access member variables. Otherwise you can just make it static and you're done.

提交回复
热议问题