在调整大小时使用图形 DrawString 的问题
Issue Using Graphics DrawString On Resize
目标
我想在用户控件的左侧显示一个垂直文本,让用户知道他们是哪个产品 creating/editting。像这样:
我如何构建它?
这个用户控件由三个控件组成。
- 标签 和文字 "Product Information"。停靠=顶部
- 用户控件,垂直绘制字符串文本 "Product #1"。停靠=左
- Table 布局面板 其中包含 X 数量的用户控件。码头=填充
这是设计视图:
这是我的产品名称用户控件的代码,它绘制了 "Product #1"
Public Class uProductName
Public drawString As String = "Product #1"
Protected Overrides Sub OnPaint(e As PaintEventArgs)
' Call the OnPaint method of the base class.
MyBase.OnPaint(e)
' Call methods of the System.Drawing.Graphics object.
DrawVerticalString(e)
End Sub
Public Sub DrawVerticalString(ByVal e As PaintEventArgs)
Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
Dim drawFont As New System.Drawing.Font("Arial", 20)
Dim drawBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Black)
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(drawString, drawFont)
Dim x As Single = (Me.Width / 2) - (stringSize.Height / 2)
Dim y As Single = (Me.Height / 2) - (stringSize.Width / 2)
Dim drawFormat As New System.Drawing.StringFormat
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat)
drawFormat.Dispose()
drawFont.Dispose()
drawBrush.Dispose()
formGraphics.Dispose()
End Sub
End Class
当前问题
当我开始选择按钮时,table 布局面板会扩展以显示更多选择,并且 "Product #1" 文本开始出现故障。见下文:
我尝试将 "Double Buffer" 属性 设置为 true 但没有结果。有什么建议吗?
您需要为控件设置 ControlStyles.ResizeRedraw
样式,以指示控件是否在调整大小时自行重绘。
也不要使用CreateGraphics()
,而是使用OnPaint
方法的图形对象,永远不要释放它,因为它不属于你。
Public Sub New()
' If the base class is Control, comment the next line
InitializeComponent()
Me.SetStyle(ControlStyles.ResizeRedraw, True)
End Sub
Public Sub DrawVerticalString(ByVal e As PaintEventArgs)
Dim formGraphics As System.Drawing.Graphics = e.Graphics
'...
End Sub
目标
我想在用户控件的左侧显示一个垂直文本,让用户知道他们是哪个产品 creating/editting。像这样:
我如何构建它?
这个用户控件由三个控件组成。
- 标签 和文字 "Product Information"。停靠=顶部
- 用户控件,垂直绘制字符串文本 "Product #1"。停靠=左
- Table 布局面板 其中包含 X 数量的用户控件。码头=填充
这是设计视图:
这是我的产品名称用户控件的代码,它绘制了 "Product #1"
Public Class uProductName
Public drawString As String = "Product #1"
Protected Overrides Sub OnPaint(e As PaintEventArgs)
' Call the OnPaint method of the base class.
MyBase.OnPaint(e)
' Call methods of the System.Drawing.Graphics object.
DrawVerticalString(e)
End Sub
Public Sub DrawVerticalString(ByVal e As PaintEventArgs)
Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
Dim drawFont As New System.Drawing.Font("Arial", 20)
Dim drawBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Black)
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(drawString, drawFont)
Dim x As Single = (Me.Width / 2) - (stringSize.Height / 2)
Dim y As Single = (Me.Height / 2) - (stringSize.Width / 2)
Dim drawFormat As New System.Drawing.StringFormat
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat)
drawFormat.Dispose()
drawFont.Dispose()
drawBrush.Dispose()
formGraphics.Dispose()
End Sub
End Class
当前问题
当我开始选择按钮时,table 布局面板会扩展以显示更多选择,并且 "Product #1" 文本开始出现故障。见下文:
我尝试将 "Double Buffer" 属性 设置为 true 但没有结果。有什么建议吗?
您需要为控件设置 ControlStyles.ResizeRedraw
样式,以指示控件是否在调整大小时自行重绘。
也不要使用CreateGraphics()
,而是使用OnPaint
方法的图形对象,永远不要释放它,因为它不属于你。
Public Sub New()
' If the base class is Control, comment the next line
InitializeComponent()
Me.SetStyle(ControlStyles.ResizeRedraw, True)
End Sub
Public Sub DrawVerticalString(ByVal e As PaintEventArgs)
Dim formGraphics As System.Drawing.Graphics = e.Graphics
'...
End Sub