LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

I need to find if string is too long for a 'Text Message' control, so I can take corrective action.

I've searched the Knowledge base, and most of the emails regarding string truncation do not actually determine the number of PIXELS wide (and high) a string will be on the screen (#see below).

If I could get the PIXEL-length (not character-string-length) of a string, that would be *good*.

EXAMPLE:
========

Say I have the following C string:
"c:\directory\directory\directory\very_long_file_name.bin"

If I try to fit it into a too-small "Text Message" control (not a "Text Box"), I get this:

|c:\directory\directory\directory\very|

What I want to do is:

1. I can get the PIXEL-size of the Text Message Control. I can
do this with GetCtrlAttribute(ATTR_WIDTH), eg:

ret = GetCtrlAttribute(panel, PANEL_FILE, ATTR_WIDTH, &Attr_Width);

and Attr_Width I get back is, say, 300 pixels.

2. I now want to know what the PIXEL-length of my string is. I cannot find if there is a LW/CVI function that does this.

*** I can get the POINT size.
*** I can get the number of characters if I've already written a string to the Text Message control.

3. in my code, I want to be able to say:

if ( PIXEL_LENGTH(string) > Attr_Width )
try shortening the string

for example, I can create a temporary string and replace the directory path with 3 dots ("...") using fnsplit()/fnmerge():

fnsplit ( strn, drive, dir , fname, ext );
fnmerge ( temp, drive, "...", fname, ext );

and I can retry Step 2, which give us:

|c:\...\very_long_file_name.bin |

2. this time the PIXEL size is, say, 250 pixels, so everything
is OK


---------------------------------------------------
#typical examples:
Fmt function truncates output buffer at 64K bytes. If the following code is executed: static double farray[100000]; static char buffer[100000] Fmt(buffer, '%s 0BKGLKTL
Message 1 of 4
(3,581 Views)
Alf,

You can use the function GetTextDisplaySize to get the pixel information of the string.

You'll need to supply the font that the text message control is using (ATTR_TEXT_FONT). The font needs to be a metafont, which by default it is. However, if you have changed any of the font attributes on the text message, you'll need to obtain all of the font attributes from the control (ATTR_TEXT_POINT_SIZE, ATTR_TEXT_BOLD, ATTR_TEXT_FONT, ...) and then use them to create your own metafont (CreateMetaFont).

Luis Gomes
NI
Message 2 of 4
(3,581 Views)
Thanks, Luis,

I spent about 1 1/2 hours poring over the documentation for GetCtrlValue() and GetCtrlAttribute() and I felt I was just hitting a brick wall! I *knew* there had to be an answer, but I just couldn't find it.

Next time I'll try looking in the right place!

Best Regards,
Alf Lacis
mailto:alfl@voxson.com.au
http://www.voxson.com.au/
0 Kudos
Message 3 of 4
(3,581 Views)
You can create other hidden text control and insert your text. You must set the control size to fit the text, the you can get the length of the control, so you can get near the length of the text in pixels.
0 Kudos
Message 4 of 4
(3,581 Views)