
Duda con TextRenderer.MeasureText
Publicado por Leonardo (5 intervenciones) el 08/09/2016 19:43:50
Hola, estoy recién con VB.Net y no me resulta algo, tengo que obtener el ancho de un texto en pixeles pero no me resulta:
El código original lo saque del siguiente ejemplo:
Yo lo adapte al siguiente código:
No me resulta por que no se donde o como sacar el valor de e ("ByVal e As PaintEventArgs"),
Se agradece cualquier ayuda
Saludos
El código original lo saque del siguiente ejemplo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
' Declare strings to render on the form.
Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}
' Declare fonts for rendering the strings.
Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
New Font("Arial", 14, FontStyle.Italic), _
New Font("Arial", 14, FontStyle.Regular)}
Dim startPoint As New Point(10, 10)
' Set TextFormatFlags to no padding so strings are drawn together.
Dim flags As TextFormatFlags = TextFormatFlags.NoPadding
' Declare a proposed size with dimensions set to the maximum integer value.
Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)
' Measure each string with its font and NoPadding value and draw it to the form.
For i As Integer = 0 To stringsToPaint.Length - 1
Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
stringsToPaint(i), fonts(i), proposedSize, flags)
Dim rect As Rectangle = New Rectangle(startPoint, size)
TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
startPoint, Color.Black, flags)
startPoint.X += size.Width
Next
End Sub
Yo lo adapte al siguiente código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
For Columna = 0 To FlexGrid.Cols - 1
FlexGrid.Col = Columna
Dim stringsToPaint As String = FlexGrid.Text
Dim fonts As Font = New Font(FlexGrid.FontName, FlexGrid.FontSize, FontStyle.Bold)
' Set TextFormatFlags to no padding so strings are drawn together.
Dim flags As TextFormatFlags = TextFormatFlags.NoPadding
' Declare a proposed size with dimensions set to the maximum integer value.
Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)
Dim size As Size = TextRenderer.MeasureText(????, stringsToPaint, fonts, proposedSize, flags)
Col_Width = size.Width
'Comparar cual es mayor
If Ancho_Columna(Columna) < Col_Width Then
Ancho_Columna(Columna) = Col_Width
End If
Next
No me resulta por que no se donde o como sacar el valor de e ("ByVal e As PaintEventArgs"),
Se agradece cualquier ayuda
Saludos
Valora esta pregunta


0