Draw Path and get mouse click : Path « 2D « C# / CSharp Tutorial






Draw Path and get mouse click
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class DrawPathMouseClick : System.Windows.Forms.Form
{
  private System.ComponentModel.Container components = null;

  GraphicsPath myPath = new GraphicsPath();

  private bool isImageClicked = false;
  private int imageClickedIndex;

  public DrawPathMouseClick()
  {
    InitializeComponent();

    myPath.StartFigure();
    myPath.AddLine(new Point(150, 10), new Point(120, 150));
    myPath.AddArc(200, 200, 100, 100, 0, 90);
    Point point1 = new Point(250, 250);
    Point point2 = new Point(350, 275);
    Point point3 = new Point(350, 325);
    Point point4 = new Point(250, 350);
    Point[] points = {point1, point2, point3, point4};
    myPath.AddCurve(points);
    myPath.CloseFigure();
  }

  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(376, 361);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.DrawPathMouseClick_MouseUp);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawPathMouseClick_Paint);
  }

  [STAThread]
  static void Main() 
  {
    Application.Run(new DrawPathMouseClick());
  }

  private void DrawPathMouseClick_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
    Point mousePt = new Point(e.X, e.Y);

    if(myPath.IsVisible(mousePt))
    {
      isImageClicked = true;
      imageClickedIndex = 3;
    }
    else
    {
      isImageClicked = false;
    }
    Invalidate();
  }

  private void DrawPathMouseClick_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;

    g.FillPath(Brushes.RoyalBlue, myPath);

    if(isImageClicked == true)
    {
      Pen outline = new Pen(Color.Purple, 5);

      switch(imageClickedIndex)
      {
        case 3:
          g.DrawPath(outline, myPath);
          break;

        default:
          break;
      }
    }      
  }
}








27.10.Path
27.10.1.Draw Path and get mouse clickDraw Path and get mouse click
27.10.2.GraphicsPath.CloseFigure
27.10.3.Close a figure and Create another figure