11-19-2013 10:02 AM
Hi,
I know that I can get the resolution of the monitor, and with that set the display size of a control in a GUI. However, the real size (in centimeters) depends actually on the sizes (in centimeters) of the screen. Can I get that, programatically?
Solved! Go to Solution.
11-19-2013 10:22 AM
No. Screen resolution refers to the total number of pixels, not to the pixel density.
11-20-2013 04:37 PM
You should be able to use the Windows SDK GetDeviceCaps function to convert between pixels and physical coordinates for your monitor. Something like this:
dc = GetDC (NULL);
xPixelsPerCentimer = GetDeviceCaps (dc, LOGPIXELSX) / 2.54;
yPixelsPerCentimer = GetDeviceCaps (dc, LOGPIXELSY) / 2.54;
ReleaseDC (dc);
(I divided by 2.54 in order convert from inches to centimeters).
Once you have this ratio, you'll know how many pixels to use when you size the control.
Luis
11-21-2013 12:41 AM
Hi Sincrono,
sorry for providing the wrong answer, I didn't know better Thanks to Luis I have learnt more about the Win API
11-25-2013 09:26 AM
As my teacher said: "Everything is possible with C programming"
Thanks for the answer, that is definetly the solution.
11-26-2013 12:29 PM
After writing #include <windows.h> at the beginning of the program, I wrote the following function called by the main.
void GeometriaGUI(){ HDC dc; double xPixelsPerCentimer, yPixelsPerCentimer,xPixelsPerMilimeter, yPixelsPerMilimeter;; dc = GetDC (NULL); xPixelsPerCentimer = GetDeviceCaps (dc, LOGPIXELSX) / 2.54; yPixelsPerCentimer = GetDeviceCaps (dc, LOGPIXELSY) / 2.54; xPixelsPerMilimeter= GetDeviceCaps (dc, HORZRES)/GetDeviceCaps (dc, HORZSIZE); yPixelsPerMilimeter= /*GetDeviceCaps (dc, VERTRES)/GetDeviceCaps (dc, VERTSIZE); //ReleaseDC (dc); SetCtrlAttribute (panelHandle, PNL_INSTR_2, ATTR_HEIGHT, (int)(40.0*yPixelsPerMilimeter)); SetCtrlAttribute (panelHandle, PNL_INSTR_2, ATTR_WIDTH, (int)(40.0*yPixelsPerMilimeter)); }
First, I eliminated the ReleaseDC line because it caused an error "Type error in argument 1 to `ReleaseDC'; found 'HDC' expected 'HWND'." at compile time.
If I use xPixelsPerCentimeter to set the size of a control in the GUI, and set a value of 4*xPixelsPerCentimeter, I get, after measuring with a ruler, only 3.5 cm.
If I use 40*xPixelsPerMilimeter, I get a size of 1.8 cm. In both cases, 4 cm are expected.
If I look at the values given the Windows sdk functions, the ones given by the resolution are atribute are fine (1280 x 800). However, the ones given by HORZSIZE and VERTSIZE attributes don't match what I measure physically. The sizes given are larger than the computer itself (a notebook with only 1 monitor).
The idea is that the size of certain controls are constant across any monitor used. But the function, unfortunately, is not giving the expected results.
12-03-2013 08:41 PM
The code, though should work on old versions of windows, doesn't work anymore in newer ones because of (for me) stupid reasons.
In this page (solution #5)
lies an answer to get the physical size of the screen programatically and correctly.
However, I can't implement it. The first lines (both include statements) show errors. atlstr.h is not found (nor can i find it on my pc), and SetupAPI.h shows several errors inside the .h file, which of course has not been modified.
This problem will definetly not beat me, but I need you help.
I suspect that my version of Labwindows (2009) is not new enough or something like that.
12-04-2013 03:54 AM
Hi,
I got it working on my system (Win7Pro SP1) with a few adaptations.
Attached a small sample project that creates a decoration sized in centimeters: you can use it as a basis for your actual needs.
Basically, atlstr.h is only required for some string handling which is not needed (as mentioned by the author of the article you linked). Also, you need to include windows.h too for some declarations and add setupapi.lib to the project. Besides that, I only made minor changes: original lines are commented out immediately before the actual lines to be executed.
12-04-2013 02:09 PM
Now it is definetly solved. I only had to extract de relevant part of the code and paste it where i wanted, after including the SetupApi library and the header files in the right order.
12-04-2013 02:13 PM
Thanks RobertoBozzolo !!
Maybe a moderator can 'move' the actual solution (that worked for me) to your post.