LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Getting the contents length of a numeric control

I have a panel with a graph control on it, and several numeric controls. The panel scales its contents on resize, however, the font size of the controls is reset to the original size after every panel size.
 
If the panel is made pretty small, the contents of the numeric controls aren't visible completely anymore. I want to be able to check when this happens, so I can either resize the control or adjust the precision for the contents to fit again.
 
There is a way to get the width of the control, but the width of the contents remain amystery... Any ideas?
0 Kudos
Message 1 of 2
(2,763 Views)
It's not an easy thing to do: when scaling a panel, text point size of controls scales too, so you may retrieve it and use in your calculations. GetTextDisplaySize gives you the text lenght provided you create a metafont with the appropriate font name and size:
 
 GetCtrlAttribute (panel, PANEL_NUMERIC, ATTR_TEXT_POINT_SIZE, &size);
 GetCtrlAttribute (panel, PANEL_NUMERIC, ATTR_TEXT_FONT, font);
 CreateMetaFont ("dummy", font, size, 0, 0, 0, 0);
 GetCtrlVal (panel, PANEL_NUMERIC, &val);
 sprintf (msg, "%.2f", val);
 GetTextDisplaySize (msg, "dummy", NULL, &Twidth);
Twidth is the text width in pixels.

This works well if the panel maintains original proportions between its width and height, otherwise the controls are stretched so that returned text lenght does not meet actual text size (you could have texts high and compressed or on the contrary spread over the screen but very reduced in height). In this case you must calculate panel proportion and compare with original proportions adapting text width to actual panel shape. The code in this case must be extended a little (origRatio is the original proportion of the panel dimensions when loaded and must be saved in a global variables);
 
 GetCtrlAttribute (panel, PANEL_NUMERIC, ATTR_TEXT_POINT_SIZE, &size);
 GetCtrlAttribute (panel, PANEL_NUMERIC, ATTR_TEXT_FONT, font);
 CreateMetaFont ("dummy", font, size, 0, 0, 0, 0);
 GetCtrlVal (panel, PANEL_NUMERIC, &val);
 sprintf (msg, "%.2f", val);

 GetTextDisplaySize (msg, "dummy", NULL, &Twidth);
 GetPanelAttribute (panel, ATTR_WIDTH, &width);
 GetPanelAttribute (panel, ATTR_HEIGHT, &height);
 ratio = (double)width / (double)height;
 Twidth *= ratio / origRatio;

 
I don't know if the continuous creation of metafonts causes memory overload or memory leaks due to resources not released, but since there isn't a DestroyMetafont function it can be assumed that there is no problem acting this way, but this should be investigated a little.


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?
0 Kudos
Message 2 of 2
(2,750 Views)