Utilisateur anonyme
-
6 déc. 2007 à 10:14
Utilisateur anonyme -
9 déc. 2007 à 02:31
Salut à tous,
Voila je suis entrain de développer un composant de type potentiomètre. Tout fonctionne enfin presque . Ca scintille à mort et quand je met un doublebuffered ca change rien.
procedure Register;
begin
RegisterComponents('MUSIC_PRO', [TPotentio]);
end;
Procedure TPotentio.SetStickColor(Value:TColor);
Begin
fStickColor:=Value;
Self.Repaint;
End;
Procedure TPotentio.SetButtonColor(Value:TColor);
Begin
fButtonColor:=Value;
Self.Repaint;
End;
Procedure TPotentio.SetBorderColor(Value:TColor);
Begin
fBorderColor:=Value;
Self.Repaint;
End;
Procedure TPotentio.Resize;
Begin
Self.Height:=Self.Width;
Self.Repaint;
End;
Procedure TPotentio.SetMin(Value:Integer);
Begin
If (Value<fMax) and (Value<=fPos) Then
fMin:=Value;
fDelta:=(270*(fMin-fPos)/(fMin-fMax)-45)*PI/180;
Self.Repaint;
End;
Procedure TPotentio.SetMax(Value:Integer);
Begin
If (Value>fMin) and (Value>=fPos) Then
fMax:=Value;
fDelta:=(270*(fMin-fPos)/(fMin-fMax)-45)*PI/180;
fPos:=Round(fMin-(fMin-fMax)*((180*fDelta/PI)+45)/270);
Self.Repaint;
End;
Procedure TPotentio.SetPos(Value:Integer);
Begin
If (Value<=fMin) Then fPos:=fMin;
If (Value>=fMax) Then fPos:=FMax;
fPos:=Value;
fDelta:=(270*(fMin-fPos)/(fMin-fMax)-45)*PI/180;
fPos:=Round(fMin-(fMin-fMax)*((180*fDelta/PI)+45)/270);
Self.Repaint;
End;
Procedure TPotentio.Paint(var Message: TWMPaint);
var
Rect:TRect;
R:Extended;
Index:Integer;
begin
if not assigned(Parent) then exit;
with fCanvas Do
begin
Brush.Color := self.Color;
Brush.Style := bsSolid;
FillRect(Self.ClientRect);
control:=self;
Brush.Style:=BsSolid;
Pen.Width:=1;
Pen.Color:=ClBlack;
Brush.Color:=fBorderColor;
With Rect Do
Begin
Left:=(Self.Width Div 7);
Right:=(6*Self.Width Div 7);
Top:=(Self.Height Div 7);
Bottom:=(6*Self.Height Div 7);
End;
Ellipse(Rect);
Brush.Color:=fButtonColor;
With Rect Do
Begin
Left:=Round(2*Self.Width / 7);
Right:=Round(5*Self.Width / 7);
Top:=Round(2*Self.Height / 7);
Bottom:=Round(5*Self.Height / 7);
End;
Ellipse(Rect);
Pen.Color:=Self.fStickColor;
Brush.Color:=Self.fStickColor;
For Index:=-1 To 5 Do
Begin
R:=Round(2*Self.Width / 7);
Rect.Left:=Round(Self.Width Div 2-Round(R*Cos((45*Index)*PI/180))-Self.Width/25);
REct.Top:=Round(Self.Width Div 2-R*Sin((45*Index)*PI/180)-Self.Width/25);
Rect.Right:=Round(Rect.Left+2*Self.Width/25);
Rect.Bottom:=Round(Rect.Top+2*Self.Width/25);
Ellipse(Rect);
End;
R:=(3*Width DIV 14);
With Rect Do
Begin
MoveTo(Width Div 2, Height Div 2);
LineTo(Self.Width Div 2-Round(R*Cos(fDelta)),Round(Self.Width Div 2-R*Sin(fDelta)));
End;
Refresh;
end;
end;
destructor TPotentio.Destroy;
begin
FreeAndNil(fCanvas);
inherited;
end;
procedure TPotentio.MouseDown(Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
Var
I : Extended;
Begin
Inherited;
If Shift=[SSLeft] Then
Begin
If X<>Self.Width Div 2 Then
Begin
I:=(Y-Self.Width Div 2)/(X-Self.Width Div 2);
fDelta:=ArcTan(I);
If x>Self.Width Div 2 Then fDelta:=FDelta+PI;
If fDelta>=(225*PI/180) then fDelta:=(225*PI/180);
If fDelta<=(-45*PI/180) then fDelta:=(-45*PI/180);
fPos:=Round(fMin+(fMax-fMin)*(fDelta*180/PI+45) / 270);
Self.Repaint;
End;
End;
End;
procedure TPotentio.MouseMove(Shift: TShiftState; X,Y: Integer);
Var
I : Extended;
Begin
Inherited;
If Shift=[SSLeft] Then
Begin
If X<>Self.Width Div 2 Then
Begin
I:=(Y-Self.Width Div 2)/(X-Self.Width Div 2);
fDelta:=ArcTan(I);
If x>Self.Width Div 2 Then fDelta:=FDelta+PI;
If fDelta>=(225*PI/180) then fDelta:=(225*PI/180);
If fDelta<=(-45*PI/180) then fDelta:=(-45*PI/180);
fPos:=Round(fMin+(fMax-fMin)*(fDelta*180/PI+45) / 270);
Self.Repaint;
End;
End;
End;
procedure TPotentio.MouseUp(Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
Begin
Inherited;
End;
end.
Si il y en a un qui me dit qu'il y a trop de refresh par seconde, il se prend une fessée directe
procedure Register;
begin
RegisterComponents('MUSIC_PRO', [TPotentio]);
end;
Procedure TPotentio.SetStickColor(Value:TColor);
Begin
if fStickColor=Value then exit;
fStickColor:=Value;
Self.Repaint;
End;
Procedure TPotentio.SetButtonColor(Value:TColor);
Begin
if fButtonColor=Value then exit;
fButtonColor:=Value;
Self.Repaint;
End;
Procedure TPotentio.SetBorderColor(Value:TColor);
Begin
if fBorderColor=Value then exit;
fBorderColor:=Value;
Self.Repaint;
End;
Procedure TPotentio.Resize;
Begin
Self.Height:=Self.Width;
Self.Repaint;
End;
Procedure TPotentio.SetMin(Value:Integer);
Begin
If (Value<fMax) and (Value<=fPos) Then
fMin:=Value;
fDelta:=(270*(fMin-fPos)/(fMin-fMax)-45)*PI/180;
Self.Repaint;
End;
Procedure TPotentio.SetMax(Value:Integer);
Begin
If (Value>fMin) and (Value>=fPos) Then
fMax:=Value;
fDelta:=(270*(fMin-fPos)/(fMin-fMax)-45)*PI/180;
fPos:=Round(fMin-(fMin-fMax)*((180*fDelta/PI)+45)/270);
Self.Repaint;
End;
Procedure TPotentio.SetPos(Value:Integer);
Begin
If (Value<=fMin) Then fPos:=fMin;
If (Value>=fMax) Then fPos:=FMax;
fPos:=Value;
fDelta:=(270*(fMin-fPos)/(fMin-fMax)-45)*PI/180;
fPos:=Round(fMin-(fMin-fMax)*((180*fDelta/PI)+45)/270);
Self.Repaint;
End;
Procedure TPotentio.Paint;
var
Rect:TRect;
R:Extended;
Index:Integer;
begin
if not assigned(Parent) then exit;
with Canvas Do
begin
Brush.Color := self.Color;
Brush.Style := bsSolid;
FillRect(Self.ClientRect);
// control:=self;
Brush.Style:=BsSolid;
Pen.Width:=1;
Pen.Color:=ClBlack;
Brush.Color:=fBorderColor;
With Rect Do
Begin
Left:=(Self.Width Div 7);
Right:=(6*Self.Width Div 7);
Top:=(Self.Height Div 7);
Bottom:=(6*Self.Height Div 7);
End;
Ellipse(Rect);
Brush.Color:=fButtonColor;
With Rect Do
Begin
Left:=Round(2*Self.Width / 7);
Right:=Round(5*Self.Width / 7);
Top:=Round(2*Self.Height / 7);
Bottom:=Round(5*Self.Height / 7);
End;
Ellipse(Rect);
Pen.Color:=Self.fStickColor;
Brush.Color:=Self.fStickColor;
For Index:=-1 To 5 Do
Begin
R:=Round(2*Self.Width / 7);
Rect.Left:=Round(Self.Width Div 2-Round(R*Cos((45*Index)*PI/180))-Self.Width/25);
REct.Top:=Round(Self.Width Div 2-R*Sin((45*Index)*PI/180)-Self.Width/25);
Rect.Right:=Round(Rect.Left+2*Self.Width/25);
Rect.Bottom:=Round(Rect.Top+2*Self.Width/25);
Ellipse(Rect);
End;
R:=(3*Width DIV 14);
With Rect Do
Begin
MoveTo(Width Div 2, Height Div 2);
LineTo(Self.Width Div 2-Round(R*Cos(fDelta)),Round(Self.Width Div 2-R*Sin(fDelta)));
End;
end;
end;
destructor TPotentio.Destroy;
begin
inherited;
end;
procedure TPotentio.MouseDown(Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
Var
I : Extended;
Begin
Inherited;
If Shift=[SSLeft] Then
Begin
If X<>Self.Width Div 2 Then
Begin
I:=(Y-Self.Width Div 2)/(X-Self.Width Div 2);
fDelta:=ArcTan(I);
If x>Self.Width Div 2 Then fDelta:=FDelta+PI;
If fDelta>=(225*PI/180) then fDelta:=(225*PI/180);
If fDelta<=(-45*PI/180) then fDelta:=(-45*PI/180);
fPos:=Round(fMin+(fMax-fMin)*(fDelta*180/PI+45) / 270);
Self.Repaint;
End;
End;
End;
procedure TPotentio.MouseMove(Shift: TShiftState; X,Y: Integer);
Var
I : Extended;
Begin
Inherited;
If Shift=[SSLeft] Then
Begin
If X<>Self.Width Div 2 Then
Begin
I:=(Y-Self.Width Div 2)/(X-Self.Width Div 2);
fDelta:=ArcTan(I);
If x>Self.Width Div 2 Then fDelta:=FDelta+PI;
If fDelta>=(225*PI/180) then fDelta:=(225*PI/180);
If fDelta<=(-45*PI/180) then fDelta:=(-45*PI/180);
fPos:=Round(fMin+(fMax-fMin)*(fDelta*180/PI+45) / 270);
Self.Repaint;
End;
End;
End;
procedure TPotentio.MouseUp(Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
Begin
Inherited;
End;
cs_Kenavo
Messages postés702Date d'inscriptionvendredi 21 mars 2003StatutMembreDernière intervention 1 octobre 20095 7 déc. 2007 à 18:30
Francky,
Pour ce que j'ai compris de la différence entre TGraphicControl et TCustomControl, c'est que ce dernier est fenêtré. C'est à dire qu'il dérive de TWinControl.
Ce qui lui donne, outre une certaine lourdeur qu'il ne doit pas à un abus de Kinder-suicide, des fonctions et comportements d'une fenêtre. C'est là qu'il peut, tout seul, profiter de la propriété DoubleBuffered ou gérer le clavier.
Il est toutefois possible, avec un TGraphicControl, d'obtenir un affichage fluide en faisant ainsi :
- Dessiner dans le canvas d'un bitmap
- Copier le bitmap dans le canvas du controle
- Mettre la propriété DoubleBuffered de la fiche à vrai
La procédure Paint est déclarée ainsi (voire Aide : rubrique "Dessin de l'image du composant"):
procedure Paint; override;
et peut être écrite ainsi :
procedure TPotentio.Paint;
var Rect: TRect;
R: Extended;
Index: Integer;
Bitmap: TBitmap; // déclaration d'un TBitmap pour le dessin begin if not assigned(Parent) then exit;
// création du bitmap Bitmap := TBitmap.Create;
Bitmap.Width := Width;
Bitmap.Height := Height;
//utilisation du canvas du bitmap pour dessiner
with Bitmap.Canvas do begin Brush.Color : = self.Color;
Brush.Style := bsSolid;
FillRect(Self.ClientRect);
// control:=self;
Brush.Style : = BsSolid;
Pen.Width := 1;
//....
// code pour dessiner sur le canvas du bitmap
//....
end ;
Canvas.Draw(0, 0, Bitmap); // copie dans le canvas du controle
Bitmap.Free // Libération du Bitmap end;
Cirec
Messages postés3833Date d'inscriptionvendredi 23 juillet 2004StatutModérateurDernière intervention18 septembre 202250 6 déc. 2007 à 11:37
J'ai vu autre chose qui me "choque" dans ta conception
pourquoi avoir recrée un Canvas alors que GraphicControl en possède un ?
Ensuite il en va de même pour la procedure Paint elle existe aussi il fallait juste faire :
protected
{ Déclarations protégées }
Procedure Paint; Override;