2016-06-06 1 views
0

J'utilise Visual Studio C# Windows forme, j'ai besoin d'aide pour dessiner un cercle en utilisant le clic de la souris .. premier clic me donnera le centre du cercle égal à la position du curseur et la seconde clic me donnera un point sur la bordure du cercle égal à la deuxième position du curseur, la distance entre les points me donnera le rayon .. maintenant j'ai rayon et point ..Je peux dessiner un cercle ..Le le code ne fonctionne pas parce que je ne peux obtenir une position du curseur, peu importe combien de fois je clique la sourissélectionnez deux points pour Dessiner un cercle

private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     int lastX = Cursor.Position.X;//the first click x cursor position 
     int lastY = Cursor.Position.Y;//the first click y cursor position,   

    //is there any way to reuse the Cursor.Position for different point ?? 
    int x = Cursor.Position.X;//the second click x cursor position 
     int y = Cursor.Position.Y;//the second click y cursor position 
     Graphics g; 
     double oradius=Math.Sqrt(((lastX-x)^2) +((lastY-y)^2)); 
     //double newy = Math.Sqrt(lastY); 
     // int newxv = Convert.ToInt32(newx); 
     int radius= Convert.ToInt32(oradius); 
     g = this.CreateGraphics(); 

     Rectangle rectangle = new Rectangle(); 
     PaintEventArgs arg = new PaintEventArgs(g, rectangle); 

     DrawCircle(arg, x, y,radius,radius); 
    } 


    private void DrawCircle(PaintEventArgs e, int x, int y, int width, int height) 
    { 
     System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 3); 
     e.Graphics.DrawEllipse(pen, x - width/2, y - height/2, width, height); 
    } 
} 

Répondre

0

Il y a beaucoup de choses fondamentalement mal avec ce code, voici un exemple de travail complet,.

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private Point clickCurrent = Point.Empty; 
     private Point clickPrev = Point.Empty; 

     private void Form1_MouseDown(object sender, MouseEventArgs e) 
     { 
      clickPrev = clickCurrent; 
      clickCurrent = this.PointToClient(Cursor.Position); 
      if (clickPrev == Point.Empty) return;  
      Graphics g; 
      double oradius = Math.Sqrt((Math.Pow(clickPrev.X - clickCurrent.X, 2)) + (Math.Pow(clickPrev.Y - clickCurrent.Y, 2))); 
      int radius = Convert.ToInt32(oradius); 
      g = this.CreateGraphics(); 
      Rectangle rectangle = new Rectangle(); 
      PaintEventArgs arg = new PaintEventArgs(g, rectangle); 
      DrawCircle(arg, clickPrev.X, clickPrev.Y, radius * 2, radius * 2); 
      clickCurrent = Point.Empty; 
     } 


     private void DrawCircle(PaintEventArgs e, int x, int y, int width, int height) 
     { 
      System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 3); 
      e.Graphics.DrawEllipse(pen, x - width/2, y - height/2, width, height); 
     } 
    } 
+0

Merci à vous tous, il a été parfait réponses – user6425922

+0

Il y a un problème, quand je redimensionnez la fenêtre les cercles sont disapears !! – user6425922

+0

Il le fait parce que le formulaire est redessiné lorsque vous redimensionnez. Pour bien comprendre, vous devez comprendre comment les graphiques sur les formulaires Windows, les événements Paint, etc. fonctionnent et appliquent cette compréhension. Jetez un oeil en ligne pour quelques tutoriels/ressources comme celui-ci: http://www.c-sharpcorner.com/uploadfile/TheButler/the-basics-of-drawing-graphics-onto-windows-forms/ – CamW

1

Votre lastX et lastY sont des variables locales, et vous les initialiser au début de l'événement MouseDown h andler. Ils doivent être des variables de niveau classe et doivent être renseignés à la fin du gestionnaire d'événements MouseDown.
De même, vous devriez tester si elles ont déjà une valeur, et seulement si elles ont de la valeur puis dessinez le cercle puis effacez-les (de sorte que le cercle suivant aura son propre centre).

Voici une amélioration de votre code. Remarque: J'ai utilisé le mot-clé using avec l'objet graphique et avec le stylet - pour l'utiliser chaque fois que vous utilisez une instance de tout ce qui met en œuvre l'interface IDisposable.

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (_lastPosition != Point.Empty) 
    { 
     var currentPosition = Cursor.Position; 
     var oradius = Math.Sqrt(((_lastPosition.X - currentPosition.X)^2) + ((_lastPosition.Y - currentPosition.Y)^2)); 
     var radius = Convert.ToInt32(oradius); 
     using (var g = this.CreateGraphics()) 
     { 
      var arg = new PaintEventArgs(g, new Rectangle()); 
      DrawCircle(arg, currentPosition, radius, radius); 
     } 
     _lastPosition = Point.Empty; 
    } 
    else 
    { 
     _lastPosition = Cursor.Position; 
    } 

} 


private void DrawCircle(PaintEventArgs e, Point position, int width, int height) 
{ 
    using (var pen = new System.Drawing.Pen(System.Drawing.Color.Red, 3)) 
    { 
     e.Graphics.DrawEllipse(pen, position.X - width/2, position.Y - height/2, width, height); 
    } 
} 

Remarque: Ce code peut encore être amélioré.

2

Vous devez également enregistrer le premier clic avant de commencer les calculs. Une façon de le faire est de créer une classe qui jette simplement un événement chaque seconde fois que vous passez coordonnées x et y comme ceci:

public class CircleDrawer 
{ 
    private int _firstX; 
    private int _firstY; 
    private int _secondX; 
    private int _secondY; 

    private bool _isSecondClick; 

    private event EventHandler OnSecondClick; 

    public void RegisterClick(int x, int y) 
    { 
      if(_isSecondClick) 
      { 
       _secondX = x; 
       _secondY = y; 
       if(OnSecondClick != null) 
        OnSecondClick(this, null); 
      } 
      else 
      { 
       _firstX = x; 
       _firstY = y; 
       _isSecondClick = true; 
      }  
    } 
    } 

Vous pouvez alors dans votre code simplement appeler vos méthodes:

private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     int lastX = Cursor.Position.X;//the first click x cursor position 
     int lastY = Cursor.Position.Y;//the first click y cursor position, 

     _circleDrawer.RegisterClick(lastX, lastY); 
    } 

Et dans votre constuctor:

public MyForm() 
{ 
    _circleDrawer = new CircleDrawer(); 
    _circleDrawer.OnSecondClick += DrawCircle(); 
} 

public void DrawCircle() 
{ 
    // Your drawing code 
} 
0
 private int _firstX; 
     private int _firstY; 
     private int _secondX; 
     private int _secondY; 

     private bool _isSecondClick; 


    private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (_isSecondClick) 
     { 
      _secondX = Cursor.Position.X; 
      _secondY = Cursor.Position.Y; 
      var radious1 = Math.Pow(_firstX - _secondX, 2); 
      var radious2 = Math.Pow(_firstY - _secondY, 2); 

      var radious = Math.Sqrt(radious1 + radious2); 
      Graphics g = this.CreateGraphics(); 

      Rectangle rectangle = new Rectangle(); 
      PaintEventArgs arg = new PaintEventArgs(g, rectangle); 
      DrawCircle(arg, _secondX, _secondY, radious, radious); 
     } 
     else 
     { 
      _firstX = Cursor.Position.X; 
      _firstY = Cursor.Position.Y; 
      _isSecondClick = true; 
     }  
    } 

    private void DrawCircle(PaintEventArgs arg, int x, int y, double width, double height) 
    { 
     System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 3); 
     var xL = Convert.ToInt32(x - width/2); 
     var yL = Convert.ToInt32(y - height/2); 
     var hL = Convert.ToInt32(height); 
     var wL = Convert.ToInt32(width); 
     arg.Graphics.DrawEllipse(pen, xL, yL, wL, hL); 
    }