1

我创建了一个 Subclass 类,并在其中SetWindowLongPtr()设置GWLP_USERDATA为包含指向派生类的指针,派生类用于分发消息。

因此,如果我使用SetWindowLongPtr(CurrentWindow, GWLP_USERDATA, (LONG_PTR)Data);,当我不再想子类化时,如何恢复旧数据?MSDN说初始值为零,所以我应该使用SetWindowLongPtr(CurrentWindow, GWLP_USERDATA, (LONG_PTR)NULL);函数吗?

编辑:我实际上使用 SetProp、GetProp、RemoveProp 找到了解决此问题的方法,现在我不必担心会弄乱其他功能。贝娄是我为可能需要它的人提供的代码:

#ifndef WIN32_SUBCLASS_CLASS_H
#define WIN32_SUBCLASS_CLASS_H

#include <Windows.h>
#include <tchar.h>

class SubclassWindow
{
public:
    static LRESULT CALLBACK stWinSubclassHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        SubclassWindow* pWnd;
        pWnd = (SubclassWindow*)GetProp(hwnd, TEXT("Subclass"));

        if (pWnd)
            return pWnd->WinSubclassHandler(hwnd, uMsg, wParam, lParam);
        else
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    BOOL Subclass(SubclassWindow* Data, HWND hwnd)
    {
        if (Subclassed == TRUE || CurrentWindow != NULL)
            return 0;

        CurrentWindow = hwnd;
        Subclassed = TRUE;
        OriginalProc = (WNDPROC)GetWindowLongPtr(hwnd, GWL_WNDPROC);
        SetProp(CurrentWindow, TEXT("Subclass"), (HANDLE)Data);
        SetWindowLongPtr(CurrentWindow, GWL_WNDPROC, (LONG_PTR)SubclassWindow::stWinSubclassHandler);

        return 1;
    }
    BOOL RemoveSubclass()
    {
        if (OriginalProc == NULL || CurrentWindow == NULL)
            return 0;

        Subclassed = FALSE;
        RemoveProp(CurrentWindow, TEXT("Subclass"));
        SetWindowLongPtr(CurrentWindow, GWL_WNDPROC, (LONG_PTR)OriginalProc);

        return 1;
    }

    BOOL IsSubclassed() { return Subclassed; }
protected:
    HWND CurrentWindow;
    WNDPROC OriginalProc;
    BOOL Subclassed;

    SubclassWindow()
    {
        CurrentWindow = NULL;
        OriginalProc = NULL;
        Subclassed = FALSE;
    }

    virtual LRESULT CALLBACK WinSubclassHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;
};

#endif
4

2 回答 2

4

SetWindowLongPtr()返回旧值,因此请保存它,然后在您SetWindowLongPtr()稍后调用以删除子类时恢复它。

话虽如此,替换GWLP_USERDATA不是您自己创建的窗口是危险的。您不知道该窗口是否已GWLP_USERDATA用于其自身目的。改为使用SetWindowSubclass(),旨在解决该问题:

class SubclassWindow
{
public:
    static LRESULT CALLBACK stWinSubclassHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
    {
        SubclassWindow* pWnd = (SubclassWindow*) dwRefData;    
        if ((pWnd) && (uIdSubclass == 1))
            return pWnd->WinSubclassHandler(hwnd, uMsg, wParam, lParam);
        else
            return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }

    BOOL Subclass(SubclassWindow* Data, HWND hwnd)
    {
        if (CurrentWindow != NULL)
            return FALSE;

        if (!SetWindowSubclass(hwnd, &stWinSubclassHandler, 1, (DWORD_PTR)Data))
            return FALSE;

        CurrentWindow = hwnd;
        return TRUE;
    }

    BOOL RemoveSubclass()
    {
        if (CurrentWindow == NULL)
            return FALSE;

        if (!RemoveWindowSubclass(CurrentWindow, &stWinSubclassHandler, 1))
            return FALSE;

        CurrentWindow = NULL;
        return TRUE;
    }

    BOOL IsSubclassed() { return (CurrentWindow != NULL); }

protected:
    HWND CurrentWindow;

    SubclassWindow()
    {
        CurrentWindow = NULL;
    }

    virtual LRESULT CALLBACK WinSubclassHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;
};

如果您的派生类需要默认处理任何给定的消息,那么您必须做的唯一更改是调用它们的WinSubclassHandler()实现DefSubclassProc()而不是调用它们DefWindowProc()(顺便说一句,调用的 API 是错误的 - 您应该一直调用它CallWindowProc(OriginalProc, ...),所以前面的窗口过程您的子类替换有机会处理消息)。为此,我建议将该调用包装在您的SubclassWindow类中以向后代隐藏该细节,例如:

class SubclassWindow
{
...
protected:
    ...
    static LRESULT DefaultHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    ...
};

class MySubclass : public SubclassWindow
{
protected:
    virtual LRESULT CALLBACK WinSubclassHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        ...
        return DefaultHandler(hWnd, uMsg, wParam, lParam);
    }
};

或者(这将允许后代更好地从后代派生):

class SubclassWindow
{
...
protected:
    ...
    virtual LRESULT CALLBACK WinSubclassHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    ...
};

class MySubclass1 : public SubclassWindow
{
protected:
    virtual LRESULT CALLBACK WinSubclassHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        ...
        return SubclassWindow::WinSubclassHandler(hWnd, uMsg, wParam, lParam);
    }
};

class MySubclass2 : public MySubclass1
{
protected:
    virtual LRESULT CALLBACK WinSubclassHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        ...
        return MySubclass1::WinSubclassHandler(hWnd, uMsg, wParam, lParam);
    }
};

class MySubclass3 : public MySubclass2
{
protected:
    virtual LRESULT CALLBACK WinSubclassHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        ...
        return MySubclass2::WinSubclassHandler(hWnd, uMsg, wParam, lParam);
    }
};
于 2013-08-11T21:34:48.603 回答
2

正如汉斯所说,如果有什么需要恢复的,你的状态就不好了。这意味着原始的WNDPROC,它仍然被用于任何你不会处理的东西,将在那里期待另一个值。

也就是说,SetWindowLongPtr返回旧值,因此您始终可以先调用GetWindowLongPtr来获取原始值。该值可能仍然为零,但至少您会确定。

于 2013-08-11T18:30:37.400 回答