开发者

User control drawing ok on one machine, but not another. Drawing behind graphics?

开发者 https://www.devze.com 2023-03-26 21:44 出处:网络
I have a custom control class that I just made. On my dev machine at work (virtualbox guest Win7 x64 running VS2010) it draws perfectly. On re-size, and everything.

I have a custom control class that I just made. On my dev machine at work (virtualbox guest Win7 x64 running VS2010) it draws perfectly. On re-size, and everything.

On my home machine, It draws the first time, then if I resize it I don't see anything change except for the edges. Paying closer attention to the artifacts seems to point at my control drawing underneath what it just drew instead of on top. I have no idea what could be different.

Imports System.Drawing

Public Class NetworkDiagram
    Inherits Windows.Forms.UserControl

    Sub New()
    End Sub

    Sub DrawNetwork(e As System.Windows.Forms.PaintEventArgs)

        Dim pen1 As New Pen(Color.Blue, 1.0F)

        ' Draw a basic smiley face
        e.Graphics.FillEllipse(Brushes.Yellow, 0, 0, Me.Width - 1, Me.Height - 1) ' Face
        e.Graphics.FillEllipse(Brushes.Black, Me.开发者_JS百科Width / 4.0F, Me.Height / 4.0F, Me.Width / 6.0F, Me.Height / 6.0F) ' Left Eye
        e.Graphics.FillEllipse(Brushes.Black, Me.Width - (Me.Width / 3.0F) - 10, (Me.Height / 4.0F), (Me.Width / 6.0F), (Me.Height / 6.0F)) ' Right Eye
        e.Graphics.DrawArc(Pens.Black, (Me.Width / 4.0F), (Me.Height / 2.0F), (Me.Width / 2.0F), (Me.Height / 4.0F), 0, 180) ' Mouth

        pen1.Dispose()
    End Sub

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        DrawNetwork(e)
        MyBase.OnPaint(e)
    End Sub
End Class


UserControl is a container control, like Panel. They don't normally have their own content but just act as containers for other controls. Their behavior was optimized for that, avoiding excessive painting as that can cause a lot of flicker during resizing operations. But in this case you really do care about the painting. Which is fine, you just have to undo the optimization. Make your constructor look like this:

Sub New()
    Me.InitializeComponent()
    Me.SetStyle(ControlStyles.ResizeRedraw, True)
End Sub

I left the InitializeComponent() call in since it is normally there. The SetStyle() call is the important one.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号