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++
Printable View
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++
To get the HWND use FindWindow and FindWindowEx. I'm sure you know how to use them from VB.
To get the DC you need the HWND of the window and then use GetDC or GetWindowDCQuote:
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.
Quote:
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.
Quote:
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.
Getting the DC is easy:
To get the hwnd of a window you didn't create, use FindWindow or FindWindowEx, otherwise you could use the return value of CreateWindowEx.Code:HDC hDC = GetDC(hWnd);
// Code for working with DC here
ReleaseDC(hWnd, hDC);
Also if you are using dialogues then to get the hwnd of some item (window) on the dialog you can use
Quote:
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.
Thanks guys. Thats all the help I wanted