SetWindowLong Function

Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Platforms

Description & Usage

SetWindowLong sets a 32-bit value constituting the information about a window. This function can also set a 32-bit value within the block of extra memory given to the window, if such a block exists.

Return Value

If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns the previous setting of whatever 32-bit value was replaced.

Visual Basic-Specific Issues

None.

Parameters

hWnd
A handle to the window to set a 32-bit value in.
nIndex
To set a 32-bit value within the window's extra memory block, this is the zero-based offset of the byte to begin writing to. Valid values range from 0 to the size of the extra memory block in bytes minus four. To set a 32-bit value of one of the properties of the window, this is one of the following flags specifying which piece of information to set:
GWL_EXSTYLE
Set the extended window styles of the window. dwNewLong is the new setting.
GWL_HINSTANCE
Set which application instance is considered to own the window. dwNewLong is the new setting.
GWL_HWNDPARENT
Retrieve a handle to the parent window, if any. dwNewLong is a handle to the instance to set as the owner.
GWL_ID
Set the identifier of the window. dwNewLong is the new identifier.
GWL_USERDATA
Set the application-defined 32-bit value associated with the window. dwNewLong is the new value.
GWL_STYLE
Retrieve the window styles of the window.
GWL_WNDPROC
Set the WindowProc hook function to use as the window's procedure. dwNewLong is a pointer to the hook function to set as the window procedure.
If the window happens to be a dialog box, this could also be one of the following flags:
DWL_DLGPROC
Set the WindowProc hook function to use as the dialog box procedure. dwNewLong is a pointer to the hook function to set as the window procedure.
DWL_MSGRESULT
Set the return value of the last message processed by the dialog box. dwNewLong is the new value.
DWL_USER
Set the application-defined 32-bit value associated with the dialog box. dwNewLong is the new value.
dwNewLong
The 32-bit value to set as specified by nIndex.

Constant Definitions

Const GWL_EXSTYLE = -20
Const GWL_HINSTANCE = -6
Const GWL_HWNDPARENT = -8
Const GWL_ID = -12
Const GWL_STYLE = -16
Const GWL_USERDATA = -21
Const GWL_WNDPROC = -4
Const DWL_DLGPROC = 4
Const DWL_MSGRESULT = 0
Const DWL_USER = 8

Example

Have a window play the SystemAsterisk sound whenever it gains or loses the focus. This is done by playing the sound whenever the window receives the WM_ACTIVATE message. To use this example, copy the following code into the proper locations.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hWnd As Long, _
	ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = -4
Public Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal lpPrevWndFunc _
	As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As Any, ByVal _
	hModule As Long, ByVal dwFlags As Long) As Long 
Public Const WM_ACTIVATE = &H6

' *** Place this code in a module. ***

' The following variable is accessible to all code in this example.
Public pOldProc As Long  ' pointer to the previous window function

' Define the new window procedure.
Public Function WindowProc (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, _
		 ByVal lParam As Long) As Long
	Dim retval As Long  ' return value
	' If the message is WM_ACTIVATE (we don't care about the parameters),
	' play the SystemAsterisk sound.
	If uMsg = WM_ACTIVATE Then
		retval = PlaySound("SystemAsterisk", 0, SND_ALIAS Or SND_ASYNC)
	End If
	' No matter what happened, use the old window procedure to
	' finish processing the message.
	retval = CallWindowProc(pOldProc, hWnd, uMsg, wParam, lParam)
	' Have this function return whatever the function above returned.
	WindowProc = retval
End Function

' *** Place the following code in the form window. ***

Private Sub Form_Load ()
	Dim retval As Long  ' return value
	
	' Set the new window procedure for Form1, saving a pointer to the old one.
	pOldProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Private Sub Form_Unload (Cancel As Integer)
	Dim retval As Long  ' return value
	
	' Restore the window's procedure before closing.
	retval = SetWindowLong(Me.hWnd, GWL_WNDPROC, pOldProc)
End Sub

See Also

GetWindowLong, SetClassLong

Category

Window Classes

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: October 29, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/s/setwindowlong.html