Double buffering

cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 - 15 mars 2012 à 14:18
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 - 17 mars 2012 à 22:39
Bonjour à tous

J'essaie d'utiliser le double buffering
le but dessiner dans une panel une suite de rectangles identiques positionné au meme niveau
horizontal
la suite des coordonnées des rectangles sont
dans une list of rectangle
une variable pos qui sert d'index
pos = 0
avec un timer
dessiner au dessus d'un rectangle index par pos
une ligne vertical et le pos et incrimenter
le pos pour passer au rectangle suivant

j'ai réussi à dessiner les rectangles
quand le timer se déclenche je vois bien
une ligne au dessus du premier rectangle
mais au prochain tick du timer
toutes les lignes dessinés avant sont visibles
et pas seulement la ligne au dessus du rectangle indexé par pos

Private Structure imageflag
   Public rect As Rectangle
   Public flag As Boolean
End Structure
Private BufferredGraphic As BufferedGraphics
Private CurrentContext As BufferedGraphicsContext
 Private pos As Integer
Private listrect As New List(Of imageflag)Private Sub drawindicator()
        Dim mypen As New Pen(Color.Goldenrod, 2)
        Dim sizestring As New SizeF
        Dim myfont As New Font("arial", 22, FontStyle.Bold, GraphicsUnit.Pixel)
        sizestring = BufferredGraphic.Graphics.MeasureString(counter.ToString, myfont, sizestring)
        BufferredGraphic.Graphics.DrawLine(mypen, listrect(pos).rect.Left + listrect(pos).rect.Width \ 2, listrect(pos).rect.Top, listrect(pos).rect.Left + listrect(pos).rect.Width \ 2, listrect(pos).rect.Top - (HEIGHTFLAG - 20))
        BufferredGraphic.Graphics.DrawString((counter + 1).ToString, myfont, Brushes.Blue, listrect(pos).rect.Left + listrect(pos).rect.Width \ 2 - Convert.ToInt32(sizestring.Width) \ 2, listrect(pos).rect.Top - (HEIGHTFLAG - 20) - sizestring.Height)
        Me.Panelshow_Paint(Me, New PaintEventArgs(Me.BufferredGraphic.Graphics, Panelshow.DisplayRectangle))

    End Sub


  Private Sub Timeranimation_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timeranimation.Tick
    drawindicator()
    pos +=1
    if pos =  listrect.count then
        pos = 0
    end if
 end sub 
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
 CurrentContext = BufferedGraphicsManager.Current
 BufferredGraphic = Me.CurrentContext.Allocate(Panelshow.CreateGraphics(), Panelshow.DisplayRectangle)
        BufferredGraphic.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        BufferredGraphic.Graphics.Clear(Color.Cornsilk)
pos = 0
 Timeranimation.Interval = 1000
end sub
Private Sub Panelshow_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panelshow.Paint
 BufferredGraphic.Render()
end sub 



Merci

2 réponses

cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
16 mars 2012 à 11:42
avec un code plus simple
déplacer un rectangle sur la diagonal
utilisant le double buffering

Public Class Form1
    Private BufferredGraphic As BufferedGraphics
    Private pos As New Point
    Private CurrentContext As BufferedGraphicsContext

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Panel1.Width = ClientRectangle.Width
        Panel1.Height = ClientRectangle.Height
        Panel1.Left = 0
        Panel1.Top = 0
        CurrentContext = BufferedGraphicsManager.Current
        BufferredGraphic = Me.CurrentContext.Allocate(Panel1.CreateGraphics(), Panel1.DisplayRectangle)
        BufferredGraphic.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
        BufferredGraphic.Graphics.Clear(Color.Cornsilk)
        pos.X = 0
        pos.Y = 0
        Timer1.Interval = 10
        Timer1.Stop()
    End Sub
    Private Sub drawrect()
        Dim rect As New Rectangle
        rect.Location = New Point(pos.X, pos.Y)
        rect.Width = 50
        rect.Height = 50
        BufferredGraphic.Graphics.FillRectangle(Brushes.Blue, rect)
        Panel1_Paint(Panel1, New PaintEventArgs(Me.BufferredGraphic.Graphics, Panel1.DisplayRectangle))
    End Sub

    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        BufferredGraphic.Render()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        pos.X += 10
        pos.Y += 6
        If pos.X > Panel1.Right Or pos.Y > Panel1.Bottom Then
            Timer1.Stop()
        End If
        
        drawrect()
    End Sub

   
    Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
        Timer1.Start()
    End Sub
End Class


ça ne marche pas comme je veux
lors du déplacement tous les rectangles précedent sont visible
j'ai du oublier quelque chose ?
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
17 mars 2012 à 22:39
J'ai trouvé

il faut ajouter
BufferredGraphic.Graphics.Clear(Color.Cornsilk)

dans le code du Timer1_Tick
0
Rejoignez-nous