Restoring a Drawing

 

Introduction

If you draw on a canvas using the Mouse events, when the form looses focus, it also looses the image that was drawn. The only way you can keep your drawing is if you draw using the OnPaint event. This has to do with the operating system, not the VCL and you can get it regardless of the programming environment you are using.

This exercise shows how to restore that drawing when the form regains focus.

 

  1. Start Borland C++ Builder with the startup form.

  2. On the form, place a PaintBox control.

  3. In the private section of the form, declare the following variables:
     

    //---------------------------------------------------------------------------
    
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    #include <ExtCtrls.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:	// IDE-managed Components
        TPaintBox *PaintBox1;
    private:
        Graphics::TBitmap *DrawingBoard;
        Boolean IsDrawing;
        int StartX, StartY;	// User declarations
    public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
  4. In the source file of the form, you will need to initialize the DrawingBoard bitmap variable to occupy the PaintBox control.

  5. Then you will use the OnMouseDown, OnMouseUp, and OnPaint events of the PaintBox to do your thing.
    Here is the complete file
     

    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
        DrawingBoard = new Graphics::TBitmap;
        DrawingBoard->Width = this->PaintBox1->Width;
        DrawingBoard->Height = this->PaintBox1->Height;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y)
    {
        IsDrawing = True;
        StartX = X;
        StartY = Y;    
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::PaintBox1MouseUp(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y)
    {
        if( IsDrawing )
        {
            DrawingBoard->Canvas->Rectangle(StartX, StartY, X, Y);
            PaintBox1->Canvas->Draw(0, 0, DrawingBoard);
            IsDrawing = False;
        }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
    {
        PaintBox1->Canvas->Draw(0, 0, DrawingBoard);
    }
    //---------------------------------------------------------------------------
  6. Test the application

 

 

 

 

Copyright 2003-2007 FunctionX, Inc.