By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,230 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,230 IT Pros & Developers. It's quick & easy.

Calculate Width of text

P: n/a
Sorry about that last one.

Does anyone know how to calculate the width a string of text for given Font
name and size?
I want to buildup a block of text strings to display in a unbound control,
that has limited width but multiple lines. Currently I am using the left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.
May 17 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
wrote:

There is no easy way to do this. The Windows API GetTextExtent and its
cousins will give you some information, but this is not a trivial
route to take.

-Tom.

Sorry about that last one.

Does anyone know how to calculate the width a string of text for given Font
name and size?
I want to buildup a block of text strings to display in a unbound control,
that has limited width but multiple lines. Currently I am using the left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.


May 17 '06 #2

P: n/a
* Tom van Stiphout:
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
wrote:

There is no easy way to do this. The Windows API GetTextExtent and its
cousins will give you some information, but this is not a trivial
route to take.

-Tom.

Sorry about that last one.

Does anyone know how to calculate the width a string of text for given Font
name and size?
I want to buildup a block of text strings to display in a unbound control,
that has limited width but multiple lines. Currently I am using the left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.


I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.
May 17 '06 #3

P: n/a
Try something like this (Access 2003) in a format event of a report

Private Sub GroupHeader0_Format(Cancel As Integer, FormatCount As Integer)

Dim lChangeLength As Long

With Me
.FontBold = True
.FontItalic = True
.FontSize = 12
.FontName = "Times New Roman"
End With

lChangeLength = Me.TextWidth(Me.AssignmentCompany)

'MsgBox lChangeLength
Me!AssignmentProject.LeftMargin = lChangeLength + 100

End Sub
This sets the relevant font for measuring the width of the field and
TextWidth returns the value in the scale set for the report.

The final line actually uses the value to display an offset for another
field so that the two fields appear in different formats at a set distance
apart no matter what the data being displayed is.
--
Slainte

Craig Alexander Morrison
Crawbridge Data (Scotland) Limited
"paii, Ron" <pa**@packairinc.com> wrote in message
news:NJ******************************@athenet.net. ..
Sorry about that last one.

Does anyone know how to calculate the width a string of text for given
Font
name and size?
I want to buildup a block of text strings to display in a unbound control,
that has limited width but multiple lines. Currently I am using the
left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.

May 17 '06 #4

P: n/a
Of course I may have answered a different question. Ooops! (g)

This only applies to reports.

--
Slainte

Craig Alexander Morrison
Crawbridge Data (Scotland) Limited
"Craig Alexander Morrison" <ca*@microsoft.newsgroups.public.com> wrote in
message news:44******@212.67.96.135...
Try something like this (Access 2003) in a format event of a report

Private Sub GroupHeader0_Format(Cancel As Integer, FormatCount As Integer)

Dim lChangeLength As Long

With Me
.FontBold = True
.FontItalic = True
.FontSize = 12
.FontName = "Times New Roman"
End With

lChangeLength = Me.TextWidth(Me.AssignmentCompany)

'MsgBox lChangeLength
Me!AssignmentProject.LeftMargin = lChangeLength + 100

End Sub
This sets the relevant font for measuring the width of the field and
TextWidth returns the value in the scale set for the report.

The final line actually uses the value to display an offset for another
field so that the two fields appear in different formats at a set distance
apart no matter what the data being displayed is.
--
Slainte

Craig Alexander Morrison
Crawbridge Data (Scotland) Limited
"paii, Ron" <pa**@packairinc.com> wrote in message
news:NJ******************************@athenet.net. ..
Sorry about that last one.

Does anyone know how to calculate the width a string of text for given
Font
name and size?
I want to buildup a block of text strings to display in a unbound
control,
that has limited width but multiple lines. Currently I am using the
left$()
function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of
the control. To keep the output nice, I don't want to use a fixed pitch
font.



May 17 '06 #5

P: n/a

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
wrote:

There is no easy way to do this. The Windows API GetTextExtent and its
cousins will give you some information, but this is not a trivial
route to take.

-Tom.

Sorry about that last one.

Does anyone know how to calculate the width a string of text for given Font name and size?
I want to buildup a block of text strings to display in a unbound control, that has limited width but multiple lines. Currently I am using the left$() function to trim the text to a standard number of characters. I want a
function to return the number of characters that will fit in the width of the control. To keep the output nice, I don't want to use a fixed pitch
font.


I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the function
GetLeftQty() to calculate the number of characters that will fit into the
control.
fTextWidth() sometimes underestimates the TWIPS causing my character count
to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) & ", "
& ctlr.Width & ", " & Left$(strTxt, iLen)
End Function
May 17 '06 #6

P: n/a
Are you using English Text?

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
> On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
> wrote:
>
> There is no easy way to do this. The Windows API GetTextExtent and its
> cousins will give you some information, but this is not a trivial
> route to take.
>
> -Tom.
>
>
>> Sorry about that last one.
>>
>> Does anyone know how to calculate the width a string of text for given Font >> name and size?
>> I want to buildup a block of text strings to display in a unbound control, >> that has limited width but multiple lines. Currently I am using the left$() >> function to trim the text to a standard number of characters. I want a
>> function to return the number of characters that will fit in the width of >> the control. To keep the output nice, I don't want to use a fixed
>> pitch
>> font.
>>
>


I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the function
GetLeftQty() to calculate the number of characters that will fit into the
control.
fTextWidth() sometimes underestimates the TWIPS causing my character count
to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) & ", "
& ctlr.Width & ", " & Left$(strTxt, iLen)
End Function

May 17 '06 #7

P: n/a
Yes,

The text is normally a persons name followed by a ":" and then some
information.

ex. I would pass the function text one line at a time adding the result to
the control

..21
Doe, John: 8 Hours Vacation
Walacs, Henry: 8 Hours Vacation
Clar, Sue: 8 Hours Vacation

May come back like this

..21
Doe, John: 8 Hours Vacation
Walacs, Henry: 8 Hours Vacation
Clar, Sue: 8 Hours Vacation

It looks like longer strings return fewer Twips.

"Stephen Lebans" <ForEmailGotoMy.WebSite.-WWWdotlebansdot...@linvalid.com>
wrote in message news:dG*******************@ursa-nb00s0.nbnet.nb.ca...
Are you using English Text?

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
> On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
> wrote:
>
> There is no easy way to do this. The Windows API GetTextExtent and its > cousins will give you some information, but this is not a trivial
> route to take.
>
> -Tom.
>
>
>> Sorry about that last one.
>>
>> Does anyone know how to calculate the width a string of text for given
Font
>> name and size?
>> I want to buildup a block of text strings to display in a unbound

control,
>> that has limited width but multiple lines. Currently I am using the

left$()
>> function to trim the text to a standard number of characters. I want
a >> function to return the number of characters that will fit in the

width of
>> the control. To keep the output nice, I don't want to use a fixed
>> pitch
>> font.
>>
>

I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the function GetLeftQty() to calculate the number of characters that will fit into the control.
fTextWidth() sometimes underestimates the TWIPS causing my character count to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) & ", " & ctlr.Width & ", " & Left$(strTxt, iLen)
End Function


May 17 '06 #8

P: n/a
On further study, fTextWidth() underestimates the TWIPS with my font
settings by 1-2 characters. My subtracting 2 characters just made it look
like an overestimate.

Font Name: MS Sans Serif
Font Size: 8
Font Weight: Normal
Font Italic: No
Font Underline: No
Text Align: General

Width: 1.7854"
Height: 1.1458"

I tried the following change, which gave me the same results as the supplied
code.

lngRet = apiDrawText(hDC, sText, -1, sRect, DT_CALCRECT Or DT_BOTTOM Or
DT_LEFT Or DT_EXTERNALLEADING Or DT_NOCLIP Or DT_SINGLELINE)

"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com>
wrote:

There is no easy way to do this. The Windows API GetTextExtent and its
cousins will give you some information, but this is not a trivial
route to take.

-Tom.
> Sorry about that last one.
>
> Does anyone know how to calculate the width a string of text for given
Font
> name and size?
> I want to buildup a block of text strings to display in a unbound control,> that has limited width but multiple lines. Currently I am using the left$()> function to trim the text to a standard number of characters. I want
a> function to return the number of characters that will fit in the width
of> the control. To keep the output nice, I don't want to use a fixed
pitch> font.
>


I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the function
GetLeftQty() to calculate the number of characters that will fit into the
control.
fTextWidth() sometimes underestimates the TWIPS causing my character count
to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) & ",

" & ctlr.Width & ", " & Left$(strTxt, iLen)
End Function

May 18 '06 #9

P: n/a
Well I found a solution for my application. Increase the TWIPSERINCH
constant from

Private Const TWIPSPERINCH = 1440

To

Private Const TWIPSPERINCH = 1600

This gave me much finer control than reducing the character count in
GetLeftQty().
I would like to thank Stephen Lebans for very useful function.

"paii, Ron" <pa**@packairinc.com> wrote in message
news:er******************************@athenet.net. ..
On further study, fTextWidth() underestimates the TWIPS with my font
settings by 1-2 characters. My subtracting 2 characters just made it look
like an overestimate.

Font Name: MS Sans Serif
Font Size: 8
Font Weight: Normal
Font Italic: No
Font Underline: No
Text Align: General

Width: 1.7854"
Height: 1.1458"

I tried the following change, which gave me the same results as the supplied code.

lngRet = apiDrawText(hDC, sText, -1, sRect, DT_CALCRECT Or DT_BOTTOM Or
DT_LEFT Or DT_EXTERNALLEADING Or DT_NOCLIP Or DT_SINGLELINE)

"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..

"Randy Harris" <pl****@send.no.spam> wrote in message
news:lg*******************@newssvr11.news.prodigy. com...
* Tom van Stiphout:
> On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com> > wrote:
>
> There is no easy way to do this. The Windows API GetTextExtent and its > cousins will give you some information, but this is not a trivial
> route to take.
>
> -Tom.
>
>
>> Sorry about that last one.
>>
>> Does anyone know how to calculate the width a string of text for given
Font
>> name and size?
>> I want to buildup a block of text strings to display in a unbound control,
>> that has limited width but multiple lines. Currently I am using the

left$()
>> function to trim the text to a standard number of characters. I
want a >> function to return the number of characters that will fit in the width
of
>> the control. To keep the output nice, I don't want to use a fixed
pitch >> font.
>>
>

I didn't have the time to go check it out, but I think Stephen Lebans
has a solution for this.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


I tried Stephen Lebans TextHeightWidthVer45.mdb database with the

function GetLeftQty() to calculate the number of characters that will fit into the control.
fTextWidth() sometimes underestimates the TWIPS causing my character count to be high. and other times overestimates the TWIPS causing my character
count to be low.

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
' Function returns the number of character of strTxt that will fit in 1
line of control ctlr

Dim iLen As Integer ' Text length
Dim lngTextWidth As Long
Dim ctlW As Long

ctlW = ctlr.Width
iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

Do While iLen > 1
lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))

' Does text fit? If yes then exit
If lngTextWidth < ctlW Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
If iLen > 2 Then
iLen = iLen - 2
End If
GetLeftQty = iLen
'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) &

", "
& ctlr.Width & ", " & Left$(strTxt, iLen)
End Function


May 18 '06 #10

P: n/a
The solution is not to change an intrinsic constant. You are now tied to the
current resolution setting of your graphics card.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"paii, Ron" <pa**@packairinc.com> wrote in message
news:Ga******************************@athenet.net. ..
Well I found a solution for my application. Increase the TWIPSERINCH
constant from

Private Const TWIPSPERINCH = 1440

To

Private Const TWIPSPERINCH = 1600

This gave me much finer control than reducing the character count in
GetLeftQty().
I would like to thank Stephen Lebans for very useful function.

"paii, Ron" <pa**@packairinc.com> wrote in message
news:er******************************@athenet.net. ..
On further study, fTextWidth() underestimates the TWIPS with my font
settings by 1-2 characters. My subtracting 2 characters just made it look
like an overestimate.

Font Name: MS Sans Serif
Font Size: 8
Font Weight: Normal
Font Italic: No
Font Underline: No
Text Align: General

Width: 1.7854"
Height: 1.1458"

I tried the following change, which gave me the same results as the

supplied
code.

lngRet = apiDrawText(hDC, sText, -1, sRect, DT_CALCRECT Or DT_BOTTOM Or
DT_LEFT Or DT_EXTERNALLEADING Or DT_NOCLIP Or DT_SINGLELINE)

"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..
>
> "Randy Harris" <pl****@send.no.spam> wrote in message
> news:lg*******************@newssvr11.news.prodigy. com...
> > * Tom van Stiphout:
> > > On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron" <pa**@packairinc.com> > > > wrote:
> > >
> > > There is no easy way to do this. The Windows API GetTextExtent and its > > > cousins will give you some information, but this is not a trivial
> > > route to take.
> > >
> > > -Tom.
> > >
> > >
> > >> Sorry about that last one.
> > >>
> > >> Does anyone know how to calculate the width a string of text for

given
> Font
> > >> name and size?
> > >> I want to buildup a block of text strings to display in a unbound
> control,
> > >> that has limited width but multiple lines. Currently I am using
> > >> the
> left$()
> > >> function to trim the text to a standard number of characters. I want
a
> > >> function to return the number of characters that will fit in the

width
> of
> > >> the control. To keep the output nice, I don't want to use a fixed

pitch
> > >> font.
> > >>
> > >
> >
> > I didn't have the time to go check it out, but I think Stephen Lebans
> > has a solution for this.
> >
> > --
> > Randy Harris
> > tech at promail dot com
> > I'm pretty sure I know everything that I can remember.
>
> I tried Stephen Lebans TextHeightWidthVer45.mdb database with the

function > GetLeftQty() to calculate the number of characters that will fit into the > control.
> fTextWidth() sometimes underestimates the TWIPS causing my character count > to be high. and other times overestimates the TWIPS causing my
> character
> count to be low.
>
> Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
> Integer) As Integer
> ' Function returns the number of character of strTxt that will fit in
> 1
> line of control ctlr
>
> Dim iLen As Integer ' Text length
> Dim lngTextWidth As Long
> Dim ctlW As Long
>
> ctlW = ctlr.Width
> iLen = Len(strTxt)
> If lmax > 0 Then
> iLen = MinV(iLen, lmax)
> End If
>
> Do While iLen > 1
> lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))
>
> ' Does text fit? If yes then exit
> If lngTextWidth < ctlW Then Exit Do
>
> ' Decrease text length
> iLen = iLen - 1
> Loop
> If iLen > 2 Then
> iLen = iLen - 2
> End If
> GetLeftQty = iLen
> 'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen)) &

",
"
> & ctlr.Width & ", " & Left$(strTxt, iLen)
> End Function
>
>



May 18 '06 #11

P: n/a
You use WidthTwips = .Right * (TWIPSPERINCH / lngDPI) to calculate width. Is
GetDeviceCaps returning the wrong value? If so, what problems would I have
if lngDPI is adjusted for this calculation?

"Stephen Lebans" <ForEmailGotoMy.WebSite.-WWWdotlebansdot...@linvalid.com>
wrote in message news:Q1*******************@ursa-nb00s0.nbnet.nb.ca...
The solution is not to change an intrinsic constant. You are now tied to the current resolution setting of your graphics card.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"paii, Ron" <pa**@packairinc.com> wrote in message
news:Ga******************************@athenet.net. ..
Well I found a solution for my application. Increase the TWIPSERINCH
constant from

Private Const TWIPSPERINCH = 1440

To

Private Const TWIPSPERINCH = 1600

This gave me much finer control than reducing the character count in
GetLeftQty().
I would like to thank Stephen Lebans for very useful function.

"paii, Ron" <pa**@packairinc.com> wrote in message
news:er******************************@athenet.net. ..
On further study, fTextWidth() underestimates the TWIPS with my font
settings by 1-2 characters. My subtracting 2 characters just made it look like an overestimate.

Font Name: MS Sans Serif
Font Size: 8
Font Weight: Normal
Font Italic: No
Font Underline: No
Text Align: General

Width: 1.7854"
Height: 1.1458"

I tried the following change, which gave me the same results as the

supplied
code.

lngRet = apiDrawText(hDC, sText, -1, sRect, DT_CALCRECT Or DT_BOTTOM Or
DT_LEFT Or DT_EXTERNALLEADING Or DT_NOCLIP Or DT_SINGLELINE)

"paii, Ron" <pa**@packairinc.com> wrote in message
news:va******************************@athenet.net. ..
>
> "Randy Harris" <pl****@send.no.spam> wrote in message
> news:lg*******************@newssvr11.news.prodigy. com...
> > * Tom van Stiphout:
> > > On Wed, 17 May 2006 07:37:52 -0500, "paii, Ron"

<pa**@packairinc.com>
> > > wrote:
> > >
> > > There is no easy way to do this. The Windows API GetTextExtent and
its
> > > cousins will give you some information, but this is not a trivial
> > > route to take.
> > >
> > > -Tom.
> > >
> > >
> > >> Sorry about that last one.
> > >>
> > >> Does anyone know how to calculate the width a string of text for
given
> Font
> > >> name and size?
> > >> I want to buildup a block of text strings to display in a
unbound > control,
> > >> that has limited width but multiple lines. Currently I am using
> > >> the
> left$()
> > >> function to trim the text to a standard number of characters. I

want
a
> > >> function to return the number of characters that will fit in the
width
> of
> > >> the control. To keep the output nice, I don't want to use a fixed pitch
> > >> font.
> > >>
> > >
> >
> > I didn't have the time to go check it out, but I think Stephen Lebans > > has a solution for this.
> >
> > --
> > Randy Harris
> > tech at promail dot com
> > I'm pretty sure I know everything that I can remember.
>
> I tried Stephen Lebans TextHeightWidthVer45.mdb database with the

function
> GetLeftQty() to calculate the number of characters that will fit into

the
> control.
> fTextWidth() sometimes underestimates the TWIPS causing my character

count
> to be high. and other times overestimates the TWIPS causing my
> character
> count to be low.
>
> Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
> Integer) As Integer
> ' Function returns the number of character of strTxt that will fit in > 1
> line of control ctlr
>
> Dim iLen As Integer ' Text length
> Dim lngTextWidth As Long
> Dim ctlW As Long
>
> ctlW = ctlr.Width
> iLen = Len(strTxt)
> If lmax > 0 Then
> iLen = MinV(iLen, lmax)
> End If
>
> Do While iLen > 1
> lngTextWidth = fTextWidth(ctlr, Left$(strTxt, iLen))
>
> ' Does text fit? If yes then exit
> If lngTextWidth < ctlW Then Exit Do
>
> ' Decrease text length
> iLen = iLen - 1
> Loop
> If iLen > 2 Then
> iLen = iLen - 2
> End If
> GetLeftQty = iLen
> 'Debug.Print iLen & ", " & fTextWidth(ctlr, Left$(strTxt, iLen))

& ",
"
> & ctlr.Width & ", " & Left$(strTxt, iLen)
> End Function
>
>



May 18 '06 #12

P: n/a

"Stephen Lebans" <ForEmailGotoMy.WebSite.-WWWdotlebansdot...@linvalid.com>
wrote in message news:Q1*******************@ursa-nb00s0.nbnet.nb.ca...
The solution is not to change an intrinsic constant. You are now tied to the current resolution setting of your graphics card.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


You are right!

I modifyed GetLeftQty() to calculate the number of twips for the character
"X" and used that to reduce the number width of the control in the loop

Public Function GetLeftQty(ctlr As Control, strTxt As String, lmax As
Integer) As Integer
On Error Resume Next
' Function returns the number of character of strTxt that will fit in 1 line
of control ctlr

Dim iLen As Integer ' Text length
Dim lWidth As Long ' Adjusted control width

iLen = Len(strTxt)
If lmax > 0 Then
iLen = MinV(iLen, lmax)
End If

lWidth = ctlr.Width - fTextWidth(ctlr, "X") ' Reduce control width by 1
character.
If lWidth < 1 Then
GetLeftQty = 1 ' Return 1 if adjusted control width is < 1 Twip
Exit Function
End If

Do While iLen > 1

' Does text fit? If yes then exit
If fTextWidth(ctlr, Left$(strTxt, iLen)) < lWidth Then Exit Do

' Decrease text length
iLen = iLen - 1
Loop
GetLeftQty = iLen
End Function
May 18 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.