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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
__fastcall TCercle::TCercle(TComponent *Owner, Graphics::TBitmap *I)
: TCustomControl(Owner)
{
if(Owner->InheritsFrom(__classid(TWinControl)))
{
Parent = (TWinControl*)Owner;
}
Bitmap = new Graphics::TBitmap;
Bitmap->PixelFormat = pf24bit;
Source = new Graphics::TBitmap;
Source->PixelFormat = pf24bit;
Image = I;
Ofx = 0; Ofy = 0;
Flag = false;
OnMouseDown = CercleMouseDown;
OnMouseMove = CercleMouseMove;
OnMouseUp = CercleMouseUp;
}
//------
__fastcall TCercle::~TCercle()
{
delete Bitmap;
delete Source;
}
//------
void __fastcall TCercle::SetWidth(int Value)
{
TCustomControl::Width = Value;
Bitmap->Width = Value;
Source->Width = Value;
}
//------
void __fastcall TCercle::SetHeight(int Value)
{
TCustomControl::Height = Value;
Bitmap->Height = Value;
Source->Height = Value;
}
//------
int __fastcall TCercle::GetWidth()
{
return TCustomControl::Width;
}
//------
int __fastcall TCercle::GetHeight()
{
return TCustomControl::Height;
}
//------
void __fastcall TCercle::Paint()
{
//Effacer le bitmap qui va être affiché
TCanvas *B = Bitmap->Canvas;
B->Pen->Color = clSilver; //couleur de fond de la form
B->Pen->Style = psSolid;
B->Pen->Mode = pmCopy;
B->Brush->Color = clSilver;
B->Brush->Style = bsSolid;
B->Rectangle(0,0,Width, Height);
//Effacer le bitmap intermédiaire
TCanvas *S = Source->Canvas;
S->Pen->Color = clBlack;
S->Pen->Style = psSolid;
S->Pen->Mode = pmCopy;
S->Brush->Color = clBlack;
S->Brush->Style = bsSolid;
S->Rectangle(0, 0, Width, Height);
//Copier une zone de l'image dans le bitmap intermédiaire
TRect Dest = Rect(0, 0, Source->Width, Source->Height);
TRect Srce = Rect(Ofx, Ofy, Ofx + Source->Width, Ofy + Source->Height);
S->CopyRect(Dest, Image->Canvas, Srce);
//Copier les pixels qui appartiennent au cercle
Byte *pS;
Byte *pB;
double r = Width / 2;
int xe;
int ye;
/*
Le raisonnement porte ici sur un cercle.
Ce serait intéressant de développer pour une ellipse (le cercle n'étant qu'un cas
particulier... Mais je ne connais pas la formule !)
*/
for(int y = -r; y < r; y++)
{
ye = y + r;
pS = (Byte*)Source->ScanLine[ye];
pB = (Byte*)Bitmap->ScanLine[ye];
for(int x = -r; x < r; x++)
{
if(hypot(x, y) < r)
{
xe = (x + r) * 3;
pB[xe] = pS[xe]; xe++;
pB[xe] = pS[xe]; xe++;
pB[xe] = pS[xe];
}
}
}
Canvas->Draw(0,0,Bitmap);
}
//------
void __fastcall TCercle::CercleMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
double r = Width / 2;
if(hypot(X - r, Y - r) < r)
{
Movx = X; Movy = Y; Flag = true;
}
}
//------
void __fastcall TCercle::CercleMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(Flag)
{
Ofx = Ofx + Movx - X;
Ofy = Ofy + Movy - Y;
Movx = X; Movy = Y;
Paint();
}
}
//------
void __fastcall TCercle::CercleMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
Flag = false;
} |
Partager