Sign in to follow this  
orcfan32

What is HDC?

Recommended Posts

orcfan32    100
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.

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
Nik02    4348
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]

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
orcfan32    100
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.

Share this post


Link to post
Share on other sites
Auron    328
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 CreateWindow
TextOut(dc, 20, 20, "this is a test", 14);
ReleaseDC(hWnd, dc); // You have to release it afterwards



Hope this helps.

-Auron

Share this post


Link to post
Share on other sites
orcfan32    100
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++?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this