【发布时间】:2018-10-10 19:09:37
【问题描述】:
如何设置listview对象标题列的垂直对齐方式以适应单元格边框内的文本(不能自动设置标题表单的高度)
ListView1.Columns.Add(items.InnerText, 90, HorizontalAlignment.Center)
将“strformat”添加到drawcolumnheader 可用于水平对齐,但不适用于垂直
Private Sub ListView3_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView3.DrawColumnHeader
Dim strFormat As New StringFormat()
If e.Header.TextAlign = HorizontalAlignment.Center Then
strFormat.LineAlignment = StringAlignment.Center
strFormat.Alignment = StringAlignment.Center
ElseIf e.Header.TextAlign = HorizontalAlignment.Right Then
strFormat.LineAlignment = StringAlignment.Far
End If
e.DrawBackground()
e.Graphics.FillRectangle(Brushes.SteelBlue, e.Bounds)
Dim headerFont As New Font("Arial", 8, FontStyle.Bold)
e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.White, e.Bounds, strFormat)
End Sub
【问题讨论】:
-
我认为您可以使用 Graphics.MeasureString 来计算加法时的正确宽度?
-
@mino 它不是关于宽度,问题是文本不适合行的高度
-
您的偏好设置为: - 调整字体大小 - PInvoking 以修改标题高度 - 使用 DataGridView。
-
使用“strFormat.LineAlignment = StringAlignment.Center”或“strFormat.Alignment = StringAlignment.Center”添加drawcoloumn标头事件效果不佳,是否有类似的想法适用于水平对齐?跨度>
-
StringFormat正在按预期工作。您的 问题 是没有足够的垂直空间来包含您的字符串。我在之前的评论中列出了 3 种可能的解决方案。