Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current DPI of a system in MFC Application?

Tags:

visual-c++

mfc

I have an existing MFC application which runs fine in default DPI ( 96 dpi) in Windows 7. But when I increase the DPI by 150 % the UI gets distorted. I have fixed issues using scroll bars at certain level and referred to msdn article. I am wondering how can I get the current DPI of a system using MFC code so that set the height and widht of a dialog.

Please suggest!!

like image 483
Ashish Ashu Avatar asked May 13 '11 12:05

Ashish Ashu


1 Answers

First you need to get the device context for your screen. This is easy, just call GetDC, like this:

HDC screen = GetDC(0);

Then you ask for the device capabilities of that device context. In your case, you need the pixels along the X- and Y-axis per inch:

int dpiX = GetDeviceCaps (screen, LOGPIXELSX);
int dpiY = GetDeviceCaps (screen, LOGPIXELSY);

(see http://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx for more information about GetDeviceCaps).

Finally, release the device context again:

ReleaseDC (0, screen);
like image 56
Patrick Avatar answered Sep 18 '22 12:09

Patrick