Label Caption Truncates 
Author Message
 Label Caption Truncates

I set the label for autosize and and use various fonts and styles.
Left justified, Flat, Borderless, transparent used.
Sometime the last letter in the caption is partially cut off.
Is there a way to automatically make the label the correct width?
Maybe a sendmessage?



Tue, 10 Jul 2012 06:09:01 GMT  
 Label Caption Truncates
I forgot to say I am trying to get the real width of the text so I can scale
it for printing to a picturebox.
So what other methods are available to get the width of the text.
Various fonts and styles are being used.
Quote:

> I set the label for autosize and and use various fonts and styles.
> Left justified, Flat, Borderless, transparent used.
> Sometime the last letter in the caption is partially cut off.
> Is there a way to automatically make the label the correct width?
> Maybe a sendmessage?



Tue, 10 Jul 2012 07:48:02 GMT  
 Label Caption Truncates
on 1/21/2010, Bee supposed :

Quote:
>> I set the label for autosize and and use various fonts and styles.
>> Left justified, Flat, Borderless, transparent used.
>> Sometime the last letter in the caption is partially cut off.
>> Is there a way to automatically make the label the correct width?
>> Maybe a sendmessage?

> I forgot to say I am trying to get the real width of the text so I can scale
> it for printing to a picturebox.
> So what other methods are available to get the width of the text.
> Various fonts and styles are being used.

That might've been a better question.

How about: Picture1.TextWidth("Some Text") ?

--
.NET: It's About Trust!
http://vfred.mvps.org



Tue, 10 Jul 2012 07:53:03 GMT  
 Label Caption Truncates
I tried something like that and got a strange number that did not scale
(scalemode) to anything that made sense.
I put the text into the label with autosize set.
The label sits on a pictureboxA.
So I did
   lWidth= me.textwidth(label)
Me (form) is vbTwips
The picturebox is vbPixels
But the relationship between the label.width and lWidth does not compute to
any scale factor relationship I can think of.
I though that label.width was in twips.

I am trying to scale it to pictureboxB by using a scalefactor of
pictureboxB.scalewidth/pictureboxA.scalewidth to compute the fontsize of the
print into pictureboxB.
Compute is really a loop, increasing the fontsize of a another label until
its width is the new scaled width.
It gets close using the label.width but is not proportional the same way.
i.e. for larger fonts I fudge by .95
for smaller fonts it like 1.05
Maybe this is all noise to you but I am thinking "out loud".  sort-a.

Quote:

> on 1/21/2010, Bee supposed :
> >> I set the label for autosize and and use various fonts and styles.
> >> Left justified, Flat, Borderless, transparent used.
> >> Sometime the last letter in the caption is partially cut off.
> >> Is there a way to automatically make the label the correct width?
> >> Maybe a sendmessage?

> > I forgot to say I am trying to get the real width of the text so I can scale
> > it for printing to a picturebox.
> > So what other methods are available to get the width of the text.
> > Various fonts and styles are being used.

> That might've been a better question.

> How about: Picture1.TextWidth("Some Text") ?

> --
> ..NET: It's About Trust!
> http://vfred.mvps.org

> .



Tue, 10 Jul 2012 09:14:01 GMT  
 Label Caption Truncates

Quote:
> I set the label for autosize and and use various fonts
> and styles. Left justified, Flat, Borderless, transparent
> used. Sometime the last letter in the caption is partially
> cut off. Is there a way to automatically make the label
> the correct width? Maybe a sendmessage?

The reason why the rightmost character is sometimes cut off (and why the
leftmost character is also sometimes cut off) is because Label Controls and
TextBoxes and some other things into which you can insert text do not take
account of the fact that the glyph (the actual drawn part) of some
characters in some fonts at some font settings actually fall outside of the
character cell in which they "live". In such cases the drawn shape of the
character glyph hangs outside either the right side or the left side (or
sometimes both sides) of the character cell. The RichTextBox also used to
behave in this manner, but I've noticed that current RTBs actually do
perform their wrapping correctly regarding the right overhang of the
rightmost character of a line, although even they do still have problem with
the left overhang of the leftmost character of a line. (Incidentally, There
are also fonts in which some characters overhang either the top or the
bottom of the character cell, but I won't go into that here because it is
unlikely to be a problem for you). You can have exactly the same alignment
and clipping problem when printing text into constrained areas as well. This
applies in smaller or larger degrees to a number of different characters in
a number of different fonts, with perhaps the most obvious one usually being
the italic "f", which in many fonts overhangs both the left and the right
sides of its cell. For example, set a Label Control's Autosize property to
True and its font to Times New Roman size 72 and set its Caption property to
"fluff". You should easily be able to see what I mean.

Before you can properly compensate for this you need to discover the exact
amount (if any) by which the specific leftmost character overhangs the left
edge of its character cell and also the exact amount (if any) by which the
rightmost character overhangs the right edge of its character cell. There
are a couple of ways of doing this. One way which will tell you the precise
vertical and horizontal position of the glyph of a character relative to the
top left corner of its character cell and also the overall total width and
heigh of the drawn glyph (what is called the "black box" measurements) is to
use the GDI GetGlyphOutline function. A slightly simpler way which gets just
the left and right overhang (and one or two other things collectively known
as the "ABC  widths") is to use the GDI GetCharABCWidths function. Rather
than explain it all here, perhaps you might like to have a look at:

    http://msdn.microsoft.com/en-us/library/dd183418(VS.85).aspx

Once you have established the amount by which the rightmost character of
your Caption overhangs its cell then you can set the width of the Label
Control accordingly. There are a number of ways of doing this, but one way
would be to set its Autosize property to True whilst first setting the
Caption (so as to easily allow it to set it height) and then setting its
Autosize property back to false. Then read the width of the Caption
(possibly by setting an otherwise unused pictureBox to the same font and
using its TextWidth method). This will give you the total width of the
character cells, to which you can then add the amount of right overhang of
the rightmost character and then set the Label's Width property accordingly.
The problem with this of course is that it does not fix the left overhang of
the leftmost character (if that character actually does have a left
overhang). If you were printing text then this too is easily accounted for,
but for a Label Control you can't really do anything to stop the Label
cutting off the left edge of such a character without introducing other
problems. If you never use Captions which have a leftmost character that
overhangs the left side of its cell then of course this doesn't matter, but
if you do then perhaps the easiest way to overcome it it to simply use a
leading space character with the Caption. Alternatively of course you can
simply draw all your text onto the Form (or whatever) instead of using Label
Controls, which will allow you to read the appropriate data using either
GetGlypgOutline or GetCharABCWidths and then position it anywhere you wish,
but if you want to retain the simplicity of using Labels you will need to do
the things I have said. Otherwise, you can of course simply avoid this
problem entirely by using both a leading and a trailing space with the
Labels's Caption. That in fact might be the simplest solution for you if you
do not wish to go into too much detail.

One thing that you may (or may not) discover whilst playing with the Labels
is the fact that on some systems performning a Cls on the Label's container
(perhaps the Form) immediately after settings the Autosize Label's font and
size and Caption actually does cause the Label to then properly display both
the leading and the trailing overhangs of its leftmost and rightmost
characters (this is just something I discovered whilst playing about with it
myself). However, this is not documented behaviour and it is quite likely
not going to happen on many systems. Also, it is not persistent anyway, with
either the left or right overhang portions disappeaing as the Form is moved
on and off the display. So, I mention this in passing just in case you
discover it yourself and are tempted to use it, which I would advise you not
to.

Mike



Tue, 10 Jul 2012 09:23:26 GMT  
 Label Caption Truncates
Bee formulated on Thursday :

Quote:
> I tried something like that and got a strange number that did not scale
> (scalemode) to anything that made sense.
> I put the text into the label with autosize set.
> The label sits on a pictureboxA.
> So I did
>    lWidth= me.textwidth(label)
> Me (form) is vbTwips
> The picturebox is vbPixels
> But the relationship between the label.width and lWidth does not compute to
> any scale factor relationship I can think of.
> I though that label.width was in twips.

It is, yes.  Tell ya what.  Rather than trying "something like" what I
suggested, why don't you actually try *what* I suggested?

Quote:
> I am trying to scale it to pictureboxB by using a scalefactor of
> pictureboxB.scalewidth/pictureboxA.scalewidth to compute the fontsize of the
> print into pictureboxB.
> Compute is really a loop, increasing the fontsize of a another label until
> its width is the new scaled width.
> It gets close using the label.width but is not proportional the same way.
> i.e. for larger fonts I fudge by .95
> for smaller fonts it like 1.05
> Maybe this is all noise to you but I am thinking "out loud".  sort-a.

Whole lotta noise, yeah.  You might also find the ScaleX/Y methods
useful.

--
.NET: It's About Trust!
http://vfred.mvps.org



Tue, 10 Jul 2012 11:01:58 GMT  
 Label Caption Truncates

Quote:
> I am trying to scale it to pictureboxB

That's not the same question you asked initially.

Quote:
> So I did  . . . lWidth= me.textwidth(label)

I'm not sure if you understand what the above line actually does Bee, but
personally I think you need to read up on the TextWidth function.

Firstly, the TextWidth function requires a String as the argument and if (in
the above line) you mean you are passing it an actual Label Control then the
only reason it works is simply because the default property of a Label
Control happens to be its Caption property, which is of course a String. So,
if the Label is called Label1 and its Caption happens to be "Rum and Coke"
then Me.TextWidth(Label1) is the equivalent of Me.TextWidth("Rum and Coke").
So, if I have understood the details of your question correctly (if you
actually are passing a Label Control as the argument to the TextWidth
function) then the actual size of the Label itself and indeed the font name
or font size of the Label's font property are in this case totally
irrelevant. All you are passing to the TextWidth function is the simple
String "Rum and Coke" (in this specific example).

Secondly (again using the above specific example) the "Me" in the line
lWidth = Me.TextWidth(Label1) means you are asking VB to tell you the width
of the text "Rum and Coke" as it would be if it was printed in "Me" (the
Form). Therefore in this case it is the Form's font name and font size that
will be used by the TextWidth function to calculate the width of the printed
text. Also, because you are using "Me", the value returned by the TextWidth
function will be in the Scale Units of "Me" (the Form), so if the Form's
ScaleMode happens to be pixels then the value returned by Me.TextWidth (etc)
will also be in pixels.

There are always various different ways of doing things, but generally if
you want to know the printed width of a string of text as it would appear in
a specific object then you should use that specific object in the call to
TextWidth, having first set the object's ScaleMode and font name and font
size to the desired values. For example, if you want the pixel width of a
piece of 36 point standard Times New Roman text as it would be printed in a
PictureBox (for example Picture1) you would use:

    Picture1.ScaleMode = vbPixels ' or whatever else you wish
    Picture1.Font.Name = "Times new Roman"
    Picture1.Font.Italic = False
    Picture1.Font.Bold = False
    Picture1.Font.Size = 36
    wide = Picture1.TextWidth("some text")

(But see my further notes below regarding what the TextWidth function
actually returns, and the important things to take into account if for some
reason you actually instead want the overall width of the text exactly as it
would be printed from the leftmost pixel plotted to the rightmost pixel
plotted, which can be either wider or narrower than the value returned by
TextWidth).

Also, if you are then setting the Width of the PictureBox to exactly the
same as the value returned by the TextWidth function then there are a couple
more things to consider. The first one is that the Width property of the
PictureBox is in the Scale Units of its Container (usually the Form) whereas
the width value returned by the TextWidth function (in the above code) is in
the Scale Units of the PictureBox. Therefore if the ScaleMode of the Form is
not the same as the ScaleMode of the pictureBox then  you should make the
appropriate conversion, or alternatively you can temporarily set the form's
ScaleMode to be the same as the PictureBox's ScaleMode, at least whilst you
set the Width property of the PictureBox. As I've said, there are other ways
of doing these things, but they all effectively amount to the same thing and
main thing is that you understand the principles involved. The second thing
you need to consider is the thickness of the PictureBox borders (unless it
is a borderless pictureBox), because setting its Width actually sets its
overall width, including the border. If it has a border and if you set its
Width to the equivalent of (say) 120 pixels then the width of its client
area (the amount of space available inside its borders) will be slightly
less than 120 pixels and any text that is exactly 120 pixels wide will not
fit. The overall total effective width of the PictureBox borders is often
typically 4 pixels, but this can be different on different systems so you
would be better off calculating it in code. It is easy to calculate, but
post again if you have any problems with it.

Incidentally, I notice you are apparently using a Long in your own existing
call to TextWidth (lWidth = etc). This is fine if the ScaleMode happens to
be pixels or twips, but it is not suitable if the ScaleMode happens to be
some other units, such as points or centimeters or inches or whatever. In
such cases you should use a floating point variable.

Also, as explained in my previous response when I was responding to your
initial differently phrased question, and as I have briefly mentioned above,
the TextWidth function returns the overall printed width of the string of
character cells (that is to say, the distance from the left side of the
first character cell up to the right side of the last character cell as they
would be printed). This is almost always NOT exactly the same as the actual
printed width of the string of characters (from the leftmost pixel plotted
to the rightmost pixel plotted). If you are using standard text then this is
normally okay and you can safely ignore it, because in most cases standard
text characters fit totally within their individual character cells and so
the printed string will fit into its container even if the width of the
client area of the container is set to exactly the same as the value
returned by the TextWidth function, with perhaps a little bit of white space
at either end, which is normally both expected and acceptable. However, in
the same "client width set to textWidth" container (which I have a suspicion
you may be using and which is often used for example when fully justifying
text into a specified area or when creating and saving a bitmap of some
text), and especially if you are using an Italic font, then it is possible
for the leftmost character to overhang the left side of its cell and/or for
the rightmost character to overhang the right side of its character cell,
resulting in the actual printed text not fully fitting into the container.
In such cases you need to add the total amount of the left and right
overhang to the value returned by textWidth (you can determine the actual
value of the overhangs as explained in my response yesterday to your initial
question).

Again, I apologise for all the "long  winded waffle" in this response, but
it is hard to answer a question when the actual question has not been made
completely clear (as is the case in this thread) and so I had to cover as
much ground as possible without going too far over the top. It's hard to
know what to put in and what to miss out :-)

Mike



Tue, 10 Jul 2012 18:48:33 GMT  
 Label Caption Truncates

Quote:
> I set the label for autosize and and use various fonts and styles.
> Left justified, Flat, Borderless, transparent used.
> Sometime the last letter in the caption is partially cut off.
> Is there a way to automatically make the label the correct width?
> Maybe a sendmessage?

Ok, some people on this site would call me a hack for this suggestion,
but it works great.
Here is what I do to get the correct width of a label or a textbox,
which has no autosize:

First I have a autosized label that is the same font, fontsize and
fontstyle as the destination label / textbox EXCEPT:   label1.visible
= false.
Then:    label1.caption = "This is some sample text"
Then:    text1.width = label1.width
Then:    text1.text = label1.caption

If the width becomes too great for the space you have for the textbox
you can add:

If label1.width > MaxWidth then
      text1. multiline = true
      text1.height = text1.height * 2
end if

What do you think????

Duke.



Wed, 11 Jul 2012 10:27:30 GMT  
 Label Caption Truncates


Quote:
> Here is what I do to get the correct width of a label or
> a textbox, which has no autosize: First I have a autosized
> label that is the same font, fontsize and fontstyle as the
> destination label / textbox EXCEPT:   label1.visible = false.
>     Then:    label1.caption = "This is some sample text"
>     Then:    text1.width = label1.width
>     Then:    text1.text = label1.caption
> If the width becomes too great for the space you have for
> the textbox you can add:
> If label1.width > MaxWidth then
>      text1. multiline = true
>      text1.height = text1.height * 2
> end if
> What do you think????

That's one way of doing it, but it will work correctly only if both the
Label and the TextBox are borderless. If the border styles are different, or
even if they are the same but both fixed single, then it will not work
properly. Having said that, reading the TextWidth of a string by placing it
in a Autosize Label Control with the Label set to the desired font
attributes and reading the resultant Width of the Label (as you are
effectively doing in your example) is one of the many available ways of
effectively reading the TextWidth of the string (with the result being in
the scale units of the Label's container), although of course it will only
return the correct size if the text is going to be displayed or drawn onto
another screen object and not onto something else with different
characteristics, such as a printer. Using the TextWidth function of a Form
or PictureBox or Printer as appropriate (or the equivalent GDI function) is
in my opinion the best way.

It should be remembered though that the TextWidth function (and the
equivalent GDI function) tells you the width of the "row of character cells"
that the string will occupy when printed (from the left edge of the leftmost
character cell to the right edge of the rightmost character cell), and so if
actual printed width is important to you (the distance between the leftmost
pixel plotted and the rightmost pixel plotted) then you need to take other
things into account as I mentioned in my earlier response, because the
actual printed pixel width can be either smaller or greater than the
TextWidth, depending on the font used and the specific characters used at
the left and right ends of the string. In cases where the pixel printed
width is smaller than the TextWidth then of course it doesn't really matter
(and in fact the small white space at either end is usually actually
required), but in cases where the pixel printed width is greater than the
TextWidth then it often does matter. If, for example, you are creating and
saving a bitmap of some drawn text then you must take those things into
account, otherwise some of the drawn text at either the left edge or the
right edge (or both) may be missing in the bitmap (or may be clipped when
displayed in a TextWidth sized container).

The only reason I went into so much detail in my responses to Bee (the OP)
is because he did not make his question very clear and it appeared from what
he said that he did not really understand any of the methods involved, so
(in the absence of a clearly defined question) I wanted to give him enough
information to enable him to work things out for himself.

Mike



Wed, 11 Jul 2012 17:51:00 GMT  
 Label Caption Truncates

Quote:
> . . . reading the TextWidth of a string by placing it in a
> Autosize Label Control with the Label set to the desired
> font attributes and reading the resultant Width of the Label
> (as you are effectively doing in your example) is one of the
> many available ways of effectively reading the TextWidth
> of the string (with the result being in the scale units of the
> Label's container), although of course it will only . . .

Having just re-read what I wrote [above] I of course meant to include the
proviso that the Label should be borderless (and of course subject to the
other provisos I mentioned).

Mike



Wed, 11 Jul 2012 18:18:13 GMT  
 Label Caption Truncates
Sorry I did not get back sooner.
Mike, thanks for the clarification.
That all really helped me understand.
Quote:



> > . . . reading the TextWidth of a string by placing it in a
> > Autosize Label Control with the Label set to the desired
> > font attributes and reading the resultant Width of the Label
> > (as you are effectively doing in your example) is one of the
> > many available ways of effectively reading the TextWidth
> > of the string (with the result being in the scale units of the
> > Label's container), although of course it will only . . .

> Having just re-read what I wrote [above] I of course meant to include the
> proviso that the Label should be borderless (and of course subject to the
> other provisos I mentioned).

> Mike

> .



Thu, 12 Jul 2012 00:33:01 GMT  
 Label Caption Truncates

Quote:
> Sorry I did not get back sooner.
> Mike, thanks for the clarification.
> That all really helped me understand.

You're welcome. By the way, there is something else I wanted to mention, but
I thought it best to leave it until you had read all that other stuff I
posted just in case it caused more confusion rather than helping. It's to do
with the slightly inconsistent behaviour of an Autosize Label Control when
dealing with different kinds of font. If you are using it in the way you
suggested to effectively read the TextWidth of a string which you intend to
print or draw on the screen or a Form or a PictureBox then it returns the
correct result when using TrueType fonts whether they are italic or not, and
it also almost always returns the correct result when using "normal" screen
bitmap fonts. However, it usually returns an incorrect result when using
Italic screen bitmap fonts (an example of a screen bitmap font is MS Sans
Serif which is often the default on VB Forms, although many people do not
often use the italic version of it). The reason for the incorrect result is
partly because the ABC character width measurements (explained in the MSDN
link I posted the other day) actually mean something slightly different in
respect of screen bitmap fonts than they do for TrueType fonts (although
that fact is not mentioned or explained at all in the MSDN article) and
partly because the Label Control behaves slightly differently in response to
those values in respect of screen bitmap fonts anyway. I won't go into the
detail of it here (although I will do so later if you really are interested
and if you ask me to) but it effectively means that the Autosize Label
Control when displaying many italic screen font characters sets its width to
a value greater than the actual advance width (TextWidth) of the first
character in the string, but adds an amount that is actually equal to the
advance width (TextWidth) of all the other characters in the string. The
resultant Width of the Label Control will in such cases thereore NOT be the
same as the value that would be returned by the VB TextWidth function or the
equivalent GDI funstion (GetTextExtentPoint32 or whatever). Therefore (as I
mentioned in my other posts) you really would be better off using the
textWidth function or the equivalent GDI function when you want to know the
actual real advance width (TextWidth) of a character or a string of
characters.

Mike



Thu, 12 Jul 2012 06:42:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Label.Caption too big for Label.Width

2. Discovery re truncated userform labels

3. Label text truncated on exported userforms

4. FYI - Labels truncated when printing

5. Label Caption in VBA

6. Permanently change a text label's caption?

7. How to get label captions from an array...?

8. Auto label/Caption change

9. Changing the caption property on a label at run time

10. Label Caption Change

11. Linebreak in label control caption

12. Changing label captions: how to refresh

 

 
Powered by phpBB® Forum Software