GetDC question

G

Guest

I need to use GetDC but I don't know how to find a parameter to pas to the
function. This is the GetDc from MSVC help:

**********
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.

***********

HWND hWnd // handle to a window -- THIS IS MY PROBLEM.

Where or how can I find it ?

I'm working in a global function and I havent any hWnd of any type.
 
T

Tamas Demjen

mark said:
HWND hWnd // handle to a window -- THIS IS MY PROBLEM.

Where or how can I find it ?

I'm working in a global function and I havent any hWnd of any type.

It depends what you want to do:

1. GetDC(0) gets a DC to the screen. Don't forget to call ReleaseDC.
2. CreateCompatibleDC(0) creates a new DC that's compatible with the
screen's bit depth. This is what you usually do for rendering off-screen
bitmaps or something like that. Don't forget to call DeleteDC.
3. CreateDC to create a DC for printers. Use DeleteDC in the end.
4. If you paint somewhere in a widget, the DC is usually provided by the
GUI toolkit.

Perhaps your function should be generic enough to work with any DC, and
thus the DC should be a parameter. Be careful, because DCs may change
during the lifetime of an application. Avoid storing a DC permanently,
always obtain it right before painting.

You have to provide more specific details if you need more help, and
maybe post your question to a graphics or GDI related group.

Tom
 
G

Guest

Tamas Demjen said:
It depends what you want to do:

1. GetDC(0) gets a DC to the screen. Don't forget to call ReleaseDC.
2. CreateCompatibleDC(0) creates a new DC that's compatible with the
screen's bit depth. This is what you usually do for rendering off-screen
bitmaps or something like that. Don't forget to call DeleteDC.
3. CreateDC to create a DC for printers. Use DeleteDC in the end.
4. If you paint somewhere in a widget, the DC is usually provided by the
GUI toolkit.

Perhaps your function should be generic enough to work with any DC, and
thus the DC should be a parameter. Be careful, because DCs may change
during the lifetime of an application. Avoid storing a DC permanently,
always obtain it right before painting.

You have to provide more specific details if you need more help, and
maybe post your question to a graphics or GDI related group.

Tom

Thank you Tom

Ok.
I need to create a DIBSection and below there is a piece of my code ( Idon't
know if it is right).

***
HBITMAP Bitmap;
int Width, Height;
HDC WindowDC, MemDC;
BITMAPINFO BitmapInfo;
unsigned char *Data;

HWND hwnd = NULL;
CRect rect;
WindowDC = GetDC(hwnd);

Width = image_width;
Height = image_height;

memset(&BitmapInfo.bmiHeader, 0, sizeof(BitmapInfo.bmiHeader));
BitmapInfo.bmiHeader.biWidth = Width;
BitmapInfo.bmiHeader.biHeight = -Height;
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 24;
BitmapInfo.bmiHeader.biCompression = BI_RGB;

MemDC = CreateCompatibleDC(WindowDC);
Bitmap = CreateDIBSection(WindowDC, &BitmapInfo, DIB_RGB_COLORS, (void
**)&Data, 0, 0);

***

how you can see I'm using "hwnd = NULL" that is the full screen. I need only
the window region of the application. What I have to do to put to hwnd the
right value to do that?
 
T

Tamas Demjen

mark said:
MemDC = CreateCompatibleDC(WindowDC);
Bitmap = CreateDIBSection(WindowDC, &BitmapInfo, DIB_RGB_COLORS, (void
**)&Data, 0, 0);

I simply use WindowDC = 0, and it works just fine. The first parameter
for CreateDIBSection is not used anyway, unless the 3rd parameter is
DIB_PAL_COLORS. You're passing DIB_RGB_COLORS, which means WindowDC is
irrelevant. I think you'll be fine with WindowDC = 0. If you're
compatible with the screen, you're compatible with the current window.
If you have concerns, you can always pass the current window handle to
your function.

Tom
 
G

Guest

Tamas Demjen said:
If you have concerns, you can always pass the current window handle to
your function.

Tom

The question is that i don't know how to obtain the current window handle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top