1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| __fastcall jGraphicControl::jGraphicControl(TComponent *Owner)
: TGraphicControl(Owner)
{
if(Owner->InheritsFrom(__classid (TWinControl))) Parent = (TWinControl*)Owner;
Bitmap = new Graphics::TBitmap;
Image = NULL; //tant qu'on n'a pas chargé d'image
IsMoving = false;
OnMouseDown = MseDown;
OnMouseMove = MseMove;
OnMouseUp = MseUp;
}
__fastcall jGraphicControl::~jGraphicControl()
{
if(Image != NULL) delete Image; Image = NULL;
delete Bitmap;
}
void __fastcall jGraphicControl::Paint()
{
if(Image != NULL)
{
if(Bitmap->Width != Width) Bitmap->Width = Width;
if(Bitmap->Height != Height) Bitmap->Height = Height;
Bitmap->Canvas->Brush->Color = Parent->Brush->Color;
Bitmap->Canvas->FillRect(Rect(0,0,Width, Height));
TRect Dest = Rect(0,0,Width, Height);
TRect Srce = Rect(HotPoint.x,
HotPoint.y,
HotPoint.x + Width,
HotPoint.y + Height);
Bitmap->Canvas->CopyRect(Dest, Image->Canvas, Srce);
Canvas->Draw(0,0,Bitmap);
}
else
{
Canvas->Brush->Style = bsClear;
Canvas->Pen->Color = clBlack;
Canvas->Pen->Mode = pmCopy;
Canvas->Pen->Style = psDashDot;
Canvas->Rectangle(0,0,Width, Height);
}
}
void __fastcall jGraphicControl::LoadFromFile(AnsiString A)
{
if(FileExists(A))
{
if(Image == NULL) Image = new Graphics::TBitmap;
Image->LoadFromFile(A);
HotPoint = Point(0,0);
Paint();
}
else
{
ShowMessage("Fichier image introuvable");
}
}
void __fastcall jGraphicControl::AbsoluteMove(int X, int Y)
{
HotPoint = Point(X,Y);
Paint();
}
void __fastcall jGraphicControl::RelativeMove(int X, int Y)
{
HotPoint = Point(HotPoint.x - X, HotPoint.y - Y);
Paint();
}
//Je rajoute ces 3 méthodes, juste pour le fun. Elles vont permettre
//à l'utilisateur de faire bouger l'image avec la souris (drag)
void __fastcall jGraphicControl::MseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
IsMoving = true;
Move_xy = Point(X,Y);
}
void __fastcall jGraphicControl::MseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(IsMoving)
{
RelativeMove(X - Move_xy.x, Y - Move_xy.y);
Move_xy = Point(X,Y);
}
}
void __fastcall jGraphicControl::MseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
IsMoving = false;
} |
Partager