Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Windows tile will adjust the font size or text by accepting tile size. also adjust the font size By accepting a rectangular region. the main intention of mine is that ,not to get truncate font @ windows tile when the font size got longer. instead what I want is to auto minimize or make the tile shape to rectangle automaticaly inorder to make the font size to fit with tile size. i have been searching the solution but i cant make it.

please help me guy's!!!

with a regards,
Posted

1 solution

This could be done very easy with the System.Windows.Forms.TextRenderer.MeassureString-method.
You give it your text and your font and it gives you a size.
Now you have to build a method, which compares (by trying) which maximum fontsize would match.
You have to increase or decrease the try-fontsize until the TextRenderer-Size matches best to your Control-Size.

VB
Function GetFontSizeMatch(ByVal myText As String, ByVal myFont As Font, ByVal mySize As Size) As Single

    If Trim(myText).Length <= 0 Then myText = "X"

    Dim currentSize As Single = CSng(myFont.Size)
    Dim workFont As Font = New Font(myFont.Name, currentSize, myFont.Style, myFont.Unit)

    Dim Nutzbreite As Single = CSng(mySize.Width)
    Dim Nutzhöhe As Single = CSng(mySize.Height)
    Dim myTextSize As SizeF

    If (Nutzbreite >= 1) AndAlso (Nutzhöhe >= 1) Then
        Do
            currentSize += 4.0 : If currentSize > 50 Then Exit Do
            workFont = New Font(workFont.Name, currentSize, workFont.Style, workFont.Unit)
            myTextSize = TextRenderer.MeasureText(myText, workFont)
        Loop Until (myTextSize.Width > Nutzbreite) Or (myTextSize.Height > Nutzhöhe)

        Do
            currentSize -= 0.5 : If currentSize < 5 Then Exit Do
            workFont = New Font(workFont.Name, currentSize, workFont.Style, workFont.Unit)
            myTextSize = TextRenderer.MeasureText(myText, workFont)
        Loop Until (myTextSize.Width <= Nutzbreite) AndAlso (myTextSize.Height <= Nutzhöhe)

    End If

    Return currentSize

End Function
 
Share this answer
 
v2
Comments
Member 11582405 4-Jul-15 2:23am    
hope this could work thanks! but can u show me the overall building of those methodes? or can you get me sample codes on it?
i have been working on how to come up with System.Windows.Forms.TextRenderer.MeassureString method and all you mentioned. but that takes me a lot to do.
Ralf Meier 6-Jul-15 1:06am    
I have improved my solution - but I am a VB-Developer. So I hope, that my VB-Code is useful too for you ... (if Yes please accept my Answer and rate it).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900