[comp.windows.ms.programmer] Window Handles: what's real?

whisd@sersun1.essex.ac.uk (Whiteside S D B) (05/17/91)

Can anyone clarify this for me?

When I use a CreateWindow call, my window's message function gets called with one of the parameters being hWnd.

When the CreateWindow call returns it gives me the hWnd for the window just created.

BUT... these two handles are different! Which is the real one?

I've tried both, and both seem to work. Why does a window need TWO handles?!!

Thanks in advance,

Simon Whiteside
 

risto@tuura.UUCP (Risto Lankinen) (05/20/91)

whisd@sersun1.essex.ac.uk (Whiteside S D B) writes:

>When I use a CreateWindow call, my window's message function gets called
>with one of the parameters being hWnd.
>When the CreateWindow call returns it gives me the hWnd for the window
>just created.
>BUT... these two handles are different! Which is the real one?
>I've tried both, and both seem to work. Why does a window need TWO handles?

Hi!

They're probably the same handle in locked and unlocked format.  So it is,
if their difference is 1 (and the odd hWnd is greater than the even hWnd).

Terveisin: Risto Lankinen
-- 
Risto Lankinen / product specialist ***************************************
Nokia Data Systems, Technology Dept *  2                              3   *
THIS SPACE INTENTIONALLY LEFT BLANK * 2 +1 is PRIME!  Now working on 2 -1 *
replies: risto@yj.data.nokia.fi     ***************************************

Eric_Lapaille@p2721.f27.n295.z2.fidonet.org (Eric Lapaille) (05/23/91)

 > When I use a CreateWindow call, my window's message 
 > function gets called with one of the parameters being 
 > hWnd. 
 > 
 > When the CreateWindow call returns it gives me the hWnd 
 > for the window just created.

 When you pass a hWnd as argument to the CreateWindow function , it's the
handle of the parent's window not the window itself.

CreateWindow

Syntax  HWND CreateWindow(lpClassName, lpWindowName, dwStyle, X, Y, nWidth,
        nHeight, hWndParent, hMenu, hInstance, lpParam)

        Parameter       Type/Description

        lpClassName     LPSTR Points to a null-terminated character string
                        that names the window class. The class name can be
                        any name registered with the RegisterClass function
                        or any of the predefined control-class names
                        specified in Table T.2, "Control Classes."

        lpWindowName    LPSTR Points to a null-terminated character string
                        that represents the window name.

        dwStyle         DWORD Specifies the style of window being created.
                        It can be any combination of the styles given in
                        Table T.3, "Window Styles, " the control styles
                        given in Table 4.4, "Control Styles, " or a
                        combination of styles created by usingthe bitwise OR
                        operator.

        X               int Specifies the initial x-position of the window.
                        For an overlapped or pop-up window, the X parameter
                        is the initial x-coordinate of the window's
                        upper-left corner (in screen coordinates). If this
                        value is CW_USEDEFAULT, Windows selects the default
                        position for the window's upper-left corner. For a
                        child window, X is the x-coordinate of the
                        upper-left corner of the window in the client area
                        of its parent window.

        Y               int Specifies the initial y-position of the window.
                        For an overlapped window, the Y parameter is the
                        initial y-coordinate of the window's upper-left
                        corner. For a pop-up window, Y is the y-coordinate
                        (in screen coordinates) of the upper-left corner of
                        the pop-up window. For list-box controls, Y is the y
                        -coordinate of the upper-left corner of the
                        control's client area. For a child window, Y is the
                        y-coordinate of the upper-left corner of the child
                        window. All of these coordinates are for the window,
                        not the window's client area.

        nWidth          int Specifies the width (in device units) of the
                        window. For overlapped windows, the nWidth parameter
                        is either the window's width (in screen coordinates)
                        or CW_USEDEFAULT. If nWidth is CW_USEDEFAULT,
                        Windows selects a default width and height for the
                        window (the default width extends from the initial x
                        -position to the right edge of the screen,, and the
                        default height extends from the initial y-position
                        to the top of the icon area).

        nHeight         int Specifies the height (in device units) of the
                        window. For overlapped windows, the nHeight
                        parameter is the window's height in screen
                        coordinates. If the nWidth parameter is
                        CW_USEDEFAULT, Windows ignores nHeight.

        hWndParent      HWND Identifies the parent or owner window of the
                        window being created. A valid window handle must be
                        supplied when creating a child window or an owned
                        window. An owned window is an overlapped window that
                        is destroyed when its owner window is destroyed,
                        hidden when its owner is made iconic, and which is
                        always displayed on top of its owner window. For
                        pop-up windows, a handle can be supplied, but is not
                        required. If the window does not have a parent or is
                        not owned by another window, the hWndParent
                        parameter must be set to NULL.

        hMenu           HMENU Identifies a menu or a child-window
                        identifier. The meaning depends on the window style.
                        For overlapped or pop-up windows, the hMenu
                        parameter identifies the menu to be used with the
                        window. It can be NULL, if the class menu is to be
                        used. For child windows, hMenu specifies the
                        child-window identifier, an integer value that is
                        used by a dialog-box control to notify its parent of
                        events (such as the EN_HSCROLL message). The
                        child-window identifier is determined by the
                        application and should be unique for all child
                        windows with the same parent window.

        hInstance       HANDLE Identifies the instance of the module to be
                        associated with the window.

        lpParam         LPSTR Points to a value that is passed to the window
                        through the CREATESTRUCT data structure referenced
                        by the lParam parameter of the WM_CREATE message. If
                        an application is calling CreateWindow to create a
                        multiple document interface (MDI) client window,
                        lpParam must point to a CLIENTCREATESTRUCT data
                        structure.

Return Value  The return value identifies the new window. It is NULL if the
        window is not created.

Comments  For overlapped windows where the X parameter is CW_USEDEFAULT, the
        Y parameter can be one of the show-style parameters described with
        the ShowWindow function, or, for the first overlapped window to be
        created by the application, it can be the nCmdShow parameter passed
        to the WinMain function.

See also:       ^QControl Classes^P

                ^QWindow Styles^P

                ^QControl Styles^P

                                       -^D-

kensy@microsoft.UUCP (Ken SYKES) (05/23/91)

In article <5175@servax0.essex.ac.uk> whisd@essex.ac.uk (Whiteside S D B) writes:
>Can anyone clarify this for me?
>
>When I use a CreateWindow call, my window's message function gets called with one of the parameters being hWnd.
>
>When the CreateWindow call returns it gives me the hWnd for the window just created.
>
>BUT... these two handles are different! Which is the real one?
>
>I've tried both, and both seem to work. Why does a window need TWO handles?!!

This has been discussed before but here is the reason (sort of): The hwnd
at the WM_CREATE create message is not "fully initialized" as a window
handle.  You are in the process of creating a window so this kinda makes
sense.  The createstruct pointed to by lParam contains the proper handle
if I remember correctly.  The hWnd passed back from CreateWindow is
valid and will match the handle seen in subsequent messages to your
WndProc.

Hope this clears up the confusion.

Ken Sykes
Disclaimer: The above opinions are solely my own.