2016-06-06 13 views
0

Ich benutze Visual Studio C# Windows Form, ich brauche Hilfe, um einen Kreis mit dem Mausklick zu zeichnen .. ersten Klick gibt mir das Zentrum des Kreises gleich der Cursorposition und der Sekunde Klick gibt mir einen Punkt auf der Grenze des Kreises gleich der zweiten Position des Cursors, der Abstand zwischen den Punkten gibt mir den Radius .. Jetzt habe ich Radius und Punkt ..Ich kann einen Kreis zeichnen ..Die Code funktioniert nicht, weil ich nur eine Position des Cursors egal bekommen kann, wie oft ich die Mauswählen Sie zwei Punkte, um einen Kreis zu zeichnen

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); 
    } 
} 

Antwort

0

Es gibt viele Dinge grundlegend falsch mit diesem Code, hier ist ein komplettes, funktionierendes Beispiel.

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

Danke euch allen es die perfekte Lösung wurde – user6425922

+0

Es gibt ein Problem, wenn ich das Fenster die Größe neu die Kreise sind disapears !! – user6425922

+0

Es tut dies, weil das Formular neu gezeichnet wird, wenn Sie die Größe ändern. Um diese Dinge richtig zu machen, müssen Sie verstehen, wie Grafiken auf Windows-Formularen, Paint-Events usw. funktionieren und dieses Verständnis anwenden. Sehen Sie online nach Tutorials/Ressourcen wie diesem hier: http://www.c-sharpcorner.com/uploadfile/TheButler/the-basics-of-drawing-graphics-onto-windows-forms/ – CamW

1

Ihre lastX und lastY sind lokale Variablen, und klicken Sie initialisieren sie am Anfang der MouseDown- Veranstaltung h Andler. Sie sollten Variablen auf Klassenebene sein und sollten am Ende des MouseDown-Event-Handlers aufgefüllt werden.
Sie sollten auch testen, ob sie bereits einen Wert haben, und nur wenn sie Wert haben, dann zeichnen Sie den Kreis und löschen Sie sie dann (damit der nächste Kreis seinen eigenen Mittelpunkt hat).

Hier ist eine Verbesserung Ihres Codes. Hinweis: Ich habe das Schlüsselwort using mit dem Grafikobjekt und mit dem Stift verwendet - verwenden Sie es jedes Mal, wenn Sie eine Instanz von allem verwenden, das die Schnittstelle IDisposable implementiert.

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); 
    } 
} 

Hinweis: Dieser Code kann noch weiter verbessert werden.

2

Sie müssen auch den ersten Klick speichern, bevor Sie mit den Berechnungen beginnen. Eine Möglichkeit, dies zu tun ist, um eine Klasse zu erstellen, die einfach wirft ein Ereignis jedes zweite Mal passieren Sie es x und y-Koordinaten wie folgt aus:

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; 
      }  
    } 
    } 

Anschließend können Sie in Ihrem Code einfach Ihre Methoden aufrufen:

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); 
    } 

Und in Ihrem Konstruktor:

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); 
    } 
Verwandte Themen