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
| __fastcall jBanditBoyButton::jBanditBoyButton(TComponent *Owner, TRect R)
: TCustomControl(Owner)
{
if(Owner->InheritsFrom(__classid(TWinControl))) Parent = (TWinControl*)Owner;
BoundsRect = R;
OnMouseDown = WhenMouseDown;
}
__fastcall jBanditBoyButton::~jBanditBoyButton()
{
}
void __fastcall jBanditBoyButton::Paint()
{
//La couleur de fond est donnée par Parent->Brush->Color
//Si on ne devait utiliser que 3 boutons sur les 4, Outside
//permettrait de simuler un clic dans le vide...
Outside = Parent->Brush->Color;
//Brush, Pen et on efface avec la couleur du parent tout en
//dessinant un cadre, puisque toute la surface est prise
Canvas->Brush->Color = Outside;
Canvas->Brush->Style = bsSolid;
Canvas->Pen->Color = clBlack;
Canvas->Pen->Style = psSolid;
Canvas->Pen->Mode = pmCopy;
Canvas->Rectangle(0,0,Width, Height);
//Calcul des 8 points
//Points[0] correspond au point le plus à droite les autres points en suivant.
//J'ai opté pour ce type de calcul afin de garder une apparence similaire
//quelles que soient les dimensions (dans les limites du raisonnable)
Points[0] = Point(Width - 1, Height * 9 / 16);
Points[1] = Point(Width * 12 / 24, Height * 9 / 16);
Points[2] = Point(Width * 9 / 24, Height * 3 / 16);
Points[3] = Point(Width * 6 / 24, Height * 3 / 16);
Points[4] = Point(Width * 4 / 24, Height * 7 / 16);
Points[5] = Point(Width * 4 / 24, Height * 13 / 16);
Points[6] = Point(Width * 8 / 24, Height * 13 / 16);
Points[7] = Point(Width * 10 / 24, Height - 1);
//Dessin des lignes
Canvas->Polyline(Points, 7);
Canvas->MoveTo(Points[3].x, 0); Canvas->LineTo(Points[3].x, Points[3].y);
Canvas->MoveTo(0, Points[4].y); Canvas->LineTo(Points[4].x, Points[4].y);
//Remplissage des zones
TColor color = (TColor)0x00000090;
if(ActiveButton == red) color = clRed;
Canvas->Brush->Color = color;
Canvas->FloodFill(1,1, Outside, fsSurface);
color = clNavy;
if(ActiveButton == blue) color = (TColor)clAqua;
Canvas->Brush->Color = color;
Canvas->FloodFill(1, Height - 2, Outside, fsSurface);
color = (TColor)0x00008A8A;
if(ActiveButton == yellow) color = clYellow;
Canvas->Brush->Color = color;
Canvas->FloodFill(Width-2, Height-2, Outside, fsSurface);
color = clGreen;
if(ActiveButton == green) color = clLime;
Canvas->Brush->Color = color;
Canvas->FloodFill( Width - 2, 1, Outside, fsSurface);
}
void __fastcall jBanditBoyButton::SetActiveButton(int Value)
{
if(Value <= green)
{
FActiveButton = Value;
Repaint();
}
}
void __fastcall jBanditBoyButton::WhenMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
//Les boutons fonctionnent en mode exclusion mutuelle
//La dernière partie de l'arbre logique (facultatif) permet de revenir à état
//initial, c'est à dire aucun bouton sélectionné.
//La sélection est représentée par une teinte plus vive (approximativement)
TColor Pixel = Canvas->Pixels[X][Y];
if(Pixel == (TColor)0x00000090)
{
ActiveButton = red;
}
else
{
if(Pixel == clNavy)
{
ActiveButton = blue;
}
else
{
if(Pixel == (TColor)0x00008A8A)
{
ActiveButton = yellow;
}
else
{
if(Pixel == clGreen)
{
ActiveButton = green;
}
//cette partie permet d'annuler la sélection
//elle n'arrive que lorsque l'on clique sur un
//boutton déjà sélectionné
else
{
ActiveButton = none;
}
}
}
}
} |