Hey, I actually found the VB code that I used to compute a small enough
font to fit a string into given space.  Might give you a start. 

 

oFld is the actual customer name field from the Crystal report
definition.

 

-- Jeremy

 

    Private Function getFontThatFits(ByVal oFld As FieldObject, ByVal
str As String, _

                                     ByVal e As
System.Windows.Forms.PaintEventArgs) As System.Drawing.Font

        'jhg 07/14/2004

        Dim f As System.Drawing.Font

        Dim s As Single

        Dim sf As SizeF

        Dim fldWidth As Int32

 

        Dim gr As Graphics = Me.CreateGraphics 'jhg 04/26/2005

        Dim twipsToPixelsLocal As Decimal = gr.DpiX / 1440 '182 / 2552
'jhg 04/25/2005

        gr.Dispose()

        fldWidth = oFld.Width * twipsToPixelsLocal     'jhg 04/25/2005

 

        'fldWidth is the approx width of customer name field in
packLabel.rpt, expressed in pixels.

        '182 is the empirically derived "width" of the crystal
customername field

        'in pixels on a screen set to 1024 pixels total width.

 

        s = oFld.Font.Size

        's = shipCustFontSize

 

        f = New System.Drawing.Font(oFld.Font.Name, s, oFld.Font.Style)

        sf = e.Graphics.MeasureString(str, f)

 

        While (sf.Width > fldWidth)

            s = s - 0.5

            f = New System.Drawing.Font(oFld.Font.Name, s,
oFld.Font.Style, oFld.Font.Unit)

            sf = e.Graphics.MeasureString(str, f)

        End While

 

        getFontThatFits = f

 

        'lblMsg.Text = "str width: " + sf.Width.ToString + "; fontsize:
" + s.ToString

 

    End Function

 

 

 

From: advanced_delphi@yahoogroups.com
[mailto:advanced_del...@yahoogroups.com] On Behalf Of dosiemoe
Sent: Wednesday, April 15, 2009 5:25 PM
To: advanced_delphi@yahoogroups.com
Subject: [advanced_delphi] Calculate string length in millimeters based
on other HDC

 






Is it that impossible to calcutate how wide a given
string will print, in millimeters, if I don't have
the printer's canvas before-hand?

So a font is actually a logical font, it will be a
whole different things once we use SelectObject from
one canvas to another.

MSDN said there will be round off error when using
logical unit other than MM_TEXT, but I found that it
is ridiculously huge.
At first I thought it is going to be at most one char
wide.

I'm going to find out more about Portable TrueType
Metric, and Design Units.
------------
FIreHAzaR:-D
West Java, Indonesia



<<image001.jpg>>

<<image002.jpg>>

Reply via email to