HOLA!!!
Uh mil disculpas bazooka, entendi mal la consigna y por ahi te maree un poco...
La funcion que te deje yo te va a devolver cuantos pixeles ocupa una cadena de texto en la fuente y tamaño de tu label.
Pero no perdamos los animos digo!!!
Aca codee una funcion complementaria a la que le pones un texto y un label y te devuelve el fontsize maximo que podras ponerle sin perder ninguna letra!
todo el code es el siguiente:
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" _
(ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As SIZE) As Long
Private Type SIZE
cx As Long
cy As Long
End Type
Function GetVisualLength(ByVal Text, LabelX As Label, Fsize As Long, XtrueYfalse As Boolean) As Long
Dim hDC As Long
Dim sz As SIZE
Me.Font = LabelX.Font
Me.FontBold = LabelX.FontBold
Me.FontName = LabelX.FontName
Me.FontSize = Fsize
hDC = GetDC(Me.hWnd)
Call GetTextExtentPoint32(hDC, Text, Len(Text), sz)
If XtrueYfalse Then GetVisualLength = sz.cx Else GetVisualLength = sz.cy
Call ReleaseDC(form_hWnd, hDC)
End Function
Function GetMaxSize(Text As String, LabelX As Label) As Long
Dim x As Long
Dim y As Long
Dim MaxW As Long
Dim MaxH As Long
Me.ScaleMode = 3
MaxW = LabelX.Width
MaxH = LabelX.Height
For x = 2 To 74 Step 2
If MaxH < GetVisualLength(Text, LabelX, x, False) Then Exit For
Next
x = x - 2
For y = 2 To 74 Step 2
If MaxW < GetVisualLength(Text, LabelX, y, True) Then Exit For
Next
y = y - 2
If y > x Then GetMaxSize = x Else GetMaxSize = y
End Function
El uso es muy simple:
Private Sub Form_Load()
' aca consultamos a la funcion getmaxsize cual es el tamaño de fuente indicado
' para que la palabra hola quepa en label1
Label1.FontSize = GetMaxSize("hola", Label1)
End Sub
P.D: Esta hecha para poner en el form, si la queres poner en un modulo asi:
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" _
(ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As SIZE) As Long
Private Type SIZE
cx As Long
cy As Long
End Type
Function GetVisualLength(ByVal Text, LabelX As Label, Fsize As Long, XtrueYfalse As Boolean, FormX As Form) As Long
Dim hDC As Long
Dim sz As SIZE
FormX.Font = LabelX.Font
FormX.FontBold = LabelX.FontBold
FormX.FontName = LabelX.FontName
FormX.FontSize = Fsize
hDC = GetDC(FormX.hWnd)
Call GetTextExtentPoint32(hDC, Text, Len(Text), sz)
If XtrueYfalse Then GetVisualLength = sz.cx Else GetVisualLength = sz.cy
Call ReleaseDC(form_hWnd, hDC)
End Function
Function GetMaxSize(Text As String, LabelX As Label, FormX As Form) As Long
Dim x As Long
Dim y As Long
Dim MaxW As Long
Dim MaxH As Long
FormX.ScaleMode = 3
MaxW = LabelX.Width
MaxH = LabelX.Height
For x = 2 To 74 Step 2
If MaxH < GetVisualLength(Text, LabelX, x, False, FormX) Then Exit For
Next
x = x - 2
For y = 2 To 74 Step 2
If MaxW < GetVisualLength(Text, LabelX, y, True, FormX) Then Exit For
Next
y = y - 2
If y > x Then GetMaxSize = x Else GetMaxSize = y
End Function
y este codigo en el form:
Private Sub Form_Load()
Label1.FontSize = GetMaxSize("hola", Label1, Me)
End Sub
GRACIAS POR LEER!!!