VB.NET自定义控件(自​​定义图形)刷新问题 [英] VB.NET Custom Control (custom drawing) Refresh issue

查看:129
本文介绍了VB.NET自定义控件(自​​定义图形)刷新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含2个项目的简单解决方案。第一个项目(类库)包含一个名为Container的自定义控件,该控件将自己绘制为圆角。第二个项目(Windows窗体)是一个测试应用程序。

I've created a simple solution with 2 projects. The 1st project (class library) contains a custom control called Container which draws itself with rounded corners. The 2nd project (windows forms) is a test application.

如果我在第二个项目的主Form中添加一个Container实例,则会很好地显示圆角。同样,当我运行第二个项目时,我可以看到容器。

If I add a Container instance to main Form in the 2nd project it shows the rounded corners nicely. Also when I run the 2nd project I can see the Container.

但是,当我开始移动表单时(单击并按住标题栏),尤其是当我非常移动它时很快,所有图形都被弄乱了,一遍又一遍地绘制,但没有先清除它的表面...

However when I start moving the form (click and hold the title bar), especially when I move it very fast, all the drawing is messed up, drawn over and over again but not clearing it's surface first...

我可以在Form1中调用Container1.Refresh()。 Move事件,但是我不想每次都设置此事件,因为这也意味着我必须在 Form1.Resize 事件中调用Container1.Refresh(),谁知道这其他事件...

I can call Container1.Refresh() in the Form1.Move event, but I don't want to set this every time because this also means I have to call Container1.Refresh() in the Form1.Resize event and who knows which other event...

在Container(控件)类本身中是否有一个事件,我应该调用Me.Refresh()或Me.Update()或Me。 Invalidate()?

Is there an event in the Container (control) class itself where I should call Me.Refresh() or Me.Update() or Me.Invalidate() ?

供参考(Form1.vb)

For reference (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):

for reference (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


推荐答案

这是您的问题:

Dim r As Rectangle = e.ClipRectangle

将其更改为:

Dim r As Rectangle = Me.ClientRectangle

这篇关于VB.NET自定义控件(自​​定义图形)刷新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆