How do I get the current DPI of a system in the MFC application?


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!!

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 for more information about GetDeviceCaps).

Finally, release the device context again:

ReleaseDC (0, screen);