//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TPoint Origin, MovePt;
bool Drawing;
enum TDrawingTool { dtLine, dtRectangle, dtFreeHand, dtRubber, dtBucket };
TDrawingTool DrawingTool;
TColor tlo;
TBrushStyle OldBrush, CurrentBrush;
TPenStyle OldPen, CurrentPen;
Graphics::TBitmap *HistoryBMP[4];
int i = 0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
tlo = clWhite;
Image1->Canvas->Brush->Color = tlo;
SpinEdit1->Value = Image1->Canvas->Pen->Width;
DoubleBuffered = true;
for(i = 0; i < 4; i++)
{
HistoryBMP[i] = new Graphics::TBitmap;
HistoryBMP[i]->Width = Form1->Image1->Picture->Width;
HistoryBMP[i]->Height = Form1->Image1->Picture->Height;
}
i = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
int X, int Y)
{
// Zapis poprzednich danych
OldBrush = Image1->Canvas->Brush->Style;
OldPen = Image1->Canvas->Pen->Style;
CurrentBrush = bsClear;
CurrentPen = psSolid;
// Ustawienie zaznaczenia;
HistoryBMP[i]->Canvas->Brush->Style = CurrentBrush;
HistoryBMP[i]->Canvas->Pen->Style = CurrentPen;
Drawing = true;
HistoryBMP[i]->Canvas->MoveTo(X, Y);
Origin = Point(X, Y);
MovePt = Origin;
if(DrawingTool == dtRubber) HistoryBMP[i]->Canvas->Pen->Color = tlo;
else HistoryBMP[i]->Canvas->Pen->Color = clBlack;
TVarRec tempvar[2] = {X, Y};
StatusBar1->Panels->Items[0]->Text = Format("Origin: (%d, %d)", tempvar, 2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
int X, int Y)
{
HistoryBMP[i]->Canvas->Brush->Style = OldBrush;
HistoryBMP[i]->Canvas->Pen->Style = OldPen;
if (Drawing)
{
DrawShape(Origin, Point(X, Y), pmCopy);
Drawing = false;
}
Image1->Canvas->Draw(0, 0, HistoryBMP[i]);
i++;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift, int X,
int Y)
{
if (Drawing)
{
if((DrawingTool == dtFreeHand) || (DrawingTool == dtRubber)) DrawShape(Origin, MovePt, pmCopy);
else DrawShape(Origin, MovePt, pmNotXor);
MovePt = Point(X, Y);
if((DrawingTool == dtFreeHand) || (DrawingTool == dtRubber)) DrawShape(Origin, MovePt, pmCopy);
else DrawShape(Origin, MovePt, pmNotXor);
}
TVarRec tempvar[2] = {X, Y};
StatusBar1->Panels->Items[1]->Text = Format("Current: (%d, %d)", tempvar, 2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DrawShape(TPoint TopLeft, TPoint BottomRight, TPenMode AMode){
Image1->Canvas->Pen->Mode = AMode;
switch (DrawingTool)
{
case dtLine:
{
HistoryBMP[i]->Canvas->MoveTo(TopLeft.x, TopLeft.y);
HistoryBMP[i]->Canvas->LineTo(BottomRight.x, BottomRight.y);
break;
}
case dtFreeHand:
{
HistoryBMP[i]->Canvas->LineTo(BottomRight.x, BottomRight.y);
break;
}
case dtRubber:
{
HistoryBMP[i]->Canvas->LineTo(BottomRight.x, BottomRight.y);
break;
}
case dtBucket:
{
HistoryBMP[i]->Canvas->FloodFill(TopLeft.x, TopLeft.y, HistoryBMP[i]->Canvas->Pixels[TopLeft.x][TopLeft.y], fsSurface);
break;
}
case dtRectangle:
{
HistoryBMP[i]->Canvas->Rectangle(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
break;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DrawingTool = dtRectangle;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
DrawingTool = dtLine;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
DrawingTool = dtFreeHand;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpinEdit1Change(TObject *Sender)
{
HistoryBMP[i]->Canvas->Pen->Width = SpinEdit1->Value;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
DrawingTool = dtRubber;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
HistoryBMP[i]->Canvas->Brush->Color = clRed;
DrawingTool = dtBucket;
}
//---------------------------------------------------------------------------