Cliquez pour évaluer et commenter
MSDN
MSDN Library
Développement .NET
.NET Framework 3.5
.NET Framework
Bibliothèque de classes ....
System.Drawing, espace de noms
BufferedGraphics, classe

  Passer à l'affichage pour faible bande passante
Cette page est spécifique à
Microsoft Visual Studio 2008/.NET Framework 3.5

D'autres versions sont également disponibles pour :
Bibliothèque de classes .NET Framework
BufferedGraphics, classe

Mise à jour : novembre 2007

Fournit une mémoire tampon de graphiques pour le mécanisme de double tampon.

Espace de noms :  System.Drawing
Assembly :  System.Drawing (dans System.Drawing.dll)

Visual Basic (Déclaration)
Public NotInheritable Class BufferedGraphics _
    Implements IDisposable
Visual Basic (Utilisation)
Dim instance As BufferedGraphics
C#
public sealed class BufferedGraphics : IDisposable
VisualC++
public ref class BufferedGraphics sealed : IDisposable
J#
public final class BufferedGraphics implements IDisposable
JScript
public final class BufferedGraphics implements IDisposable

La classe BufferedGraphics permet d'implémenter le mécanisme de double tampon personnalisé pour vos graphiques. Elle fournit un wrapper pour une mémoire tampon de graphiques, ainsi que les méthodes que vous pouvez utiliser pour écrire dans une mémoire tampon et restituer son contenu dans un périphérique de sortie.

Les graphiques qui utilisent le mécanisme de double tampon peuvent réduire ou éliminer le scintillement provoqué lorsque la surface d'affichage est redessinée. Lorsque vous utilisez le mécanisme de double tampon, les graphiques mis à jour sont d'abord dessinés dans une mémoire tampon, puis le contenu de cette mémoire tampon est rapidement écrit dans une partie ou dans l'intégralité de la surface affichée. Cette substitution relativement brève des graphiques affichés réduit généralement ou élimine le scintillement susceptible de survenir lorsque les graphiques sont mis à jour.

Remarque :

La façon la plus simple d'utiliser le mécanisme de double tampon est de définir l'indicateur de style de contrôle OptimizedDoubleBuffer sur un contrôle à l'aide de la méthode SetStyle. La définition de l'indicateur OptimizedDoubleBuffer pour un contrôle redirige toute la peinture du contrôle via une mémoire tampon de graphiques par défaut, sans requérir de code supplémentaire. Cet indicateur a la valeur true par défaut.

La classe BufferedGraphics n'a aucun constructeur public et doit être créée par BufferedGraphicsContext pour un domaine d'application qui utilise sa méthode Allocate. Vous pouvez récupérer BufferedGraphicsContext pour le domaine d'application actuel à partir de la propriété BufferedGraphicsManager..::.Current statique.

La propriété Graphics peut être utilisée pour dessiner dans la mémoire tampon de graphiques. Cette propriété fournit l'accès à l'objet Graphics qui dessine dans la mémoire tampon de graphiques allouée à cet objet BufferedGraphics.

La méthode Render sans argument dessine le contenu de la mémoire tampon de graphiques dans la surface spécifiée au moment où la mémoire tampon a été allouée. D'autres surcharges de la méthode Render vous permettent de spécifier un objet Graphics ou un objet IntPtr qui pointe vers un contexte de périphérique dans lequel dessiner le contenu de la mémoire tampon de graphiques.

Pour plus d'informations sur la façon de dessiner des graphiques mis deux fois en mémoire tampon, consultez Graphiques mis deux fois en mémoire tampon.

L'exemple de code suivant illustre l'utilisation d'un objet BufferedGraphics pour dessiner des graphiques à l'aide de différents types d'implémentation de mise en mémoire tampon. Chaque clic sur le formulaire entraîne le démarrage ou l'arrêt d'une minuterie qui provoque la mise à jour des dessins. La mise à jour des dessins vous permet d'observer l'effet du mécanisme de double tampon. Lorsque vous cliquez sur le formulaire avec le bouton droit, vous faites défiler les modes de dessin suivant s:

Dans chaque mode, le texte dessiné identifie le mode actif et décrit le comportement généré chaque fois que vous appuyez sur un bouton de la souris.

Visual Basic
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Public Class BufferingExample
    Inherits Form
    Private context As BufferedGraphicsContext
    Private grafx As BufferedGraphics

    Private bufferingMode As Byte
    Private bufferingModeStrings As String() = _
        {"Draw to Form without OptimizedDoubleBufferring control style", _
         "Draw to Form using OptimizedDoubleBuffering control style", _
         "Draw to HDC for form"}

    Private timer1 As System.Windows.Forms.Timer
    Private count As Byte    

    Public Sub New()
        ' Configure the Form for this example.
        Me.Text = "User double buffering"
        AddHandler Me.MouseDown, AddressOf Me.MouseDownHandler
        AddHandler Me.Resize, AddressOf Me.ResizeHandler
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint, True)

        ' Configure a timer to draw graphics updates.
        timer1 = New System.Windows.Forms.Timer()
        timer1.Interval = 200
        AddHandler timer1.Tick, AddressOf Me.OnTimer

        bufferingMode = 2
        count = 0

        ' Retrieves the BufferedGraphicsContext for the 
        ' current application domain.
        context = BufferedGraphicsManager.Current

        ' Sets the maximum size for the primary graphics buffer
        ' of the buffered graphics context for the application
        ' domain.  Any allocation requests for a buffer larger 
        ' than this will create a temporary buffered graphics 
        ' context to host the graphics buffer.
        context.MaximumBuffer = New Size(Me.Width + 1, Me.Height + 1)

        ' Allocates a graphics buffer the size of this form
        ' using the pixel format of the Graphics created by 
        ' the Form.CreateGraphics() method, which returns a 
        ' Graphics object that matches the pixel format of the form.
        grafx = context.Allocate(Me.CreateGraphics(), _
            New Rectangle(0, 0, Me.Width, Me.Height))

        ' Draw the first frame to the buffer.
        DrawToBuffer(grafx.Graphics)
    End Sub    

    Private Sub MouseDownHandler(sender As Object, e As MouseEventArgs)
        If e.Button = MouseButtons.Right Then
            ' Cycle the buffering mode.
            bufferingMode = bufferingMode+1
            If bufferingMode > 2 Then
                bufferingMode = 0
            End If 
            ' If the previous buffering mode used 
            ' the OptimizedDoubleBuffering ControlStyle,
            ' disable the control style.
            If bufferingMode = 1 Then
                Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
            End If 
            ' If the current buffering mode uses
            ' the OptimizedDoubleBuffering ControlStyle,
            ' enabke the control style.
            If bufferingMode = 2 Then
                Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
            End If 
            ' Cause the background to be cleared and redraw.
            count = 6
            DrawToBuffer(grafx.Graphics)
            Me.Refresh()
        Else
            ' Toggle whether the redraw timer is active.
            If timer1.Enabled Then
                timer1.Stop()
            Else
                timer1.Start()
            End If
        End If
    End Sub

    Private Sub OnTimer(sender As Object, e As EventArgs)
        ' Draw randomly positioned ellipses to the buffer.
        DrawToBuffer(grafx.Graphics)

        ' If in bufferingMode 2, draw to the form's HDC.
        If bufferingMode = 2 Then
            ' Render the graphics buffer to the form's HDC.
            grafx.Render(Graphics.FromHwnd(Me.Handle))
        ' If in bufferingMode 0 or 1, draw in the paint method.
        Else
            Me.Refresh()
        End If
    End Sub

    Private Sub ResizeHandler(sender As Object, e As EventArgs)
        ' Re-create the graphics buffer for a new window size.
        context.MaximumBuffer = New Size(Me.Width + 1, Me.Height + 1)
        If (grafx IsNot Nothing) Then
            grafx.Dispose()
            grafx = Nothing
        End If
        grafx = context.Allocate(Me.CreateGraphics(), New Rectangle(0, 0, Me.Width, Me.Height))

        ' Cause the background to be cleared and redraw.
        count = 6
        DrawToBuffer(grafx.Graphics)
        Me.Refresh()
    End Sub    

    Private Sub DrawToBuffer(g As Graphics)
        ' Clear the graphics buffer every five updates.
        count = count+1
        If count > 5 Then
            count = 0
            grafx.Graphics.FillRectangle(Brushes.Black, 0, 0, Me.Width, Me.Height)
        End If

        ' Draw randomly positioned and colored ellipses.
        Dim rnd As New Random()
        Dim i As Integer
        For i = 0 To 21
            Dim px As Integer = rnd.Next(20, Me.Width - 40)
            Dim py As Integer = rnd.Next(20, Me.Height - 40)
            g.DrawEllipse(New Pen(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), _
                rnd.Next(0, 255)), 1), px, py, px + rnd.Next(0, Me.Width - px - 20), _
                py + rnd.Next(0, Me.Height - py - 20))
        Next i

        ' Draw information strings.
        g.DrawString("Buffering Mode: " + bufferingModeStrings(bufferingMode), _
            New Font("Arial", 8), Brushes.White, 10, 10)
        g.DrawString("Right-click to cycle buffering mode", New Font("Arial", 8), _
            Brushes.White, 10, 22)
        g.DrawString("Left-click to toggle timed display refresh", _
            New Font("Arial", 8), Brushes.White, 10, 34)
    End Sub    

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        grafx.Render(e.Graphics)
    End Sub   

    <STAThread()>  _
    Public Shared Sub Main(args() As String)
        Application.Run(New BufferingExample())
    End Sub

End Class

C#
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace BufferingExample 
{
    public class BufferingExample : Form
    {
        private BufferedGraphicsContext context;
        private BufferedGraphics grafx;

    private byte bufferingMode;
    private string[] bufferingModeStrings = 
        { "Draw to Form without OptimizedDoubleBufferring control style",
          "Draw to Form using OptimizedDoubleBuffering control style",
          "Draw to HDC for form" };

    private System.Windows.Forms.Timer timer1;
    private byte count;

        public BufferingExample() : base()
        {
            // Configure the Form for this example.
            this.Text = "User double buffering";
            this.MouseDown += new MouseEventHandler(this.MouseDownHandler);
            this.Resize += new EventHandler(this.OnResize);
            this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true );

            // Configure a timer to draw graphics updates.
        timer1 = new System.Windows.Forms.Timer();
        timer1.Interval = 200;
        timer1.Tick += new EventHandler(this.OnTimer);

        bufferingMode = 2;
        count = 0;

            // Retrieves the BufferedGraphicsContext for the 
            // current application domain.
            context = BufferedGraphicsManager.Current;

            // Sets the maximum size for the primary graphics buffer
            // of the buffered graphics context for the application
            // domain.  Any allocation requests for a buffer larger 
            // than this will create a temporary buffered graphics 
            // context to host the graphics buffer.
            context.MaximumBuffer = new Size(this.Width+1, this.Height+1);

            // Allocates a graphics buffer the size of this form
            // using the pixel format of the Graphics created by 
            // the Form.CreateGraphics() method, which returns a 
            // Graphics object that matches the pixel format of the form.
            grafx = context.Allocate(this.CreateGraphics(), 
                 new Rectangle( 0, 0, this.Width, this.Height ));

        // Draw the first frame to the buffer.
        DrawToBuffer(grafx.Graphics);
        }

    private void MouseDownHandler(object sender, MouseEventArgs e)
        {
        if( e.Button == MouseButtons.Right )
        {
                 // Cycle the buffering mode.
             if( ++bufferingMode > 2 )
                     bufferingMode = 0;

                 // If the previous buffering mode used 
                 // the OptimizedDoubleBuffering ControlStyle,
                 // disable the control style.
                 if( bufferingMode == 1 )
                     this.SetStyle( ControlStyles.OptimizedDoubleBuffer, true );

                 // If the current buffering mode uses
                 // the OptimizedDoubleBuffering ControlStyle,
                 // enabke the control style.
                 if( bufferingMode == 2 )
                     this.SetStyle( ControlStyles.OptimizedDoubleBuffer, false );

                 // Cause the background to be cleared and redraw.
                 count = 6;
                 DrawToBuffer(grafx.Graphics);                 
         this.Refresh();
        }
        else
        {
                // Toggle whether the redraw timer is active.
        if( timer1.Enabled )
            timer1.Stop();
        else
            timer1.Start(); 
        }
        }

    private void OnTimer(object sender, EventArgs e)
    {
            // Draw randomly positioned ellipses to the buffer.
        DrawToBuffer(grafx.Graphics);

            // If in bufferingMode 2, draw to the form's HDC.
        if( bufferingMode == 2 )            
                    // Render the graphics buffer to the form's HDC.
            grafx.Render(Graphics.FromHwnd(this.Handle));            
            // If in bufferingMode 0 or 1, draw in the paint method.
        else
              this.Refresh();
    }

        private void OnResize(object sender, EventArgs e)
        {
           // Re-create the graphics buffer for a new window size.
           context.MaximumBuffer = new Size(this.Width+1, this.Height+1);
           if( grafx != null )
           {
           grafx.Dispose();
               grafx = null;               
           }
           grafx = context.Allocate(this.CreateGraphics(), 
               new Rectangle( 0, 0, this.Width, this.Height ));

           // Cause the background to be cleared and redraw.
           count = 6;
           DrawToBuffer(grafx.Graphics);                 
       this.Refresh();
        }    

    private void DrawToBuffer(Graphics g)
    {
            // Clear the graphics buffer every five updates.
        if( ++count > 5 )
            {
                count = 0;                
                grafx.Graphics.FillRectangle(Brushes.Black, 0, 0, this.Width, this.Height);
            }

            // Draw randomly positioned and colored ellipses.
        Random rnd = new Random();
        for( int i=0; i<20; i++ )
        {
        int px = rnd.Next(20,this.Width-40);
        int py = rnd.Next(20,this.Height-40);
        g.DrawEllipse(new Pen(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0,255), rnd.Next(0,255)), 1), 
            px, py, px+rnd.Next(0, this.Width-px-20), py+rnd.Next(0, this.Height-py-20));                     
        }

            // Draw information strings.
        g.DrawString("Buffering Mode: "+bufferingModeStrings[bufferingMode], new Font("Arial", 8), Brushes.White, 10, 10);
            g.DrawString("Right-click to cycle buffering mode", new Font("Arial", 8), Brushes.White, 10, 22);
            g.DrawString("Left-click to toggle timed display refresh", new Font("Arial", 8), Brushes.White, 10, 34);
    }

    protected override void OnPaint(PaintEventArgs e)
        {            
        grafx.Render(e.Graphics);
    }

        [STAThread]
        public static void Main(string[] args)
        {
        Application.Run(new BufferingExample());
        }
    }
}

VisualC++
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace BufferingExample
{
   public ref class BufferingExample: public Form
   {
   private:
      BufferedGraphicsContext^ context;
      BufferedGraphics^ grafx;
      Byte bufferingMode;
      array<String^>^bufferingModeStrings;
      System::Windows::Forms::Timer^ timer1;
      Byte count;

   public:
      BufferingExample()
         : Form()
      {
         array<String^>^tempStrings = {"Draw to Form without OptimizedDoubleBufferring control style","Draw to Form using OptimizedDoubleBuffering control style","Draw to HDC for form"};
         bufferingModeStrings = tempStrings;

         // Configure the Form for this example.
         this->Text = "User double buffering";
         this->MouseDown += gcnew MouseEventHandler( this, &BufferingExample::MouseDownHandler );
         this->Resize += gcnew EventHandler( this, &BufferingExample::OnResize );
         this->SetStyle( static_cast<ControlStyles>(ControlStyles::AllPaintingInWmPaint | ControlStyles::UserPaint), true );

         // Configure a timer to draw graphics updates.
         timer1 = gcnew System::Windows::Forms::Timer;
         timer1->Interval = 200;
         timer1->Tick += gcnew EventHandler( this, &BufferingExample::OnTimer );
         bufferingMode = 2;
         count = 0;

         // Retrieves the BufferedGraphicsContext for the 
         // current application domain.
         context = BufferedGraphicsManager::Current;

         // Sets the maximum size for the primary graphics buffer
         // of the buffered graphics context for the application
         // domain.  Any allocation requests for a buffer larger 
         // than this will create a temporary buffered graphics 
         // context to host the graphics buffer.
         context->MaximumBuffer = System::Drawing::Size( this->Width + 1, this->Height + 1 );

         // Allocates a graphics buffer the size of this form
         // using the pixel format of the Graphics created by 
         // the Form.CreateGraphics() method, which returns a 
         // Graphics object that matches the pixel format of the form.
         grafx = context->Allocate( this->CreateGraphics(), Rectangle(0,0,this->Width,this->Height) );

         // Draw the first frame to the buffer.
         DrawToBuffer( grafx->Graphics );
      }

   private:
      void MouseDownHandler( Object^ /*sender*/, MouseEventArgs^ e )
      {
         if ( e->Button == ::MouseButtons::Right )
         {
            // Cycle the buffering mode.
            if ( ++bufferingMode > 2 )
                        bufferingMode = 0;

            // If the previous buffering mode used 
            // the OptimizedDoubleBuffering ControlStyle,
            // disable the control style.
            if ( bufferingMode == 1 )
                        this->SetStyle( ControlStyles::OptimizedDoubleBuffer, true );

            // If the current buffering mode uses
            // the OptimizedDoubleBuffering ControlStyle,
            // enabke the control style.
            if ( bufferingMode == 2 )
                        this->SetStyle( ControlStyles::OptimizedDoubleBuffer, false );

            // Cause the background to be cleared and redraw.
            count = 6;
            DrawToBuffer( grafx->Graphics );
            this->Refresh();
         }
         else
         {

            // Toggle whether the redraw timer is active.
            if ( timer1->Enabled )
                        timer1->Stop();
            else
                        timer1->Start();
         }
      }

   private:
      void OnTimer( Object^ /*sender*/, EventArgs^ /*e*/ )
      {
         // Draw randomly positioned ellipses to the buffer.
         DrawToBuffer( grafx->Graphics );

         // If in bufferingMode 2, draw to the form's HDC.
         if ( bufferingMode == 2 )

         // Render the graphics buffer to the form's HDC.
         grafx->Render( Graphics::FromHwnd( this->Handle ) );
         // If in bufferingMode 0 or 1, draw in the paint method.
         else

         // If in bufferingMode 0 or 1, draw in the paint method.
         this->Refresh();
      }

      void OnResize( Object^ /*sender*/, EventArgs^ /*e*/ )
      {
         // Re-create the graphics buffer for a new window size.
         context->MaximumBuffer = System::Drawing::Size( this->Width + 1, this->Height + 1 );
         if ( grafx != nullptr )
         {
            delete grafx;
            grafx = nullptr;
         }

         grafx = context->Allocate( this->CreateGraphics(), Rectangle(0,0,this->Width,this->Height) );

         // Cause the background to be cleared and redraw.
         count = 6;
         DrawToBuffer( grafx->Graphics );
         this->Refresh();
      }

      void DrawToBuffer( Graphics^ g )
      {
         // Clear the graphics buffer every five updates.
         if ( ++count > 5 )
         {
            count = 0;
            grafx->Graphics->FillRectangle( Brushes::Black, 0, 0, this->Width, this->Height );
         }

         // Draw randomly positioned and colored ellipses.
         Random^ rnd = gcnew Random;
         for ( int i = 0; i < 20; i++ )
         {
            int px = rnd->Next( 20, this->Width - 40 );
            int py = rnd->Next( 20, this->Height - 40 );
            g->DrawEllipse( gcnew Pen( Color::FromArgb( rnd->Next( 0, 255 ), rnd->Next( 0, 255 ), rnd->Next( 0, 255 ) ), 1.0f ), px, py, px + rnd->Next( 0, this->Width - px - 20 ), py + rnd->Next( 0, this->Height - py - 20 ) );
         }

         // Draw information strings.
         g->DrawString( String::Format( "Buffering Mode: {0}", bufferingModeStrings[ bufferingMode ] ), gcnew System::Drawing::Font( "Arial",8 ), Brushes::White, 10, 10 );
         g->DrawString( "Right-click to cycle buffering mode", gcnew System::Drawing::Font( "Arial",8 ), Brushes::White, 10, 22 );
         g->DrawString( "Left-click to toggle timed display refresh", gcnew System::Drawing::Font( "Arial",8 ), Brushes::White, 10, 34 );
      }

   protected:
      virtual void OnPaint( PaintEventArgs^ e ) override
      {
         grafx->Render( e->Graphics );
      }
   };
}

[STAThread]
int main()
{
   Application::Run( gcnew BufferingExample::BufferingExample );
}

J#
package BufferingExample; 

import System.*;
import System.ComponentModel.*;
import System.Drawing.*;
import System.Windows.Forms.*;

public class BufferingExample extends Form
{
    private BufferedGraphicsContext context;
    private BufferedGraphics grafx;
    private ubyte bufferingMode;
    private String bufferingModeStrings[] =  
        { "Draw to Form without OptimizedDoubleBufferring control style", 
        "Draw to Form using OptimizedDoubleBuffering control style", 
        "Draw to HDC for form" };
    private System.Windows.Forms.Timer timer1;
    private ubyte count;

    public BufferingExample()
    {
        super();
        // Configure the Form for this example.
        this.set_Text("User double buffering");
        this.add_MouseDown(new MouseEventHandler(this.MouseDownHandler));
        this.add_Resize(new EventHandler(this.OnResize));
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
        // Configure a timer to draw graphics updates.
        timer1 = new System.Windows.Forms.Timer();
        timer1.set_Interval(200);
        timer1.add_Tick(new EventHandler(this.OnTimer));
        bufferingMode = 2;
        count = 0;
        // Retrieves the BufferedGraphicsContext for the 
        // current application domain.
        context = BufferedGraphicsManager.get_Current();
        // Sets the maximum size for the primary graphics buffer
        // of the buffered graphics context for the application
        // domain.  Any allocation requests for a buffer larger 
        // than this will create a temporary buffered graphics 
        // context to host the graphics buffer.
        context.set_MaximumBuffer(new Size(this.get_Width() + 1, this.get_Height() + 1));
        // Allocates a graphics buffer the size of this form
        // using the pixel format of the Graphics created by 
        // the Form.CreateGraphics() method, which returns a 
        // Graphics object that matches the pixel format of the form.
        grafx = context.Allocate(this.CreateGraphics(), new Rectangle(0, 0, this.get_Width(), this.get_Height()));
        // Draw the first frame to the buffer.
        DrawToBuffer(grafx.get_Graphics());
    } //BufferingExample

    private void MouseDownHandler(Object sender, MouseEventArgs e)
    {
        if (e.get_Button().Equals(MouseButtons.Right)) {
            // Cycle the buffering mode.
            if (++bufferingMode > 2) {
                bufferingMode = 0;
            }

            // If the previous buffering mode used 
            // the OptimizedDoubleBuffering ControlStyle,
            // disable the control style.
            if (bufferingMode == 1) {
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            }

            // If the current buffering mode uses
            // the OptimizedDoubleBuffering ControlStyle,
            // enabke the control style.
            if (bufferingMode == 2) {
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
            }

            // Cause the background to be cleared and redraw.
            count = 6;
            DrawToBuffer(grafx.get_Graphics());
            this.Refresh();
        }
        else {
            // Toggle whether the redraw timer is active.
            if (timer1.get_Enabled()) {
                timer1.Stop();
            }
            else {
                timer1.Start();
            }
        }
    } //MouseDownHandler

    private void OnTimer(Object sender, EventArgs e)
    {
        // Draw randomly positioned ellipses to the buffer.
        DrawToBuffer(grafx.get_Graphics());

        // If in bufferingMode 2, draw to the form's HDC.
        if (bufferingMode == 2) {
            // Render the graphics buffer to the form's HDC.
            grafx.Render(this.get_Handle());
        }
        // If in bufferingMode 0 or 1, draw in the paint method.
        else {
            this.Refresh();
        }
    } //OnTimer

    private void OnResize(Object sender, EventArgs e)
    {
        // Re-create the graphics buffer for a new window size.
        context.set_MaximumBuffer(new Size(this.get_Width() + 1, 
            this.get_Height() + 1));
        if (grafx != null) {
            grafx.Dispose();
            grafx = null;
        }
        grafx = context.Allocate(this.CreateGraphics(), 
            new Rectangle(0, 0, this.get_Width(), this.get_Height()));

        // Cause the background to be cleared and redraw.
        count = 6;
        DrawToBuffer(grafx.get_Graphics());
        this.Refresh();
    } //OnResize

    private void DrawToBuffer(Graphics g)
    {
        // Clear the graphics buffer every five updates.
        if (++count > 5) {
            count = 0;
            grafx.get_Graphics().FillRectangle(Brushes.get_Black(), 0, 0, 
                this.get_Width(), this.get_Height());
        }

        // Draw randomly positioned and colored ellipses.
        Random rnd = new Random();
        for (int i = 0; i < 20; i++) {
            int px = rnd.Next(20, this.get_Width() - 40);
            int py = rnd.Next(20, this.get_Height() - 40);
            g.DrawEllipse(new Pen(Color.FromArgb(rnd.Next(0, 255), 
                rnd.Next(0, 255), rnd.Next(0, 255)), 1), px, py, 
                px + rnd.Next(0, this.get_Width() - px - 20), 
                py + rnd.Next(0, this.get_Height() - py - 20));
        }

        // Draw information strings.
        g.DrawString("Buffering Mode: " 
            + bufferingModeStrings.get_Item(bufferingMode), 
            new Font("Arial", 8), Brushes.get_White(), 10, 10);
        g.DrawString("Right-click to cycle buffering mode", 
            new Font("Arial", 8), Brushes.get_White(), 10, 22);
        g.DrawString("Left-click to toggle timed display refresh",
            new Font("Arial", 8), Brushes.get_White(), 10, 34);
    } //DrawToBuffer

    protected void OnPaint(PaintEventArgs e)
    {
        grafx.Render(e.get_Graphics());
    } //OnPaint

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new BufferingExample());
    } //main
} //BufferingExample

System..::.Object
  System.Drawing..::.BufferedGraphics

La classe BufferedGraphics n'est pas thread-safe. Lors de l'accès à une mémoire tampon de graphiques à partir de threads séparés, il est important d'utiliser un mécanisme de contrôle d'accès de thread pour éviter les conflits.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professionnel Édition x64, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

Le .NET Framework et le .NET Compact Framework ne prennent pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

.NET Framework

Pris en charge dans : 3.5, 3.0, 2.0
Contenu de la communauté   Qu'est-ce que le Contenu de la communauté ?
Ajouter du contenu RSS  Annotations
Processing
© 2009 Microsoft Corporation. Tous droits réservés. Conditions d'utilisation  |  Marques  |  Confidentialité
Page view tracker