//---------------------------------------------------------------------------
#define STRICT
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "gdiplus.lib"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
ResizeWindow = this->WindowProc;
this->WindowProc = GetResizeWindow;
bitmapa = new Graphics::TBitmap();
bitmapa->LoadFromResourceName((int)HInstance, "PLIK");
PaintBox1->Width = bitmapa->Width;
PaintBox1->Height = bitmapa->Height;
this->AutoScroll = true;
this->DoubleBuffered = true;
fDraw = false;
gSize1 = 100;
gSize2 = 25;
graphRect = Rect(10, 10, 10 + gSize1, 10 + gSize2);
DrawRect(graphRect);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
Gdiplus::GdiplusShutdown(gdiplusToken);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GetResizeWindow(TMessage &Msg)
{
/* Przechwytywanie komunikatów o zmianie rorzmiaru okna */
if(Msg.Msg == WM_SIZE)
{
if( Msg.WParam == SIZE_MAXIMIZED || Msg.WParam == SIZE_RESTORED )
{
fDraw = false;
DrawCopyRect(graphRect);
graphRect.Left = 10;
graphRect.Top = 10;
graphRect.Right = 10 + gSize1;
graphRect.Bottom = 10 + gSize2;
this->VertScrollBar->Position = 0;
this->HorzScrollBar->Position = 0;
DrawRect(graphRect);
}
}
ResizeWindow(Msg);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DrawRect(TRect rDraw)
{
PaintBox1->Canvas->Pen->Color = clBlack;
PaintBox1->Canvas->Pen->Width = 1;
PaintBox1->Canvas->MoveTo(rDraw.Left, 0);
PaintBox1->Canvas->LineTo(rDraw.Left, PaintBox1->Height);
PaintBox1->Canvas->MoveTo(rDraw.Right, 0);
PaintBox1->Canvas->LineTo(rDraw.Right, PaintBox1->Height);
Gdiplus::Graphics grphxRect( PaintBox1->Canvas->Handle);
Gdiplus::SolidBrush sBrush( Gdiplus::Color(175, 255, 0, 0) ); /* 175 stopień przeźroczystości 0-255 */
Gdiplus::Pen blackPen( Gdiplus::Color(255, 0, 0, 0), 1 );
grphxRect.DrawRectangle(&blackPen, rDraw.Left, rDraw.Top,
rDraw.Width(), rDraw.Height() - 1);
grphxRect.FillRectangle(&sBrush, rDraw.Left + 1, rDraw.Top + 1,
rDraw.Width() - 1, rDraw.Height() - 2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DrawCopyRect(TRect rCopy)
{
TRect lineRect = Rect(rCopy.Left, 0, rCopy.Left + 1, PaintBox1->Height);
PaintBox1->Canvas->CopyRect(lineRect, bitmapa->Canvas, lineRect);
lineRect = Rect(rCopy.Right, 0, rCopy.Right + 1, PaintBox1->Height);
PaintBox1->Canvas->CopyRect(lineRect, bitmapa->Canvas, lineRect);
PaintBox1->Canvas->CopyRect(rCopy, bitmapa->Canvas, rCopy);
}
//---------------------------------------------------------------------------
BOOL __fastcall TForm1::FindRect(int X, int Y)
{
if( (X >= graphRect.Left && X <= graphRect.Left + gSize1) &&
(Y >= graphRect.Top && Y <= graphRect.Top + gSize2)
)
{
return true;
}
return false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(fDraw == true)
{
float Start = GetTickCount();
DrawCopyRect(graphRect);
int left = X - pbPoint.x;
int top = Y - pbPoint.y;
graphRect.Left = left;
graphRect.Top = top;
graphRect.Right = left + gSize1;
graphRect.Bottom = top + gSize2;
DrawRect(graphRect);
float End = GetTickCount();
Caption = "Draw Rectangle = " + FloatToStr(End - Start) + " ms";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if( FindRect(X, Y) )
{
pbPoint.x = X - graphRect.Left;
pbPoint.y = Y - graphRect.Top;
fDraw = true;
}
else
{
fDraw = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
fDraw = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
PaintBox1->Canvas->Draw(0, 0, bitmapa);
DrawRect(graphRect);
}
//---------------------------------------------------------------------------