渐变绘制控件 - FlowLayoutPanel滚动闪烁

时间:2014-01-30 09:20:18

标签: vb.net winforms paint gradient flowlayoutpanel

所以我有一个FlowLayoutPanel,它被多个用户控件填充。这些用户控件具有渐变绘制的背景颜色。在MouseEnter上,渐变的颜色会发生变化(如鼠标悬停效果)并在MouseLeave上恢复正常。这很好用,但是当滚动时,FlowLayoutPanel会严重闪烁(即使它是双缓冲的)。如果我将以下代码添加到我的表单中,滚动闪烁完全消失,我的用户控件的MouseEnter / MouseLeave事件仍然会触发,但新的背景渐变不会被绘制为类似于鼠标效果

Protected Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim cp As CreateParams = MyBase.CreateParams
        cp.ExStyle = cp.ExStyle Or &H2000000 ' WS_CLIPCHILDREN

        Return cp
    End Get
End Property

执行绘画的代码在公共子例程中,并且被称为

Call PaintMe(Me, ColorTranslator.FromOle(RGB(18, 18, 18)), _
                    ColorTranslator.FromOle(RGB(29, 29, 29)), _
                    ColorTranslator.FromOle(RGB(18, 18, 18)), _
                    Color.Black, True, True, 2)

Public Sub PaintMe(ByVal ctrl As Control, ByVal color1 As Color, color2 As Color, color3 As Color, ByVal border_color As Color, Optional ByVal correct_gama As Boolean = False, Optional ByVal has_border As Boolean = False, Optional ByVal border_width As Single = 0)
    ' Make the basic brush.
    Dim gBrush As LinearGradientBrush
    gBrush = New LinearGradientBrush(New Point(0, 0), New Point(0, ctrl.ClientSize.Height), _
                                     ColorTranslator.FromOle(RGB(color1.R, color1.G, color1.B)), _
                                     ColorTranslator.FromOle(RGB(color3.R, color3.G, color3.B)))

    gBrush.WrapMode = WrapMode.TileFlipX

    ' Define the colors.
    Dim color_blend As New ColorBlend
    color_blend.Colors = New Color() {ColorTranslator.FromOle(RGB(color1.R, color1.G, color1.B)), _
                                      ColorTranslator.FromOle(RGB(color2.R, color2.G, color2.B)), _
                                      ColorTranslator.FromOle(RGB(color3.R, color3.G, color3.B))}

    color_blend.Positions = New Single() {0.0, 0.5, 1.0}
    gBrush.InterpolationColors = color_blend
    gBrush.GammaCorrection = correct_gama

    ctrl.CreateGraphics().SmoothingMode = SmoothingMode.AntiAlias
    ctrl.CreateGraphics().FillRectangle(gBrush, ctrl.ClientRectangle)

    If has_border = True Then
        ' Create pen. 
        Dim myPen As New Pen(border_color, border_width)
        ' Create rectangle. 
        Dim rect As New Rectangle(0, 0, ctrl.ClientSize.Width, ctrl.ClientSize.Height)
        ctrl.CreateGraphics().DrawRectangle(myPen, rect)
    End If
End Sub

我的问题是,有没有其他方法可以从FlowLayoutPanel中删除闪烁,或者还有一种方法仍然让表单上的子控件对鼠标事件进行绘画?现在,唯一触发的PaintMe就是表单第一次实际加载控件时。

0 个答案:

没有答案