LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Can I set the size of a GUI control in centimeters?

Solved!
Go to solution

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?

0 Kudos
Message 1 of 10
(5,197 Views)

No. Screen resolution refers to the total number of pixels, not to the pixel density.

0 Kudos
Message 2 of 10
(5,194 Views)

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

 

Message 3 of 10
(5,169 Views)

Hi Sincrono,

 

sorry for providing the wrong answer, I didn't know better Smiley Wink Thanks to Luis I have learnt more about the Win API Smiley Happy

0 Kudos
Message 4 of 10
(5,160 Views)

As my teacher said: "Everything is possible with C programming"

 

Thanks for the answer, that is definetly the solution.

0 Kudos
Message 5 of 10
(5,126 Views)

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.

0 Kudos
Message 6 of 10
(5,096 Views)

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)

http://ofekshilon.com/2011/11/13/reading-monitor-physical-dimensions-or-getting-the-edid-the-right-w...

 

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.

 

0 Kudos
Message 7 of 10
(5,044 Views)
Solution
Accepted by topic author Sincrono

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.

 

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 8 of 10
(5,035 Views)

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.

0 Kudos
Message 9 of 10
(5,011 Views)

0 Kudos
Message 10 of 10
(5,008 Views)