What is HDC?

Started by
7 comments, last by Colin Jeanne 18 years, 9 months ago
I'm trying to get some text to be printed to a window in C++, but the PrintText() function isn't working as planned: MSDN says that HDC is "Used to handle to the device context." and I really don't understand what that means.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Advertisement
A device context in GDI is an abstraction of something that you can use to do graphics. Examples include a window's draw area or a printer. A handle to a device context is something like a pointer to the device context except you don't dereference it, you just use it as an argument for GDI functions to tell them when device context you want to manipulate.
Windows keeps an internal list of drawable memory areas called Device Contexts. The HDC:s are just integers, functioning as keys (or "Handles") to this internal list.

A window generally has a device context allocated by Windows, so that the user mode applications (such as the application that created the window) can draw to it via the device context handle.

Windows uses many more handle types beside HDC:s to let applications access it's internal resources from user level, for example HWND for windows, HMODULE for executables in memory, HINSTANCE for process instances etc.

EDIT: And what SiCrane said [smile]

Niko Suni

So you're saying that it can be an integer? Such as 1, or 4, or even 219?
It must be cast to an integer?
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
What it actually is doesn't matter. Just treat it as a magic baton that you pass back and forth. Internally it may be an index into a lookup table, a pointer or a hash id, but you shouldn't care. Just use the HDC type to store it.
Well, I'm using it here:
  TextOut(NULL, 20, 20, "this is a test", 14);


I have it defined in a separate .cpp file, but it says
[Linker error] undefined reference to `TextOutA@20'
I'm changing the HDC constantly with different numbers and stuff, but it won't compile.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Well, the HDC isn't your problem at the moment. It's giving you an undefined reference error, which means that you haven't linked in the libraries necessary to use the TextOut function. I think you need to link in the gdi32.lib library, but I may be corrected on this.

After that, you need to change that first parameter to use a device context for your window. Since the device context is how Windows will know which window to draw to, it's important that you pass it a useful value. What you'd probably want to do is the following:

HDC dc = GetDC(hWnd); // hWnd is the window handle you got back from CreateWindowTextOut(dc, 20, 20, "this is a test", 14);ReleaseDC(hWnd, dc); // You have to release it afterwards


Hope this helps.

-Auron
But what if you aren't in a project file? I'm just using some cpp files, so I can't link libraries...can I? And also, does that dll come with Dev-C++?
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
For Dev-C++ you'll need to link gdi32.a using -lgdi32 in your linker options.

This topic is closed to new replies.

Advertisement