|
-
Sep 24th, 2001, 04:44 AM
#1
Thread Starter
Fanatic Member
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
-
Sep 24th, 2001, 06:46 AM
#2
Frenzied Member
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.
-
Sep 24th, 2001, 02:37 PM
#3
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.
-
Sep 24th, 2001, 05:36 PM
#4
Frenzied Member
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.
-
Sep 25th, 2001, 05:40 AM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|