4

朋友们,我正在尝试实现 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 类中的变量和成员函数。我想制作一个成员函数作为回调函数(事件处理程序)。我希望你明白我要解决的问题。请帮我。

4

1 回答 1

1

您在此示例中展示的主要思想是正确的。

你做了一些static WndProc函数和映射,映射HWND到你的类。

创建小部件的新实例时,将其添加到映射中。在销毁时,您将其从映射中删除。

在您的 WndProc 函数中,您从映射中获取类的实例并调用该实例的虚拟事件处理程序函数:

class WidgetBase
{
public:
    WidgetBase()
    {
        _handle = CreateWindow(/*...*/, &WidgetBase::MainProc, /*...*/);
        _widgets.insert(std::make_pair(handle, this);
    }
    virtual ~WidgetsBase() 
    {
       _widgets.remove(handle);
    }
protected:
    HWND _handle;
    virtual LRESULT handleEvents(UINT msg,WPARAM wParam,LPARAM lParam)
    {
         return DefWindowProc(_handle, hWnd,msg,wParam,lParam);
    }
private:
    static std::map<HWND, WidgetBase*> _widgets;

    static WidgetBase* GetWidgetPointerFromHWND(HWND handle)
    {
        // some error handling should be put there
        return _widgets[handle];
    }

    static LRESULT CALLBACK MainProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
        WidgetBase* wid=GetWidgetPointerFromHWND(hWnd);
        if (wid) {
            return wid->handleEvents(msg, wParam, lParam);
        }
        else {
            return DefWindowProc(hWnd,msg,wParam,lParam);
        }
    }
};
std::map<HWND, WidgetBase*> WidgetBase::_widgets;

然后在您的派生类中,您只需覆盖handleEvents函数:

class Derived: public WidgetBase
{
protected:
virtual LRESULT handleEvents(UINT msg,WPARAM wParam,LPARAM lParam)
    {
         // This is your event handler, that is memeber function
         //...
         return WidgetBase::handleEvents(msg, wParam, lParam);
    }
};
于 2012-11-18T19:17:23.933 回答