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 window Form1 play the SystemAsterisk sound whenever it gets
' or loses the focus.  Do this by specifying a new window procedure which
' plays the sound whenever the WM_ACTIVATE message is received.  To
' process all other messages (and do whatever else WM_ACTIVATE should
' do), the procedure then calls the previous window procedure.

' *** Place this code in a module. ***
Const WM_ACTIVATE = &H6  ' identifier of the message
' 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
  ' 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 wherever you wish. ***
Dim retval As Long  ' return value

' Set the new window procedure for Form1, saving a pointer to the old one.
pOldProc = SetWindowLong(Form1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
' Now WindowProc processes Form1's messages, playing the sound
' whenever Form1 is activated or loses activated status.

Related Functions

GetWindowLong, SetClassLong

Category

Window Classes

Back to the index.


Last Modified: August 28, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information

Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/s/setwindowlong.html