如何将Win32 WndProc包装到C ++类中? [英] How to wrap a win32 WndProc into a C++ class?

查看:117
本文介绍了如何将Win32 WndProc包装到C ++类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这甚至有可能吗?例如,假设我有以下内容:

class Window {
private:
    WNDCLASSEX wc;
public:
    inline WNDCLASSEX getWindowClass() {
        return wc;
    }
    Window();
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, LPARAM lParam, WPARAM wParam);
}

void RegisterWindow(Window win) {
    WNDCLASSEX* wc = win.getWindowClass();
    RegisterClassEx(wc);

}

现在,在某处将有一个节(可能在Window类的构造函数中,有必要在其中分配WNDCLASSEXWndProc,这在Window类中有所说明.)唯一的问题是,因为它是类的一部分,所以会引发错误,因此,如何实现呢?它是静态的吗?如果我在课程之外创建它,那只会消除这一点.

解决方案

您将this指针作为GWLP_USERDATA传递给SetWindowLongPtr,这实际上使您可以简单地将free函数转发给成员函数.

Is this even possible? For example, let's say I have the following:

class Window {
private:
    WNDCLASSEX wc;
public:
    inline WNDCLASSEX getWindowClass() {
        return wc;
    }
    Window();
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, LPARAM lParam, WPARAM wParam);
}

void RegisterWindow(Window win) {
    WNDCLASSEX* wc = win.getWindowClass();
    RegisterClassEx(wc);

}

Now, somewhere there is going to be a section (probably in the constructor of the Window class, where it's necessary to assign the WNDCLASSEX a WndProc, which is noted in the Window class. The only issue is that, because it's a part of a class, there an error will be raised. Thus, how is this achieved? Is it made static? Even so, if the class wraps it it is still part of the class in some way. If I create it outside of the class, that simply obliterates the point.

解决方案

You pass the this pointer as GWLP_USERDATA to SetWindowLongPtr, which effectively allows you to simply forward the free function to the member function.

这篇关于如何将Win32 WndProc包装到C ++类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆