tags:

views:

545

answers:

4

Is it possible to get the screen pixel resolution in mm using Win32 APIs ? I have an application which is showing 0.3472222222222222 as the value for this on my 1280x1024 monitor with 96 dpi . But I am not able to find out how it got this value. Any clues will be helpful. I can use MFC also if required.

EDIT Sorry for the confusion, the software I am talking about was not using the current screen resolution. It was reading it from some config file.

A: 

Use the GetDC function to get a handle on the monitor, then call the GetDeviceCaps function to get the size of the monitor in millimeters. For example:

HDC monitor = GetDC( NULL );
int horizSize = GetDeviceCaps( monitor, HORZSIZE );
int vertSize = GetDeviceCaps( monitor, VERTSIZE );
Frerich Raabe
+2  A: 

0.3472222222222222 mm per pixel is in fact equivalent to approximately 73 dpi. Windows uses two standard settings 72dpi and 96dpi, but custom values are also supported. These are nominal values and may not bear any relationship to the physical screen. For example it is possible to have a physically 96dpi screen set to 72dpi, and this will affect the scaling of images and layout in various applications as well as the size of system fonts and icons.

The default for Windows is 72dpi, and I have found that some applications (often in their "About" and dialog boxes) do not render correctly when set to other values. If your application reports 0.34, it seems likely that it is set to 72dpi or a custom value regardless of physical resolution. When set to match the physical resolution, the page width in say Word for example when set to a zoom level of 100% will match the physical paper size. Since this metric cam be set by teh end user, it is not directly related to the actual resolution.

Clifford
+6  A: 

Get device caps for the monitor gives you both size in pixels and mm thus:

HDC screen = GetDC(NULL);
int hSize=GetDeviceCaps(screen,HORZSIZE);
int hRes=GetDeviceCaps(screen,HORZRES);
float PixelsPerMM=(float)hRes/hSize;   // pixels per millimeter
float PixelsPerInch=PixelsPerMM*25.4; //dpi
Elemental
but those are logical inches isn't it?
Ponting
There are 25.4 millimeters per inch -- not 2.54.
Spire
Thank you - edited to be correct
Elemental
Returns wrong resolution for me. Windows 7 - debug mode
tm1rbrt
Ponting is right in his comment above that this gives logical inches which isn;t really a unit in the ordinary sense.
Elemental
+1  A: 

The section on writing "DPI-aware" applications in MSDN is well worth a read for anyone interested in providing decent support for variable display resolutions. APIs for obtaining relevant device & system metrics are described here.

timday