1
我正在創建用戶控件併爲其添加文本大小屬性。在繪製控件之前,我需要知道文本大小有多大,以便我可以將它與控件的高度和寬度相關聯。VB.NET:在繪製到控件之前查找要繪製的文本的大小
我假設這是一個Windows API命令。但是我一直無法找到它的任何信息。
Public Sub DrawImage(ByVal gr As Graphics)
If Me.Image Is Nothing Then Return
Dim r As Rectangle = New Rectangle(8, 8, Me.ImageSize.Width, Me.ImageSize.Height)
Select Case Me.ImageAlign
Case ContentAlignment.TopLeft
r = New Rectangle(4, 4, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.TopCenter
r = New Rectangle((Me.Width/2 - Me.ImageSize.Width/2)/2, 4, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.TopRight
r = New Rectangle(Me.Width - 4 - Me.ImageSize.Width, 4, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.MiddleLeft
r = New Rectangle(8, Me.Height/2 - Me.ImageSize.Height/2, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.MiddleCenter
r = New Rectangle(Me.Width/2 - Me.ImageSize.Width/2, Me.Height/2 - Me.ImageSize.Height/2, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.MiddleRight
r = New Rectangle(Me.Width - 8 - Me.ImageSize.Width, Me.Height/2 - Me.ImageSize.Height/2, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.BottomLeft
r = New Rectangle(8, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.BottomCenter
r = New Rectangle(Me.Width/2 - Me.ImageSize.Width/2, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.BottomRight
r = New Rectangle(Me.Width - 8 - Me.ImageSize.Width, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height)
End Select
gr.DrawString("Header", New Font("MS SANS SERIF", 8.25, FontStyle.Regular, GraphicsUnit.Point, 1, True), Brushes.Black, 0, 0)
'gr.DrawImage(Me.Image, r)
End Sub
正如你可以看到@ gr.DrawString,我需要知道文字有多大預測是讓我能規模的控制和中心控制中的文本。