如何在WinApi C ++包装器中处理消息

时间:2015-11-22 16:52:47

标签: c++ winapi

我正在制作Windows API C ++包装器。头文件如下所示:

#include <windows.h>
#include <exception>
#include <stdexcept>
const int NOID = -1;

class inst {
    struct impinst;  // pimpl idiom
    impinst *imp;

    friend class win;   // win needs to see the private members of inst
                        // (namely, the WNDCLASS)
public:
    inst(const char *, HINSTANCE, int=NOID,
         HICON=LoadIcon(NULL, IDI_APPLICATION));

    ~inst();
};
class win {
    struct impwidget;  // pimpl idiom
    impwidget *imp;
public:
    win(inst &, const char *, int=0, int=0, int=600, int=450);
    void show(int);
    WPARAM msgpump();

    ~win();
};

// These are the object oriented message classes
// handler will be implemented by user of the library
class msg {
public:
    virtual void handler();
};
class movemsg : public msg {
public:
    void handler();
};
class sizemsg : public msg {
public:
    void handler();
};

实现(cpp)文件:

#include "winlib.h"
struct inst::impinst {
    WNDCLASS wc;
    static LRESULT CALLBACK winproc(HWND hwnd, UINT wm, WPARAM wp, LPARAM lp)
    {
        return DefWindowProc(hwnd, wm, wp, lp);
    }
    impinst(const char *classname, HINSTANCE hInst, int menuid, HICON hi)
    {
        this->wc.style = 0;
        this->wc.lpfnWndProc = this->winproc;
        this->wc.cbClsExtra = 0;
        this->wc.cbWndExtra = 0;
        this->wc.hInstance = hInst;
        this->wc.hIcon = hi;
        this->wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        this->wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
        this->wc.lpszMenuName = (menuid == NOID) ?
                                NULL : MAKEINTRESOURCE(menuid);
        this->wc.lpszClassName = classname;

        if (!RegisterClass(&this->wc))
            throw "Could not construct window instance";
    }
};
inst::inst(const char *classname, HINSTANCE hInst, int menuid, HICON hi)
{
    this->imp = new impinst(classname, hInst, menuid, hi);
}
inst::~inst()
{
    delete this->imp;
}


struct win::impwidget {
    HWND hwnd;

    impwidget(inst &i, const char *text, int x, int y, int width, int height)
    {
        this->hwnd = CreateWindow(i.imp->wc.lpszClassName, text,
                                  WS_OVERLAPPEDWINDOW, x, y, width, height,
                                  NULL, NULL, i.imp->wc.hInstance, NULL);
        if (this->hwnd == NULL)
            throw "Could not create window";
    }
};
win::win(inst &i, const char *text, int x, int y, int width, int height)
{
    this->imp = new impwidget(i, text, x, y, width, height);
}
void win::show(int cmdshow)
{
    ShowWindow(this->imp->hwnd, cmdshow);
    UpdateWindow(this->imp->hwnd);
}
WPARAM win::msgpump()
{
    MSG msg;

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
win::~win()
{
    delete this->imp;
}

我遇到了一个问题:如何将所有消息处理函数传递给我的inst类,以便它们可以在winproc函数中实现?现在,它是空的(只是调用DefWindowProc),但我需要它以某种方式获取handler函数的所有用户提供的实现,并将它们传递给winproc进行处理。我怎么能这样做?我是否需要将指针传递给msg类?

编辑:

我的问题有所不同,因为我的问题要求传递给lpParam CreateWindow的{​​{1}}参数,而不是如何问题

1 个答案:

答案 0 :(得分:4)

将数据传递到winproc回调有点迂回,但如果您注意到CreateWindow的文档,那么您可以使用lpParam参数来传递数据通过WM_CREATEWM_NCCREATE事件。

所以你可以这样做:

MyData* my_data = ...;
this->hwnd = CreateWindow(i.imp->wc.lpszClassName, text,
                          WS_OVERLAPPEDWINDOW, x, y, width, height,
                          NULL, NULL, i.imp->wc.hInstance,
                          my_data /* passed to WM_NCCREATE: */);

然后在winproc中,当您收到WM_NCCREATE消息时,我们可以使用SetWindowLongPtr将其与窗口句柄相关联,如下所示:

static LRESULT CALLBACK winproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == WM_NCCREATE)
    {
        MyData* my_data = (MyData*)(LPCREATESTRUCT(lParam)->lpCreateParams);
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)my_data);
    }
    ...
}

对于其他事件,您可以使用以下方法检索该数据:

LONG_PTR lpUserData = GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (lpUserData)
{
     MyData* my_data = (MyData*)(lpUserData);
     // do stuff with `my_data`
}

它非常迂回,所以将它包装起来并且可能在您通过winproc(或{{的回调之上制作自己的my_data方法非常方便{1}}可以是指向类的指针,您可以通过该方法调用方法。还要确保在窗口本身被销毁之前,my_data的指针被破坏。

这种环形交叉口性质是必需的,因为我们必须首先确保在调用my_data之前成功创建了窗口。最直接的地方是SetWindowLongPtr事件,我们通过WM_NCCREATE传递数据。然后,我们可以检索该数据并调用CreateWindow,并在后续事件中通过SetWindowLongPtr访问该数据。