Re: TextWidth() Alternative?
- From: "Mike Williams" <mike@xxxxxxxxxxxxxxxxx>
- Date: Tue, 18 Jul 2006 09:45:21 +0100
"Jim Carlock" <anonymous@localhost> wrote in message
news:%23cD%23o2fqGHA.1796@xxxxxxxxxxxxxxxxxxxxxxx
GetTextExtentPoint32 Me.hdc, s1, Len(s1), tsize
What exactly does that return?
It returns the width of the text in device logical units. The width of
course depends on which font is currently selected into the device. If the
device kerns characters then it takes into account any kerning involved. As
far as I know it also takes into account any special inter character spacing
which you may have set using either the SetTexCharacterExtra or the
SetTextJustification methods (although you of course won't usually have
messed about with those things and so you can usually ignore them).
For standard VB Forms and VB Picture Boxes and for the VB Printer Object the
device logical units are device pixels (unless you have deliberately changed
those units with the SetMapMode and associated APIs, which of course will
not usually be the case). By the way, VB Forms and Picture Boxes and the VB
Printer Object always use device pixels as the device logical units,
regardless of your ScaleMode setting. VB does not alter the mapmode and it
instead performs a conversion from your ScaleMode units to device pixels "on
the fly" as it passes your drawing methods to the underlying API routines it
uses.
In the case of the "screen presence" of a RichTextBox I'm not absolutely
sure what mapmode it uses, although I suspect it uses device pixels (screen
pixels) in just the same way as Picture Boxes and Forms do. You can probably
check the mapmode using the GetMapMode API. However, as you obviously
already know, a RichTextBox can display text in all sorts of different fonts
and sizes in the same block of text, and so the font name and font size that
is currently selected into the RichTextBox hDC is not necessarily the same
as the font it is currently showing. Therefore the width of a piece of text
as returned by the GetTextExtentPoint32 API is not necessarily the same as
the size reported for a picture Box or Form, even if the displayed text
happens to be using the same font and size.
If you are displaying just a single font (same name, same size etc) for all
of the text in a RichTextBox then the "screen pixel width" of the text it
displays will be the same as the screen pixel width of the same text in a
Form, even though the value returned by the GetTextExtentPoint32 API on the
RTB may not be the same. So, if you want the RTB to display a single long
line without wrapping it you should read the screen pixel width of the line
of text (using the Form's hDC) and then set the RTB RightMargin property
accordingly. The RTB RightMargin needs to be expressed in twips (not pixels)
so you will need to perform the appropriate conversion. The number of twips
in a screen pixel is system dependent, so you need to get that value in your
code at runtime using the TwipsPerPixelY property of the Screen object
(Screen.TwipsPerPixelY). You'll probably also need to make an adjustment for
the thickness of the RTB borders, and I seem to recall the RTB needing "a
few extra pixels" anyway as well as that so it would be wise to add a few.
Actually I don't ever get too much involved with the RichTextBox control, so
there may be things I have missed here, but I think the above explanation is
something along the right lines.
Mike
.
- Prev by Date: Accessing LDAP through VB
- Next by Date: Re: Dll code
- Previous by thread: Re: TextWidth() Alternative?
- Next by thread: Re: Form not being shown
- Index(es):
Relevant Pages
|