我用 2 个项目创建了一个简单的解决方案。第一个项目(类库)包含一个名为 Container 的自定义控件,它使用圆角绘制自身。第二个项目(Windows 窗体)是一个测试应用程序。
如果我在第二个项目的主窗体中添加一个容器实例,它会很好地显示圆角。此外,当我运行第二个项目时,我可以看到容器。
但是,当我开始移动表单(单击并按住标题栏)时,尤其是当我快速移动它时,所有的绘图都搞砸了,一遍又一遍地绘制,但没有先清除它的表面......
我可以在 Form1.Move 事件中调用 Container1.Refresh(),但我不想每次都设置它,因为这也意味着我必须在Form1.Resize
事件中调用 Container1.Refresh() 并且谁知道其他事件.. .
Container(控件)类本身是否存在我应该调用 Me.Refresh() 或 Me.Update() 或 Me.Invalidate() 的事件?
供参考(Form1.vb)
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
Me.Container1.Refresh()
End Sub
End Class
供参考(Container.vb):
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Container : Inherits Control
Private _Gp As GraphicsPath
Private Sub Container_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim r As Rectangle = e.ClipRectangle
Dim gp As New GraphicsPath
Dim cs As Integer = 25 'CornerSize'
r.Inflate(-5, -5)
gp.AddArc(r.X, r.Y, cs, cs, 180, 90)
gp.AddArc(r.X + r.Width - cs, r.Y, cs, cs, 270, 90)
gp.AddArc(r.X + r.Width - cs, r.Y + r.Height - cs, cs, cs, 0, 90)
gp.AddArc(r.X, r.Y + r.Height - cs, cs, cs, 90, 90)
Dim t As Single = cs / 2 + r.Y
gp.AddLine(r.X, r.Y + r.Height - cs, r.X, t)
e.Graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
e.Graphics.DrawPath(Pens.Black, gp)
End Sub
End Class