2016-10-11 4 views
1

페인트 패널이나 다른 컨트롤에서 애니메이션 된 컨트롤을 사용하고 싶지만 코드가 100 % 작동하지 않습니다. 이제 내 코드가 애니메이션을 깜박입니다. doublebuffered 변수를 true로 설정하면 컨트롤 배경이 검은 색으로 바뀝니다. Parent.Invalidate 대신에 Me.Invalidate()를 사용하면 애니메이션 그림이 매우 버그가됩니다.VB.net 애니메이션 패널의 알파벳순 컨트롤

Imports System.Reflection 

Public Class Form1 
    Private Sub FlowLayoutPanel1_Paint(sender As Object, e As PaintEventArgs) 
     Dim TheControl As Control = CType(sender, Control) 
     Dim oRAngle As Rectangle = New Rectangle(0, 0, TheControl.Width, TheControl.Height) 
     Dim oGradientBrush As Brush = New Drawing.Drawing2D.LinearGradientBrush(oRAngle, Color.White, Color.SteelBlue, Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal) 
     e.Graphics.FillRectangle(oGradientBrush, oRAngle) 
    End Sub 
    Public Shared Sub DoubleBufferedSet(ByVal dgv As Object, ByVal setting As Boolean) 
     Dim dgvType As Type = dgv.[GetType]() 
     Dim pi As PropertyInfo = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance Or BindingFlags.NonPublic) 
     pi.SetValue(dgv, setting, Nothing) 
    End Sub 
    Private Sub FlowLayoutPanel1_Resize(sender As Object, e As EventArgs) 
     sender.Invalidate() 
    End Sub 
    Dim flowlayoutpanel1 As New FlowLayoutPanels 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     flowlayoutpanel1.Dock = DockStyle.Fill 
     AddHandler flowlayoutpanel1.Paint, AddressOf FlowLayoutPanel1_Paint 
     AddHandler flowlayoutpanel1.Resize, AddressOf FlowLayoutPanel1_Resize 
     Me.Controls.Add(flowlayoutpanel1) 
     DoubleBufferedSet(flowlayoutpanel1, True) 
     Dim testc1 As New OpaqControl 
     testc1.Size = New Size(300, 100) 
     flowlayoutpanel1.Controls.Add(testc1) 
     Dim testc2 As New OpaqControl 
     testc2.Size = New Size(300, 100) 
     flowlayoutpanel1.Controls.Add(testc2) 
    End Sub 
End Class 
Public Class OpaqControl 
    Inherits Control 
    Private Timer1 As New Timer() 
    Dim up As Boolean = True 
    Dim poss As Integer = 1 
    Public Sub New() 
     'DoubleBuffered = True 
     AddHandler Timer1.Tick, AddressOf TickHandler 
     Me.Timer1.Interval = 10 
    End Sub 
    Protected Sub TickHandler(sender As Object, e As EventArgs) 
     If up Then 
      poss += 2 
      If poss >= 80 Then Me.Timer1.Enabled = False 
     Else 
      poss -= 2 
      If poss <= 0 Then Me.Timer1.Enabled = False 
     End If 
     Parent.Invalidate(New Rectangle(Me.Location, Me.Size), True) 
     'Me.Invalidate() 
    End Sub 
    Protected Overrides Sub OnMouseEnter(e As EventArgs) 
     up = True 
     Me.Timer1.Enabled = True 
     MyBase.OnMouseEnter(e) 
    End Sub 
    Protected Overrides Sub OnMouseLeave(e As EventArgs) 
     up = False 
     Me.Timer1.Enabled = True 
     MyBase.OnMouseLeave(e) 
    End Sub 
    Protected Overrides ReadOnly Property CreateParams() As CreateParams 
     Get 
      Dim cp As CreateParams = MyBase.CreateParams 
      cp.ExStyle = cp.ExStyle Or &H20 
      Return cp 
     End Get 
    End Property 
    Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs) 
    End Sub 
    Protected Overrides Sub OnPaint(e As PaintEventArgs) 
     e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(50, 0, 100, 255)), New Rectangle(0, 0, 300, 100)) 
     e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(50, 0, 0, 0)), New Rectangle(0, 100 - poss, 300, 80)) 
     e.Graphics.DrawString("Test", Font, Brushes.Yellow, New Point(100, 100 - poss)) 
    End Sub 
End Class 

죄송합니다. 내 문제를 이해하려면 내 코드를 사용해보십시오 (아포스트로피 제거 사용).

VB 2015를 사용합니다. 타사 DLL을 사용하고 싶지 않습니다. WPF를 사용하고 싶지 않습니다.

답변

1

부모 그림 자체를 볼 수 있으므로 깜박입니다. 해당 유물을 제거하려면 더블 버퍼링이 필요합니다. 이 아닌은 WS_EX_TRANSPARENT을 사용합니다. 이중 버퍼링을 무효화합니다. 컨트롤 클래스는 이미이 같은 해당 기능을 활용 잘 투명성을 지원합니다

Public Sub New() 
    Me.DoubleBuffered = True 
    Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True) 
    Me.BackColor = Color.Transparent 
    AddHandler Timer1.Tick, AddressOf TickHandler 
    Me.Timer1.Interval = 10 
End Sub 

는 CreateParams을()와 OnPaintBackground() 오버라이드 (override)를 삭제합니다. Parent.Invalidate() 대신 Me.Invalidate()를 호출하십시오. 그리고 그것은 매끈 매끈합니다.