16

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?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
nhaa123
  • 9,570
  • 11
  • 42
  • 63

6 Answers6

28

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
YeenFei
  • 3,180
  • 18
  • 26
12

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

Cogwheel
  • 22,781
  • 4
  • 49
  • 67
  • 9
    Specifically, `GetDeviceCaps(hdc, HORZRES)` and `GetDeviceCaps(hdc, VERTRES)` are most likely what's desired. – TheUndeadFish Jul 01 '10 at 02:16
  • 4
    GetDeviceCaps(hdc,*) returns the screen dimensions regardless of the window size. KevenK and msandiford's GetClientRect(WindowFromDC(hdc),&r) does the trick though. – AbePralle Dec 27 '13 at 01:05
7

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;
Claudiu
  • 91
  • 1
  • 1
  • 2
    This will only work when DC is associated with a window. For other cases (e.g. DC obtained from CreateCompatibleDC()) WindowFromDC() will return NULL. – Archie May 26 '17 at 08:10
2

As a disclaimer, I know nothing about GDI or what you have to work with in your application. I'm just trying to be helpful if possible.

That said, I found a link which seems to suggest that it's appropriate to use GetClientRect to get the size of the drawing area:

RECT clientRect;

GetClientRect(hWnd,&clientRect);

http://www.toymaker.info/Games/html/gdi.html#winsize

KevenK
  • 2,975
  • 3
  • 26
  • 33
2

You could WindowFromDC(...) to get the DC's window if it's associated with a window. You could then use @KevinK's answer to get the client rect from this.

clstrfsck
  • 14,715
  • 4
  • 44
  • 59
1

but if get Calculator' window_dc dimension, it will failed at “GetCurrentObject” or "GetObject", i think maybe the window attribute include "ws_ex_noredirectionbitmap", how to get dismension in this case?

HDC win_dc = ::GetWindowDC(hwnd);
BITMAP bm = { 0 };
HGDIOBJ hBitmap = GetCurrentObject(win_dc, OBJ_BITMAP);
if (hBitmap)
{
    if (GetObject(hBitmap, sizeof(BITMAP), &bm))
    {
        windc_dimension.cx = bm.bmWidth;
        windc_dimension.cy = bm.bmHeight;
    }
}
fredirty2017
  • 103
  • 11
  • The client are is invisible for [WS_EX_NOREDIRECTIONBITMAP](https://stackoverflow.com/questions/38179033/when-should-ws-ex-noredirectionbitmap-be-used). – Laurie Stearn Mar 30 '20 at 04:56