get the metrics of text.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

Mr. X.

Hello.
I have a label component on my code.
I need to calculate the exact size of pixels before putting a text into it.
(calculating the size in pixel, by having the font and the text value).

How can I do that ?

Thanks :)
 
Thanks.
Is there a common letter to be measured?
(suppose I have 5 letters : is it XXXXX, WWWWW, PPPPP).

Thanks :)
 
That's the function I need.
Another thing.
I don't have a graphics object.
How can I resolve it from the object : (should I ?)
(I.e DataGridView.Columns(0) ) ?

Thanks :)
 
Am 08.07.2010 18:20, schrieb Mr. X.:
That's the function I need.
Another thing.
I don't have a graphics object.
How can I resolve it from the object : (should I ?)
(I.e DataGridView.Columns(0) ) ?

Thanks :)

using g = yourcontrol.creategraphics
g.measurestring
end using
 
Then you create a string and measure that given by the method by Onur

What is your problem, seems so obvious to me?
 
It's a datagrid, which represents rows from database.
and it is dynamic grid that can show not a specific table, but the one I
request.

Because I cannot figure out what is the exact size in pixels of each row,
I need to calculate it approximately for a specific letter multiply by the
maximum size * 80%.
(I have read this long time ago).

Thanks :)
 
Am 09.07.2010 12:54, schrieb Mr. X.:
It's a datagrid, which represents rows from database.
and it is dynamic grid that can show not a specific table, but the one I
request.

Because I cannot figure out what is the exact size in pixels of each row,
I need to calculate it approximately for a specific letter multiply by the
maximum size * 80%.
(I have read this long time ago).

Would the DataGridView's AutoResizeColumns method save you some work?
Or
AutoResizeColumn(ByVal columnIndex As Integer, ByVal autoSizeColumnMode As System.Windows.Forms.DataGridViewAutoSizeColumnMode)


Besides that...:
A while ago, I wrote a FontViewer. It also displayed the max and avg
width of a char. Maybe there's an implementation in the Framework meanwhile,
but I've used an API call for this. Here's an excerpt of the original
code:


Imports System.Runtime.InteropServices

'...

'in the class:

Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal objectHandle As IntPtr) As Boolean
Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr
Private Declare Auto Function GetTextMetrics Lib "gdi32" (ByVal hdc As IntPtr, ByRef lpMetrics As TEXTMETRIC) As Boolean

'...

'in a method:

Dim TM As TEXTMETRIC
Dim TMSuccess As Boolean


Using g = pic.CreateGraphics '***** use your datagridview here instead of pic *****
Dim hdc = g.GetHdc

Try
Dim hFont = f.ToHfont

Try
Dim oldobject = SelectObject(hdc, hFont)
Try
TM = Nothing 'Dummy-Zuweisung
TMSuccess = GetTextMetrics(hdc, TM)
Finally
SelectObject(hdc, oldobject)
End Try
Finally
If Not DeleteObject(hFont) Then
Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error, "DeleteObject failed")
End If
End Try
Finally
g.ReleaseHdc(hdc)
End Try
End Using

If TMSuccess Then
'***** TM.tmMaxCharWidth may be what you're looking for *****
txtFontInfo.Text = TM.tmMaxCharWidth & "/" & TM.tmAveCharWidth & "/" & TM.tmHeight
End If

End Using
 
Forgotten:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure TEXTMETRIC
Dim tmHeight As Integer
Dim tmAscent As Integer
Dim tmDescent As Integer
Dim tmInternalLeading As Integer
Dim tmExternalLeading As Integer
Dim tmAveCharWidth As Integer
Dim tmMaxCharWidth As Integer
Dim tmWeight As Integer
Dim tmOverhang As Integer
Dim tmDigitizedAspectX As Integer
Dim tmDigitizedAspectY As Integer

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public tmFirstChar As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public tmLastChar As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public tmDefaultChar As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public tmBreakChar As String

Dim tmItalic As Byte
Dim tmUnderlined As Byte
Dim tmStruckOut As Byte
Dim tmPitchAndFamily As Byte
Dim tmCharSet As Byte
End Structure
 
Am 08.07.2010 17:13, schrieb Mr. X.:
I have a label component on my code.
I need to calculate the exact size of pixels before putting a text into it.
(calculating the size in pixel, by having the font and the text value).

Take a look at 'TextRenderer.MeasureText'. Note that since .NET 2.0
Windows Forms do not use GDI+ ('System.Drawing.Graphics.DrawString') to
draw text any more.
 
Herfried Wagner wrote:
Note that since .NET 2.0
Windows Forms do not use GDI+ ('System.Drawing.Graphics.DrawString') to
draw text any more.
<snip>

could you provide a reference? I thought Windows Forms were all about
GDI++

regards,

Branco.
 
Back
Top