Main Page | Report Page

 

  .NET DotNet Forum Index » VB.NET Forum (Visual Basic .NET) » get the metrics of text....

Author Message
Mr. X....
Posted: Thu Jul 08, 2010 9:13 am
 
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 Smile
 
Onur Güzel...
Posted: Thu Jul 08, 2010 9:13 am
 
On Jul 8, 6:13 pm, "Mr. X." <nospam.... at (no spam) gmail.com> wrote:
Quote:
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 Smile

You may want to look at MeasureString method:

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurestring.aspx

http://www.thescarms.com/dotnet/MeasureString.aspx

HTH,

Onur Güzel
 
Mr. X....
Posted: Thu Jul 08, 2010 10:20 am
 
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 Smile
 
Armin Zingler...
Posted: Thu Jul 08, 2010 11:28 am
 
Am 08.07.2010 18:20, schrieb Mr. X.:
Quote:
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 Smile

using g = yourcontrol.creategraphics
g.measurestring
end using



--
Armin
 
Armin Zingler...
Posted: Fri Jul 09, 2010 5:59 am
 
Am 09.07.2010 12:54, schrieb Mr. X.:
Quote:
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



--
Armin
 
Armin Zingler...
Posted: Fri Jul 09, 2010 9:58 am
 
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

--
Armin
 
Herfried K. Wagner [MVP]...
Posted: Fri Jul 09, 2010 3:17 pm
 
Am 08.07.2010 17:13, schrieb Mr. X.:
Quote:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Branco Medeiros...
Posted: Mon Jul 12, 2010 6:46 am
 
Herfried Wagner wrote:
<snip>
Quote:
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.
 
Herfried K. Wagner [MVP]...
Posted: Mon Jul 12, 2010 4:24 pm
 
Am 12.07.2010 18:46, schrieb Branco Medeiros:
Quote:
snip
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++

See:

Label.UseCompatibleTextRendering Property (System.Windows.Forms)
<URL:http://msdn.microsoft.com/en-us/library/system.windows.forms.label.usecompatibletextrendering.aspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Thu Aug 21, 2014 9:09 pm