C++ 如何从WndProc';检索消息发送对象;s参数?

C++ 如何从WndProc';检索消息发送对象;s参数?,c++,winapi,C++,Winapi,因此,我正在编写自己的小GUI框架,将Windows API封装在有用的类中。我目前正试图让用户以面向对象的方式处理WndProc的消息,但我遇到了一点障碍 我有一个按钮类,它继承自一个抽象的控件类,它几乎是按钮HWND句柄的包装器。 还有一个Window类,它包含(您知道什么)一个窗口句柄。此类还有一个Controls容器,负责创建自己的(静态)WndProc方法 我要做的是在包含窗口的WndProc中有一个大的switch语句,它根据函数的wParam和lParam参数调用相应发送控件的处理

因此,我正在编写自己的小GUI框架,将Windows API封装在有用的类中。我目前正试图让用户以面向对象的方式处理WndProc的消息,但我遇到了一点障碍

我有一个
按钮
类,它继承自一个抽象的
控件
类,它几乎是按钮HWND句柄的包装器。 还有一个
Window
类,它包含(您知道什么)一个窗口句柄。此类还有一个
Control
s容器,负责创建自己的(静态)WndProc方法

我要做的是在包含窗口的WndProc中有一个大的switch语句,它根据函数的wParam和lParam参数调用相应发送控件的处理函数。我看到大多数人都是这样做的:

#define MY_BUTTON 101 //Define button's ID

Button::Button()
{
    hwndButton= CreateWindow(
        "BUTTON", text,
        WS_VISIBLE | WS_CHILD,
        position.x, position.y,
        size.x, size.y,
        parent_handle,
        (HMENU)MY_BUTTON,
        GetModuleHandle(NULL),
        NULL);
}
Button* button = new Button();

//Later on, in the Window class...

LRESULT CALLBACK Window::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch((LOWORD)wParam)
    {
    case MY_BUTTON:
        button->HandleMessage(&msg);
        break;
    }
}
但是,我不希望用户为他们创建的每个对象实例分配唯一的整数。 更确切地说,因为我有一个控件容器,所以我宁愿这样做:

//In the Window Class...
Button* button = new Button();
AddControl(button);

static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(&msg)
    {
        ObtainControlFromParams(wParam, lParam)->HandleMessage(&msg);
        //ObtainControlFromParams should return a pointer to a Control, 
        //and it should look into the window's Control collection.
    }
}
尽管这听起来很棒,但我找不到一种方法来实现GetainControlFromParams函数

我认为区分每个控件实例的方法是使用一个字符串,我称之为控件的“名称”,该字符串对于每个对象实例化都是唯一的。这将给我留下两个选择(我能想到)

  • 将字符串转换为散列,将其存储在其他位置(例如在包含该字符串的窗口中),并将其用作按钮的菜单。然后,我可以使用哈希集将每个字符串哈希链接到其所有者对象,并以这种方式调用corresponding方法
  • 使用lParam方法(据我所知)存储按钮的HWND句柄,并使用该方法执行一些操作
我很抱歉,如果我想得到的不是很清楚,这对我来说是一个复杂的概念


非常感谢您的帮助

将按钮的
控件*
对象指针存储在按钮
HWND
本身中,消息过程可以从中检索它。为此,可以使用
(Set | Get)WindowLongPtr(GWL|u USERDATA)
(Get | Set)SetProp()

只有某些消息,如
WM_COMMAND
WM_NOTIFY
,才能识别发送它们的控件。这些消息将发送到控件的父窗口。这些消息包含子控件的
HWND
。父级可以检索子级的
控件*
,并将消息转发给它

其他消息直接发送到控件自己的窗口,而不是其父窗口。这些消息不能识别它们要发送到的控件。因此,每个
控件
都需要自己的
HWND
单独的WndProc过程。调用
CreateWindow()
后,使用
SetWindowLongPtr(GWL\u WNDPROC)
setWindowsSubClass()
将该WNDPROC分配给
HWND

尝试类似的方法(这是非常粗糙的,在创建UI框架时会涉及很多内容,但这会给您一些想法):


通常的方法是将发送到控件父窗口的
WM_COMMAND
WM_NOTIFY
等消息反射回发送它们的控件。这是可能的,因为这些消息标识发送者(每个消息都以唯一的方式,因此请检查文档)

<>有很多方法将C++对象指针与窗口关联:

  • 动态生成的蹦床功能作为窗口程序。
    效率最高,但也最棘手。可能需要使用
    VirtualAlloc

  • 窗口属性。
    SetProp
    GetProp
    功能正常

  • 窗口对象词。
    SetWindowLongPtr
    。需要确保有分配的空间

  • 静态映射。
    例如,一个单例映射句柄→ 反对

  • Windows标准子类化使用的任何内容。
    即使用
    setWindowsSubClass


您不必让用户手动分配唯一的id,您可以使用全局整数(或管理静态变量的类)生成id,该整数在每次创建新控件时递增。您不控制参数,因此查找的位置不正确。将指向对象的指针与窗口关联。有多种方法可以做到这一点。主题在这里已经被讨论了数千次。@bennji\u的\u溢出或仅使用HWND作为标识符;它已经是独一无二的(在按钮的生命周期内)!
typedef std::basic_string<TCHAR> tstring;

class Control
{
private:
    HWND fWnd;
    Control *fParent;
    POINT fPosition;
    SIZE fSize;
    tstring fText;
    std::list<Control*> fControls;
    ...

    void addControl(Control *c);
    void removeControl(Control *c);

    virtual void createWnd() = 0;
    void internalCreateWnd(LPCTSTR ClassName, DWORD style, DWORD exstyle);

    ...

    static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

protected:
    virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
    ...

public:
    Control();
    virtual ~Control();

    HWND getWnd();
    void destroyWnd();
    void wndNeeded();

    Control* getParent();
    void setParent(Control *value);

    POINT getPosition();
    void setPosition(POINT value);

    SIZE getSize();
    void setSize(SIZE value);

    tstring getText();
    void setText(const tstring &value);
    ...
};
static const LPTSTR szControlPropStr = TEXT("ControlPtr")

Control* ControlFromWnd(HWND hwnd)
{
    return (Control*) GetProp(hwnd, szControlPropStr);
}

Control::Control()
    : fWnd(0), fParent(0)
{
    fPosition.x = fPosition.y = 0;
    fSize.cx = fSize.cy = 0;
    ...
}

Control::~Control()
{
    setParent(0);
    destroyWnd();
}

void Control::addControl(Control *c)
{
    fControls.push_back(c);
    c->fParent = this;
    if (fWnd)
            c->wndNeeded();
}

void Control::removeControl(Control *c)
{
    fControls.remove(c);
    c->destroyWnd();
    c->fParent = 0;
}

HWND Control::getWnd()
{
    wndNeeded();
    return fWnd;
}

void Control::internalCreateWnd(LPCTSTR ClassName, DWORD style, DWORD exstyle)
{
    style |= WS_VISIBLE;
    if (fParent)
         style |= WS_CHILD;

    fWnd = CreateWindowEx(exstyle,
        ClassName, fText.c_cstr(), style,
        fPosition.x, fPosition.y,
        fSize.cx, fSize.cy,
        (fParent) ? fParent->getWnd() : 0,
        0,
        GetModuleHandle(NULL),
        NULL);
    SetProp(fWnd, szControlPropStr, (HANDLE)this);
    SetWindowLongPtr(fWnd, GWL_WNDPROC, (LONG_PTR)&Control::WndProc);
}

void Control::destroyWnd()
{
    DestroyWindow(fWnd);
    fWnd = 0;
}

void Control::wndNeeded()
{
    if (!fWnd)
    {
        createWnd();
        for (std::list<Control*>::iterator iter = fControls.begin(); iter != fControls.end(); ++iter)
            iter->wndNeeded();
    }
}

Control* Control::getParent()
{
    return fParent;
}

void Control::setParent(Control *value)
{
    if (fParent != value)
    {
        if (fParent)
            fParent->removeControl(this);
        fParent = value;
        if (fParent)
            fParent->addControl(this);
    }
}

POINT Control::getPosition()
{
    return fPosition;
}

void Control::setPosition(POINT value)
{
    fPosition = value;
    if (fWnd)
        SetWindowPos(fWnd, 0, fPosition.x, fPosition.y, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
}

SIZE Control::getSize()
{
    return fSize;
}

void Control::setSize(SIZE value)
{
    fSize = value;
    if (fWnd)
        SetWindowPos(fWnd, 0, 0, 0, fSize.cx, fSize.cy, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
}

tstring Control::getText()
{
    return fText;
}

void Control::setText(const tstring &value)
{
    fText = value;
    if (fWnd)
        SetWindowText(fWnd, fText.c_str());
}

LRESULT CALLBACK Control::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    Control *pThis = ControlFromWnd(hwnd);
    if (pThis)
        return pThis->HandleMessage(uMsg, wParam, lParam);
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

LRESULT Control::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        WM_NCDESTROY:
        {
            RemoveProp(fWnd, szControlPropStr);
            fWnd = 0;
            break;
        }

        case WM_COMMAND:
        {
            HWND hwnd  = (HWND) lParam;
            if ((hwnd) && (hwnd != fWnd))
            {
                Control *c = ControlFromWnd(hwnd);
                if  (c)
                    return c->HandleMessage(uMsg, wParam, lParam);
            }
            ...
            break;
        }

        case WM_NOTIFY:
        {
            NMHDR *hdr = (NMHDR*) lParam;
            if ((hdr->hwndFrom) && (hdr->hwndFrom != fWnd))
            {
                Control *c = ControlFromWnd(hdr->hwndFrom);
                if  (c)
                    return c->HandleMessage(uMsg, wParam, lParam);
            }
            ...
            break;
        }

        case WM_WINDOWPOSCHANGED:
        {
            WINDOWPOS *p = (WINDOWPOS*) lParam;

            if (!(p->flags & SWP_NOMOVE))
            {
                fPosition.x = p->x;
                fPosition.y = p->y;
            }

            if (!(p->flags & SWP_NOSIZE))
            {
                fSize.cx = p->cx;
                fSize.cy = p->cy;
            }

            ...
            return 0;
        }

        case WM_SETTEXT:
        {
            LRESULT ret = DefWindowProc(fWnd, uMsg, wParam, lParam);
            if (ret == TRUE)
                fText = (TCHAR*) lParam;
            return ret;
        }

        ...
    }

    return DefWindowProc(fWnd, uMsg, wParam, lParam);
}
class Button : public Control
{
protected:
    virtual void createWnd();
    virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);

public:
    Button();
};
Button::Button()
    : Control()
{
}

void Button::createWnd()
{
    Control::intetnalCreateWnd(TEXT("BUTTON"), BS_PUSHBUTTON | BS_TEXT, 0);
    ...
}

LRESULT Button::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_COMMAND:
        {
            HWND hwnd = (HWND) lParam;
            if (hwnd != fWnd)
                break;

            switch (HIWORD(wParam))
            {
                case BN_CLICKED:
                {
                    ...
                    return 0;
                }

                ...
            }

            break;
        }
    }

    return Control::HandleMessage(uMsg, wParam, lParam);
}
//In the Window Class...
Button* button = new Button();
button->setPosition(...);
button->setSize(...);
button->setText(...);
button->setParent(this);