实现类非静态成员函数做窗口过程函数

来源:互联网 发布:苏州哪里有mac专柜 编辑:程序博客网 时间:2024/06/11 22:39

实现类非静态成员函数做窗口过程函数,无非是想办法把类对象指针传递给窗口过程。最简单的办法是使用全局变量,在很多场景下是快捷有效的办法。这里介绍的办法是借助::SetWindowLong(hWnd, GWL_USERDATA, (LONG)pWindow)调用,把类指针传递给窗口。在窗口过程函数中再调用 GetWindowLong来获取出来使用。


通过CreateWindowEx最后一个参数,把类指针传递给窗口,详见代码:


class CMyWindow{protected:    static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)    {        CMyWindow* pWindow;        if (WM_NCCREATE == uMsg)        {            MDICREATESTRUCT* pMDIC = (MDICREATESTRUCT*)((LPCREATESTRUCT)lParam)->lpCreateParams;            pWindow = (CMyWindow*)(pMDIC->lParam);            ::SetWindowLong(hWnd, GWL_USERDATA, (LONG) pWindow);        }        else        {            pWindow=(CMyWindow*)::GetWindowLong(hWnd, GWL_USERDATA);        }        if (NULL != pWindow)        {            return pWindow->WndProc(hWnd, uMsg, wParam, lParam);        }        else        {            return ::DefWindowProc(hWnd, uMsg, wParam, lParam);        }    }    virtual LRESULT WndProcSelf(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)    {        //this is self wndproc        return 0L;    }    virtual HWND CreateEx(DWORD dwExStyle,        LPCTSTR lpszClass,        LPCTSTR lpszName,        DWORD dwStyle,        int x, int y,         int nWidth, int nHeight,         HWND hParent,         HMENU hMenu,         HINSTANCE hInst)    {        MDICREATESTRUCT mdic;        memset(& mdic, 0, sizeof(mdic));        mdic.lParam = (LPARAM) this;        return CreateWindowEx(dwExStyle,            lpszClass,            lpszName,            dwStyle,            x, y,            nWidth, nHeight,            hParent,            hMenu,            hInst,            & mdic);    }private:    HWND m_hWnd;};


原创粉丝点击