0

I did:

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)    
{    
  DrawingBoard = new Graphics::TBitmap;    
  DrawingBoard->Width = this->PaintBox1->Width;     
  DrawingBoard->Height = this->PaintBox1->Height;    
}

void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{     
        PaintBox1->Canvas->Pen->Color = clBlack;    
        Canvas->MoveTo(256,216);    
        Canvas->LineTo(270,250);    
}


void __fastcall TForm1::FormShow(TObject *Sender)    
{      
      PaintBox1->Canvas->Draw(256, 216, DrawingBoard);
}

So I saw the line but not inside the paint box-why?

Pierre-Alexandre Moller
  • 2,354
  • 1
  • 20
  • 29
user687459
  • 143
  • 5
  • 17

1 Answers1

0
  1. because you use Canvas-> instead of PaintBox1->Canvas->

    • so you draw on the this->Canvas-> which is Form1 in your case
  2. I assume you want to use bitmap as back buffer

    • then you should use DrawingBoard->Canvas instead of all PaintBox1->Canvas->
    • except the last in FormShow
    • also I would use OnPaint instead of OnShow ...
    • also you should use client size not size
    • also do not draw bitmap offseted to PaintBox the VCL translate the coordinates by itself
    • do not forget to clear the backbuffer before rendering (unless you are doing some cumulative drawing/effect)

Something like this:

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)    
{    
  DrawingBoard = new Graphics::TBitmap;    
  DrawingBoard->Width =  PaintBox1->ClientWidth;     
  DrawingBoard->Height = PaintBox1->ClientHeight;    
}

void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{     
        DrawingBoard->Canvas->Pen->Color = clBlack;    
        DrawingBoard->Canvas->Brush->Color = clWhite;    
        DrawingBoard->Canvas->FillRect(TRect(0,0,DrawingBoard->Width,DrawingBoard->Height));
        DrawingBoard->Canvas->MoveTo(256,216);    
        DrawingBoard->Canvas->LineTo(270,250);    
        PaintBox1->Canvas->Draw(0,0,DrawingBoard);
}

[Notes]

  • if your PaintBox is resize-able then add its OnResize event and resize the bitmap inside ...
Spektre
  • 49,595
  • 11
  • 110
  • 380