Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out DC's dimensions?

Tags:

c++

winapi

gdi

Let's say I have a handle to device context (naturally, in Windows environment):

HDC hdc;

How can I get the width and height of it?

like image 630
nhaa123 Avatar asked Jul 01 '10 01:07

nhaa123


3 Answers

A device context (DC) is a structure that defines a set of graphic objects and their associated attributes, and the graphic modes that affect output.

By width and height I'm guessing you are referring to the bitmap painted ?
If so then i guess you can try the following :

BITMAP structBitmapHeader;
memset( &structBitmapHeader, 0, sizeof(BITMAP) );

HGDIOBJ hBitmap = GetCurrentObject(hDC, OBJ_BITMAP);
GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader);

//structBitmapHeader.bmWidth
//structBitmapHeader.bmHeight
like image 92
YeenFei Avatar answered Oct 12 '22 01:10

YeenFei


I also know little about GDI, but it seems GetDeviceCaps might do the trick.

like image 39
Cogwheel Avatar answered Oct 12 '22 02:10

Cogwheel


This simple piece of code I use always to get the dimensions of the rendering area, when I have only the HDC. First, you must get a HWND from the HDC - is simple, then you can get the client rect of this HWND:

RECT    rcCli;          
GetClientRect(WindowFromDC(hdc), &rcCli);
// then you might have: 
nWidth = rcCli.right-rcCli.left; 
nHeight  = rcCli.bottom-rcCli.top;
like image 24
Claudiu Avatar answered Oct 12 '22 01:10

Claudiu