我作了一个类,类中一个成员函数是窗口过程。我使用SetWindowLong和此成员函数替换原有的窗口过程,但编译器抱错:不能将此成员函数的地址转换成long类型。但是我将窗口过程定义成全局函数,就可以了,请教是什么原因,难道成员函数的地址和普通全局函数的地址有什么区别吗?class CTest
{
  public:
    LRESULT CALLBACK MyWndProc();
    void Replace();
  .......
};LRESULT CALLBACK CTest::MyWndProc()
{
  ......
}void CTest::Replace()
{
  ......
  SetWindowLong(hwndParent,GWL_WNDPROC,(LONG)MyWndProc);
}

解决方案 »

  1.   

    SetWindowLongPtr(hwndParent,GWL_WNDPROC,MyWndProc);试一试
      

  2.   

    类的成员函数只有是静态的才可以,或者是全局函数!
    因为普通的类成员函数必须接收一个this指针参数,而SetWindowLong的窗口过程函数是没有this指针的,所以编译器不让通过!
      

  3.   

    to roger_ding(海天一色):我是在类方法中调用的SetWindowLongPtr,已经隐含了this指针.所以
    SetWindowLong(hwndParent,GWL_WNDPROC,(LONG)MyWndProc);与SetWindowLong(hwndParent,GWL_WNDPROC,(LONG)(this->MyWndProc));等价。
      

  4.   

    打错了,SetWindowLongPtr应为SetWindowLong
      

  5.   

    不是吧?
    LRESULT CALLBACK MyWndProc();
    这个成员函数  的参数列表中隐含了一个this 指针
      

  6.   

    to  changhua4929(迷惘的人)
    不是说隐含了this指针就可以了,因为MyWndProc是回调函数,它不是给你用的,是给系统API回调用的,而系统API调用回调函数时是不会传入this的,所以如果MyWndProc定义为类非静态成员函数,则编译器会给它加上this参数,即使MyWndProc没有使用类成员变量,编译器也不会通过!
      

  7.   

    SetWindowLongPtr没写错啊!有这个函数.window核心就是用这个函数的
      

  8.   

    to fanqing(火影忍者+20%):msdn中没找到SetWindowLongPtr的说明。
    to roger_ding(海天一色): 说实话,没看懂你说的。另外,如何得到类中普通成员函数的地址?
      

  9.   

    SetWindowLongPtr Function--------------------------------------------------------------------------------
    The SetWindowLongPtr function changes an attribute of the specified window. The function also sets a value at the specified offset in the extra window memory.This function supersedes the SetWindowLong function. To write code that is compatible with both 32-bit and 64-bit versions of Microsoft® Windows®, use SetWindowLongPtr.
    SyntaxLONG_PTR SetWindowLongPtr(          HWND hWnd,
        int nIndex,
        LONG_PTR dwNewLong
    );
    ParametershWnd
    [in] Handle to the window and, indirectly, the class to which the window belongs. The SetWindowLongPtr function fails if the window specified by the hWnd parameter does not belong to the same process as the calling thread. 
    nIndex
    [in] Specifies the zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window memory, minus the size of an integer. To set any other value, specify one of the following values.
    GWL_EXSTYLE
    Sets a new extended window style. For more information, see CreateWindowEx. 
    GWL_STYLE
    Sets a new window style.
    GWLP_WNDPROC
    Sets a new address for the window procedure. 
    GWLP_HINSTANCE
    Sets a new application instance handle.
    GWLP_ID
    Sets a new identifier of the window.
    GWLP_USERDATA
    Sets the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero.
    The following values are also available when the hWnd parameter identifies a dialog box.
    DWLP_DLGPROC
    Sets the new pointer to the dialog box procedure.
    DWLP_MSGRESULT
    Sets the return value of a message processed in the dialog box procedure.
    DWLP_USER
    Sets new extra information that is private to the application, such as handles or pointers.
    dwNewLong
    [in] Specifies the replacement value. 
    Return ValueIf the function succeeds, the return value is the previous value of the specified offset.If the function fails, the return value is zero. To get extended error information, call GetLastError. If the previous value is zero and the function succeeds, the return value is zero, but the function does not clear the last error information. To determine success or failure, clear the last error information by calling SetLastError(0), then call SetWindowLongPtr. Function failure will be indicated by a return value of zero and a GetLastError result that is nonzero.
    ResCertain window data is cached, so changes you make using SetWindowLongPtr will not take effect until you call the SetWindowPos function.If you use SetWindowLongPtr with the GWLP_WNDPROC index to replace the window procedure, the window procedure must conform to the guidelines specified in the description of the WindowProc callback function. If you use SetWindowLongPtr with the DWLP_MSGRESULT index to set the return value for a message processed by a dialog box procedure, the dialog box procedure should return TRUE directly afterwards. Otherwise, if you call any function that results in your dialog box procedure receiving a window message, the nested window message could overwrite the return value you set by using DWLP_MSGRESULT. Calling SetWindowLongPtr with the GWLP_WNDPROC index creates a subclass of the window class used to create the window. An application can subclass a system class, but should not subclass a window class created by another process. The SetWindowLongPtr function creates the window subclass by changing the window procedure associated with a particular window class, causing the system to call the new window procedure instead of the previous one. An application must pass any messages not processed by the new window procedure to the previous window procedure by calling CallWindowProc. This allows the application to create a chain of window procedures. Reserve extra window memory by specifying a nonzero value in the cbWndExtra member of the WNDCLASSEX structure used with the RegisterClassEx function. Do not call SetWindowLongPtr with the GWLP_HWNDPARENT index to change the parent of a child window. Instead, use the SetParent function. If the window has a class style of CS_CLASSDC or CS_PARENTDC, do not set the extended window styles WS_EX_COMPOSITED or WS_EX_LAYERED.