Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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.
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.
None.
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
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
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