Results 1 to 5 of 5

Thread: Getting HDC & HWND of Window in VC++

  1. #1

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562

    Getting HDC & HWND of Window in VC++

    Hi,
    I want to know how to get the HDC & HWND of a window in VC++.
    This is very easy in VB since I can simply say, Form1.hDC or Form1.HWnd.
    But how do I do it in VC++
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    To get the HWND use FindWindow and FindWindowEx. I'm sure you know how to use them from VB.
    FindWindow
    The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.

    HWND FindWindow(
    LPCTSTR lpClassName, // pointer to class name
    LPCTSTR lpWindowName // pointer to window name
    );

    Parameters
    lpClassName
    Pointer to a null-terminated string that specifies the class name or is an atom that identifies the class-name string. If this parameter is an atom, it must be a global atom created by a previous call to theGlobalAddAtom function. The atom, a 16-bit value, must be placed in the low-order word of lpClassName; the high-order word must be zero.
    lpWindowName
    Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.
    Return Values
    If the function succeeds, the return value is a handle to the window that has the specified class name and window name.
    To get the DC you need the HWND of the window and then use GetDC or GetWindowDC
    GetWindowDC
    The GetWindowDC function retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars. A window device context permits painting anywhere in a window, because the origin of the device context is the upper-left corner of the window instead of the client area.

    GetWindowDC assigns default attributes to the window device context each time it retrieves the device context. Previous attributes are lost.

    HDC GetWindowDC(
    HWND hWnd // handle of window
    );

    Parameters
    hWnd
    Handle to the window with a device context that is to be retrieved. If this value is NULL, GetWindowDC retrieves the device context for the entire screen.
    Windows 98, Windows NT 5.0 and later: If this parameter is NULL, GetWindowDC retrieves the device context for the primary display monitor. To get the device context for other display monitors, use the EnumDisplayMonitors and CreateDC functions.

    Return Values
    If the function succeeds, the return value is the handle of a device context for the specified window.

    If the function fails, the return value is NULL, indicating an error or an invalid hWnd parameter.
    GetDC
    The GetDC function retrieves a handle to a display device context for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the device context.

    The GetDCEx function is an extension to GetDC, which gives an application more control over how and whether clipping occurs in the client area.

    HDC GetDC(
    HWND hWnd // handle to a window
    );

    Parameters
    hWnd
    Handle to the window whose device context is to be retrieved. If this value is NULL, GetDC retrieves the device context for the entire screen.
    Windows 98, Windows NT 5.0 and later: If this parameter is NULL, GetDC retrieves the device context for the primary display monitor. To get the device context for other display monitors, use the EnumDisplayMonitors and CreateDC functions.

    Return Values
    If the function succeeds, the return value identifies the device context for the specified window's client area.

    If the function fails, the return value is NULL.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Megatron
    Guest
    Getting the DC is easy:
    Code:
    HDC hDC = GetDC(hWnd);
    
    // Code for working with DC here
    
    ReleaseDC(hWnd, hDC);
    To get the hwnd of a window you didn't create, use FindWindow or FindWindowEx, otherwise you could use the return value of CreateWindowEx.

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Also if you are using dialogues then to get the hwnd of some item (window) on the dialog you can use
    GetDlgItem
    The GetDlgItem function retrieves the handle of a control in the specified dialog box.

    HWND GetDlgItem(
    HWND hDlg, // handle of dialog box
    int nIDDlgItem // identifier of control
    );

    Parameters
    hDlg
    Identifies the dialog box that contains the control.
    nIDDlgItem
    Specifies the identifier of the control to be retrieved.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562

    Wink Thanks

    Thanks guys. Thats all the help I wanted
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width