Double Buffering : Buffer « 2D « Visual C++ .NET

Home
Visual C++ .NET
1.2D
2.Class
3.Collections
4.Data Type
5.Database ADO.net
6.Delegate
7.Development
8.File Directory
9.Function
10.Generics
11.GUI Form
12.Language Basics
13.Network
14.Reflection
15.Security
16.Statement
17.Structure
18.Thread
19.XML
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Visual C++ .NET » 2D » Buffer 
Double Buffering
 

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
  Application::Run(gcnew Form1());
  return 0;
}

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

  public ref class Form1 : public System::Windows::Forms::Form
  {
  public:
    Form1(void)
    {
      InitializeComponent();
            this->SetStyle(ControlStyles::Opaque, true);

            dbBitmap = nullptr;
            dbGraphics = nullptr;
            X = -250;  // Preset to be just left of window

            Form1_Resize(nullptr, EventArgs::Empty);
    }
        System::Windows::Forms::Timer^  timer1;

    Bitmap^   dbBitmap;
        Graphics^ dbGraphics;
        int X;        // Actual x coordinate of Happy face

    void InitializeComponent(void)
    {
            this->timer1 = (gcnew System::Windows::Forms::Timer());
            this->SuspendLayout();
            // 
            // timer1
            // 
            this->timer1->Enabled = true;
            this->timer1->Interval = 10;
            this->timer1->Tick += 
                gcnew System::EventHandler(this, &Form1::timer1_Tick);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(613);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(500300);
            this->Name = L"Form1";
            this->Text = L"Sliding Happy Face";
            this->Paint += 
                gcnew System::Windows::Forms::PaintEventHandler(this, 
                                                         &Form1::Form1_Paint);
            this->Resize += 
                gcnew System::EventHandler(this, &Form1::Form1_Resize);
            this->ResumeLayout(false);

        }

        System::Void Form1_Paint(System::Object^ sender, 
                                 System::Windows::Forms::PaintEventArgs^ e)
        {
             // Move image at end of line start from beginning
            if (X < ClientRectangle.Width)
            {
                X ++;
                dbGraphics->TranslateTransform(1.00.0);
            }
            else
            {
                X = -250;
                dbGraphics->TranslateTransform(
                    (float)-(ClientRectangle.Width+250)0.0);
            }

            // Clear background
            dbGraphics->Clear(Color::White);


            // redraw image from scratch
            Pen^ b4pen = gcnew Pen(Color::Black, 4);

            Drawing::Rectangle Head  = Drawing::Rectangle(00250250);
            dbGraphics->FillEllipse(Brushes::Yellow, Head);
            dbGraphics->DrawEllipse(b4pen, Head);

            Drawing::Rectangle Mouth = Drawing::Rectangle(7515010050);
            dbGraphics->FillPie(Brushes::White, Mouth,0,180);
            dbGraphics->DrawPie(b4pen, Mouth, 0180);

            Drawing::Rectangle LEye  = Drawing::Rectangle(75752525);
            dbGraphics->FillEllipse(Brushes::White, LEye);
            dbGraphics->DrawEllipse(b4pen, LEye);

            Drawing::Rectangle REye  = Drawing::Rectangle(150752525);
            dbGraphics->FillEllipse(Brushes::White, REye);
            dbGraphics->DrawEllipse(b4pen, REye);

            // Make the buffer visible
            e->Graphics->DrawImageUnscaled(dbBitmap, 00);

            delete b4pen;
        }

        System::Void Form1_Resize(System::Object^ sender, System::EventArgs^ e)
        {
             // Get rid of old stuff
            if (dbGraphics != nullptr)
            {
                delete dbGraphics;
            }

            if (dbBitmap != nullptr)
            {
                delete dbBitmap;
            }

            if (ClientRectangle.Width > && ClientRectangle.Height > 0)
            {
                // Create a bitmap
                dbBitmap = gcnew Bitmap(ClientRectangle.Width,
                                        ClientRectangle.Height);

                // Grab its Graphics
                dbGraphics = Graphics::FromImage(dbBitmap);

                // Set up initial translation after resize (also at start)
                dbGraphics->TranslateTransform((float)X, 25.0);
            }
       }


        System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
        {
            // Move the image
            Invalidate();
       }
  };

   
  
Related examples in the same category
1.Single Buffering
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.