简体   繁体   English

从子类附加WNDPROC函数

[英]attaching WNDPROC function from child class

I have a problem with attaching a windows procedure to a window. 将Windows过程附加到窗口时出现问题。

I have a baseclass called BaseWindow , that uses GWPL_USERDATA to call a virtual function called HandleMessage() of the child classes. 我有一个名为BaseWindow的基类,该基类使用GWPL_USERDATA调用子类的虚拟函数HandleMessage()

However, if i try to change the window procedure without creating a custom Window Class, it gives a type error from the child procedure to long. 但是,如果我尝试改变窗口的程序,而无需创建一个自定义窗口类,它提供了从子过程的错误类型长。

Here's the code: 这是代码:

static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    BaseWindow *pThis = NULL;

    if (uMsg == WM_NCCREATE)
    {
        CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
        pThis = (BaseWindow*)pCreate->lpCreateParams;
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);

        pThis->m_hwnd = hwnd;
    }
    else
    {
        pThis = (BaseWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    }
    if (pThis)
    {
        return pThis->HandleMessage(uMsg, wParam, lParam);
    }
    else
    {
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
    {return 0;};


PlayList Class : BaseWindow

SetWindowLong(m_hwnd, GWL_WNDPROC,(long)  HandleMessage); //Error

LRESULT PlayList::HandleMessage(UINT message,WPARAM wParam,LPARAM lParam) //Need to     attach this window procedure
{}

It works if the child procedure is static, however I use non static members in that procedure. 如果子过程是静态的,则可以使用,但是我在该过程中使用了非静态成员。

I want to subclass a common control, while using this base class (because a lot of code is redundant), is it possible? 我想在使用此基类的同时子类化一个通用控件(因为很多代码是多余的),有可能吗?

Here's the whole code for the base class: http://pastebin.com/ME8ks7XK 这是基类的完整代码: http : //pastebin.com/ME8ks7XK

From MSDN: 从MSDN:

An application subclasses an instance of a window by using the SetWindowLong function. 应用程序使用SetWindowLong函数将窗口的实例子类化。 The application passes the GWL_WNDPROC flag, the handle to the window to subclass, and the address of the subclass procedure to SetWindowLong. 应用程序将GWL_WNDPROC标志,窗口的句柄传递给子类,并将子类过程的地址传递给SetWindowLong。 The subclass procedure can reside in either the application's executable or a DLL. 子类过程可以驻留在应用程序的可执行文件或DLL中。

So, you should write this: 所以,你应该这样写:
SetWindowLong(m_hwnd, GWL_WNDPROC,(long) & HandleMessage);

But this doesn't compile again! 但这不会再次编译! the reason: all non-static member functions have hidden this parameter (pointer to owner class). 原因:所有非静态成员函数都隐藏了this参数(指向所有者类的指针)。 So, you HandleMessage doesn't fit the WindowProc declaration. 因此,您的HandleMessage不适合WindowProc声明。

The compiler doesn't like your declaration for HandleMessage, it isn't static. 编译器不喜欢您对HandleMessage的声明,它不是静态的。 It's missing CALLBACK too, not good. 它也缺少CALLBACK,不好。

Not sure why you are trying to do this, the whole point of your WindowProc() function is to get the message forwarded to a virtual HandleMessage() method. 不知道为什么要尝试执行此操作,WindowProc()函数的重点是将消息转发到虚拟HandleMessage()方法。 At best you'd use WindowProc in your SetWindowLong() call instead of HandleMesssage. 充其量您应该在SetWindowLong()调用中使用WindowProc而不是HandleMesssage。 Or just specify it directly in the CreateWindowEx() call. 或者直接在CreateWindowEx()调用中指定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM