Re: TextWidth() Alternative?



"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




.



Relevant Pages

  • Re: Advice for OCR solution (or alternative)
    ... My font is not fixed width. ... between each character (sometimes more than one pixels width). ... "Roedy Green" wrote in message ...
    (comp.lang.java.help)
  • Re: Rasterizer differences win98/XP
    ... More precisely, 2*n-1 sampling points, all ... vertically at the pixel's center, horizontally spaced by 1/n pixels, ... This is either a side-effect of your CRT monitor, ... I still suspect that your font was subjected to ...
    (comp.fonts)
  • Re: Speaking of fonts...
    ... understood the situation properly (those who design font rendering ... DPI means how many dots per inch, or pixels in that inch, will be used ... Changing your printer from 24 DPI to 48 DPI, for example, didn't change ...
    (Fedora)
  • Re: What do I do to my web site for MAC users be able to click on
    ... already ran the Design Checker before and fixed everything it brought up. ... You had suggested changing the font but it showed that it was a Web font so ... This is probably why the hyperlinks do not work on an MAC, or in FireFox ... Options and on the General Tab change the Measurement units to pixels ...
    (microsoft.public.publisher.webdesign)
  • Re: Speaking of fonts...
    ... specifying sizes in pixels but is pathetic for sizes in centimeters ... images in inches rather than in pixels. ... so-many pixels, points, or multiples of the normal body text font size ...
    (Fedora)