SetWindowLong

2.x

  LONG SetWindowLong(hwnd, nOffset, nVal)    
  HWND hwnd; /* handle of window, */  
  int nOffset; /* offset of value to set */
  LONG nVal; /* new value */

The SetWindowLong function places a long value at the specified offset into the extra window memory of the given window. Extra window memory is reserved by specifying a nonzero value in the cbWndExtra member of the WNDCLASS structure used with the RegisterClass function.

Parameters

hwnd

Identifies the window.

nOffset

Specifies the zero-based byte offset of the value to change. Valid values are in the range zero through the number of bytes of extra window memory, minus four (for example, if 12 or more bytes of extra memory were specified, a value of 8 would be an index to the third long integer), or one of the following values:

Value Meaning

GWL_EXSTYLE Extended window style
GWL_STYLE Window style
GWL_WNDPROC Long pointer to the window procedure

The following values are also available when the hwnd parameter identifies a dialog box:

Value Meaning

DWL_DLGPROC Specifies the address of the dialog box procedure.
DWL_MSGRESULT Specifies the return value of a message processed in the dialog box procedure.
DWL_USER Specifies extra information that is private to the application, such as handles or pointers.

nVal

Specifies the long value to place in the window's reserved memory.

Return Value

The return value is the previous value of the specified long integer, if the function is successful. Otherwise, it is zero.

Comments

If the SetWindowLong function and the GWL_WNDPROC index are used to set a new window procedure, that procedure must have the window-procedure form and be exported in the module-definition file of the application. For more information, see the description of the RegisterClass function.

Calling SetWindowLong with the GCL_WNDPROC index creates a subclass of the window class used to create the window. An application should not attempt to create a window subclass for standard Windows controls such as combo boxes and buttons.

An application should not use this function to set the WS_DISABLE style for a window. Instead, the application should use the EnableWindow function.

To access any extra 4-byte values allocated when the window-class structure was created, use a positive byte offset as the index specified by the nOffset parameter, starting at 0 for the first 4-byte value in the extra space, 4 for the next 4-byte value, and so on.

An application can use the DWL_MSGRESULT value to return values from a dialog box procedure's window procedure. Typically, a dialog box procedure must return TRUE in order for a value to be returned to the sender of the message. Some messages, however, return a value in the Boolean return value of the dialog box procedure. The following messages return values in the return value of the dialog box procedure:

WM_CHARTOITEM
WM_COMPAREITEM
WM_CTLCOLOR
WM_INITDIALOG
WM_QUERYDRAGICON
WM_VKEYTOITEM

Example

The following example shows how to use the SetWindowLong function with the DWL_MSGRESULT value to return a value from a dialog box procedure. Applications often include a switch statement to handle the messages that return values in the Boolean return value of the dialog box procedure, even when the dialog box procedure does not process these messages. This practice makes it easy to revise the dialog box procedure to handle the message and has a negligible effect on speed and memory.

BOOL CALLBACK MyDlgProc(hwndDlg, msg, wParam, lParam)
HWND hwndDlg;
UINT msg;
WPARAM wParam;
LPARAM lParam;
{
    BOOL fProcessed = FALSE;
    LRESULT lResult;

    /*
     * To return a value for a specific message, set lResult to the
     * return value and fProcessed to TRUE.
     */

    switch (msg) {
        .
        .  /* process messages */
        .

    case WM_QUERYENDSESSION:

        /*
         * Example: Do not allow the system to terminate
         * while the dialog box is displayed.
         */

        fProcessed = TRUE;
        lResult = (LRESULT) (UINT) FALSE;
        break;

    default:
        break;
    }

    if (fProcessed) {
        switch (msg) {
        case WM_CTLCOLOR:
        case WM_COMPAREITEM:
        case WM_VKEYTOITEM:
        case WM_CHARTOITEM:
        case WM_QUERYDRAGICON:
        case WM_INITDIALOG:
            return (BOOL) LOWORD(lResult);








        default:
            SetWindowLong(hwndDlg, DWL_MSGRESULT, (LPARAM) lResult);
        }
    }
    return fProcessed;
}

See Also

EnableWindow, GetWindowLong, RegisterClass, SetWindowWord