读书人

小弟我想问一个SetWindowLong的有关问

发布时间: 2012-02-12 17:16:33 作者: rapoo

我想问一个SetWindowLong的问题,请高手指点! - C++ Builder / Windows SDK/API
哪个高手能帮我看看这是怎么回事,不是说SetWindowLong的返回值是之前设定的值吗?为什么我在这里面保存的返回值不是我想要的主窗口消息WndProc的地址值呢?可是当我把下面代码中的注释部分,取消注释加到程序中时,该函数就能返回正确的值,即:之前设定的窗口消息ScrollProc的地址值,这是为什么呢?请高手指点。
…………………………
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WNDPROC OldScroll[3] ;
static TCHAR test[100];
static WNDPROC WND;
switch(message)
{
case WM_CREATE:
hwndone = CreateWindow(TEXT("scrollbar"),…………);
OldScroll[0] = (WNDPROC)SetWindowLong(hwndone,GWL_WNDPROC,(LONG)ScrollProc);
//OldScroll[0] = (WNDPROC)SetWindowLong(hwndone,GWL_WNDPROC,(LONG)ScrollProc);
WND = GetWindowLong(hwnd,GWL_WNDPROC);
wsprintf(test,TEXT("%i,%i"),WND,&WndProc,OldScroll[0],&ScrollProc);
return 0;
……
……
……
}
return DefWindowProc(hwnd,message,wParam,lParam);
}

LRESULT CALLBACK ScrollProc (HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam)
{
……
……
……
return CallWindowProc (OldScroll[id], hwnd, message, wParam, lParam) ;
}

[解决办法]
Return Value

If the function succeeds, the return value is the previous value of the specified 32-bit integer.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

If the previous value of the specified 32-bit integer is zero, and the function succeeds, the return value is zero, but the function does not clear the last error information. This makes it difficult to determine success or failure. To deal with this, you should clear the last error information by calling SetLastError(0) before calling SetWindowLong. Then, function failure will be indicated by a return value of zero and a GetLastError result that is nonzero.
[解决办法]
Remarks
If you use SetWindowLong with the GWL_WNDPROC index to replace the window procedure, the window procedure must conform to the guidelines specified in the description of the WindowProc callback function.

Calling SetWindowLong with the GWL_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 SetWindowLong 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.

读书人网 >C++ Builder

热点推荐