SetWindowLong
The SetWindowLong function changes an attribute of the specified window. The function also sets the 32-bit (long) value at the specified offset into the extra window memory.【SetWindowLong函数更改指定窗口的一项属性。该函数还可将指定偏移的32位值设置到附加的窗口内存中。】
Note This function has been superseded by the SetWindowLongPtr function. To write code that is compatible with both 32-bit and 64-bit versions of Microsoft Windows, use the SetWindowLongPtr function. 【注意 该函数已被SetWindowLongPtr函数取代。要编写与32位和64位版本微软Windows系统兼容的代码,使用SetWindowLongPtr函数。】
Syntax(语法)
LONG SetWindowLong (
HWND hWnd,
int nIndex,
LONG dwNewLong
);
Parameters(参数)
hWnd
[in] 输入参数
Handle to the window and, indirectly, the class to which the window belongs.
Windows 95/98/Me: The SetWindowLong function may fail if the window specified by the hWnd parameter does not belong to the same process as the calling thread. 【指向窗口和窗口所属类的句柄。Windows 95/98/Me: 如果由hWnd参数指定的窗口与调用线程不属于同一进程,SetWindowLong 函数可能会失败。】
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. 【指定要设置的值的基于零的偏移。有效值的范围为0到附加的窗口内存的字节数减去一个整数的大小。要设置任何其他值,请指定以下值之一。】
GWL_EXSTYLE | Sets a new extended window style. For more information, see CreateWindowEx. 【设置新的扩展窗样式。有关详细信息,请参阅CreateWindowEx。】 |
GWL_STYLE | Sets a new window style.【设置新的窗样式。】 |
GWL_WNDPROC | Sets a new address for the window procedure. Windows NT/2000/XP: You cannot change this attribute if the window does not belong to the same process as the calling thread.【设置窗口过程的新地址。如果窗口与调用线程不属于同一进程,则不能更改此属性。 |
GWL_HINSTANCE | Sets a new application instance handle. 【设置新的应用程序实例句柄。】 |
GWL_ID | Sets a new identifier of the window. 【设置窗口的新标识符。】 |
GWL_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.【当hWnd参数标识对话框时,以下值也可用。】
DWL_DLGPROC | Sets the new address of the dialog box procedure. 【设置对话框过程的新地址。】 |
DWL_MSGRESULT | Sets the return value of a message processed in the dialog box procedure. 【设置对话框过程中处理的消息的返回值。】 |
DWL_USER | Sets new extra information that is private to the application, such as handles or pointers.【设置应用程序专用的新附加信息,如句柄或指针。】 |
dwNewLong
[in] 输入参数
Specifies the replacement value. 【指定替换值。】
Return Value(返回值)
If the function succeeds, the return value is the previous value of the specified 32-bit integer.【如果函数成功,则返回值是指定的32位整数的前一个值。】
If the function fails, the return value is zero. To get extended error information, call GetLastError. 【如果函数失败,则返回值为零。要获取扩展错误信息,请调用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. 【如果指定的32位整数的前一个值为零,并且函数成功,则返回值为零,但函数不清除最后的错误信息。这使得很难决定成败。为了解决这个问题,您应该在调用SetWindowLong之前通过调用SetLastError(0)来清除最后的错误信息。然后,函数失败将由返回值0和非零的GetLastError结果指示。】
Remarks(备注)
Certain window data is cached, so changes you make using SetWindowPos will not take effect until you call the SetWindowPos function. Specifically, if you change any of the frame styles, you must call SetWindowPos with the SWP_FRAMECHANGED flag for the cache to be updated properly. 【某些窗口数据是缓存的,因此在调用SetWindowPos函数之前,使用SetWindowLong所做的更改不会生效。特别是,如果更改任何框架样式,则必须使用SWP_FRAMECHANGED标志调用SetWindowPos才能正确更新缓存。】
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. 【如果将SetWindowLong与GWL_WNDPROC一起使用来替换窗口过程,则新的窗口过程必须符合WindowProc回调函数说明中指定的准则。】
If you use SetWindowLong with the DWL_MSGRESULT index to set the return value for a message processed by a dialog procedure, you should return TRUE directly afterward. Otherwise, if you call any function that results in your dialog receiving a window message, the nested window message could overwrite the return value you set using DWL_MSGRESULT. 【如果将SetWindowLong与DWL_MSGRESULT一起使用,为对话框过程处理的消息设置返回值,则应在其后直接返回TRUE。否则,如果调用导致对话框过程接收窗口消息的任何函数,则嵌套的窗口消息可能会覆盖使用DWL_MSGRESULT.设置的返回值。】
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. 【用GWL_WNDPROC调用SetWindowLong将创建用于创建窗口的窗口类的子类。应用程序可以子类化系统类,但不应子类化由另一个进程创建的窗口类。SetWindowLong函数通过更改与特定窗口类关联的窗口过程来创建窗口子类,从而使系统调用新的窗口过程而不是前一个窗口过程。应用程序必须通过调用CallWindowProc将新窗口过程未处理的任何消息传递给上一个窗口过程。这允许应用程序创建一系列窗口过程。】
Reserve extra window memory by specifying a nonzero value in the cbWndExtra member of the WNDCLASSEX structure used with the RegisterClassEx function. 【通过在与RegisterClassEx函数一起使用的WNDCLASSEX结构的cbWndExtra成员中指定一个非零值来保留附加窗口内存。】
You must not call SetWindowLong with the GWL_HWNDPARENT index to change the parent of a child window. Instead, use the SetParent function. 【不能使用GWL_HWNDPARENT调用SetWindowLong来更改子窗口的父窗口。相反,使用SetParent函数。】
If the window has a class style of CS_CLASSDC or CS_OWNDC, do not set the extended window styles WS_EX_COMPOSITED or WS_EX_LAYERED. 【如果窗口的类样式为CS_CLASSDC或CS_OWNDC,请不要将扩展窗口样式设置为WS_EX_COMPOSITED或WS_EX_LAYERED。】
Windows XP/Vista: Calling SetWindowLong to set the style on a progressbar will reset its position. 【Windows XP/Vista:调用SetWindowLong设置进度条上的样式将重置其位置。】
Windows 95/98/Me: SetWindowLongW is supported by the Microsoft Layer for Unicode (MSLU). SetWindowLongA is also supported to provide more consistent behavior across all Windows operating systems. To use these versions, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems World Wide Web link.【Windows 95/98/Me: Microsoft Layer for Unicode(MSLU)支持SetWindowLongW。SetWindowLongA还支持在所有Windows操作系统中提供更一致的行为。要使用这些版本,必须将某些文件添加到应用程序中,如Microsoft Layer for Unicode on Windows 95/98/Me Systems World Wide Web链接中所述。】