朋友们,我正在尝试实现 Windows API 的包装,并且我想从父窗口捕获子窗口事件,所以我做了一个简单的事件处理程序。我使用函数指针来存储回调函数。我用静态函数做到了。请参阅下面的代码。
class Widget;
typedef void (*EventProc)(MSG* EventArgs);
class Widget
{
public:
/// Constructors destructor and methods for Registering and Creating Windows
static LRESULT CALLBACK MainProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
MSG struct_msg;
struct_msg.hWnd=hWnd;
struct_msg.message=msg;
struct_msg.wParam=wParam;
struct_msg.lParam=lParam;
Widget* wid=(Widget*)GetWidgetPointerFromHWND(hWnd);
switch(msg)
{
case WM_CREATE:
if(Created!=NULL)
(*(wid->Created))(&struct_msg);break;
case WM_DESTROY:
if(Destroyed!=NULL)
(*(wid->Destroyed))(&struct_msg);break;
default:
return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0;
}
EventProc Created;
EventProc Destroyed;
};
class CustomControl: public Widget
{
/// Constructor destructor and other methods
};
class Window: public Widget
{
public:
static void ChildCreated(Widget* Sender,Widget* Self,MSG* EventArgs)
{
MessageBox(0,0,0,0);
}
Window()
{
control1=new CustomControl(100,100,200,200); //left,top,width,height
this->AddChild(control1);
control1->Created = ChildCreated;
}
private:
CustomControl control1;
};
这可行,但由于静态函数没有 this 指针,我无法访问Window 类中的变量和成员函数。我想制作一个成员函数作为回调函数(事件处理程序)。我希望你明白我要解决的问题。请帮我。