绘画自定义面板控件(.Net Framework 4.0)中的问题. [英] Problem in painting custom panel control (.Net Framework 4.0).

查看:64
本文介绍了绘画自定义面板控件(.Net Framework 4.0)中的问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了标准面板控件,以创建具有自定义边框颜色的面板.问题是,当面板位于窗体上并且在任何其他窗体上移动时,它会在面板的整个背景上绘制与面板自定义边框颜色相同颜色的随意行.我尝试在其绘画事件中刷新或使面板无效,但无济于事.在设计编辑器中进行设计时,也会发生同样的情况.为什么会发生这种情况以及如何消除呢?
我的代码如下:

I have inherited the standard panel control to create a panel having custom border color. The problem is, while the panel is on a form and any other form is moved across it, it paints hapazard lines of the same color as the panel custom border color, all throughout background of the panel. I tried to Refresh or Invalidate the panel in its paint event but of no avail. The same is happening while designing in the design editor too. Why is this happening and how to get rid of it?
My code follows:

Namespace CustomPanelControl
    Public Class CustomPanel
        Inherits Panel

        Public Sub New()
            MyBase.New()
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)

            'Set the backcolor of the panel according to the focus on it.
            If Me.ContainsFocus Then
                Me.BackColor = Color.Yellow
            Else
                Me.BackColor = Color.White
            End If

            'Draw the border of the panel.
            If Me.BorderStyle = BorderStyle.None Then
                Dim borderWidth As Integer = 1
                Dim BorderColor As Color = Color.Blue
                ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, BorderColor, borderWidth, ButtonBorderStyle.Solid, BorderColor, borderWidth, ButtonBorderStyle.Solid, BorderColor, _
                borderWidth, ButtonBorderStyle.Solid, BorderColor, borderWidth, ButtonBorderStyle.Solid)
            End If
        End Sub
    End Class
End Namespace

推荐答案

我会尝试使用System.Windows.Forms.Control.Control.SetStyle添加System.Windows.Forms.ControlStyles AllPaintingInWmPaint | OptimizedDoubleBuffer,请参见:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx [ ^ ].

另外,我将从Control派生您的类,并且不调用基础OnPaint(并自行绘制背景),因为您不需要特定于Panel的任何功能.

但是,我没有发现您的代码有任何根本上的错误;我什至怀疑您的系统有问题.我记得我在Windows 2000上显示过一些Forms工件,但在XP上却没有.因此,我建议您在不同的系统上尝试您的代码,以防万一.

—SA
I would try to add System.Windows.Forms.ControlStyles AllPaintingInWmPaint | OptimizedDoubleBuffer using System.Windows.Forms.Control.Control.SetStyle, see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^].

Also, I would derive your class from Control and did not call base OnPaint (and paint the background yourself), as you don''t need any functionality specific to Panel.

However, I don''t see anything fundamentally wrong in your code; I would even suspect that something is wrong with your system. I remember I had some Forms artifacts showing on Windows 2000 but never on XP. So, I would suggest you try your code on different systems, just in case.

—SA


这篇关于绘画自定义面板控件(.Net Framework 4.0)中的问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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