Csharp/CSharp Tutorial/2D/Path

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Close a figure and Create another figure

<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form {

   protected override void OnPaint(PaintEventArgs e) {
   Graphics g = e.Graphics;
   g.FillRectangle(Brushes.White, this.ClientRectangle);
   GraphicsPath gp = new GraphicsPath();
   gp.AddLine(10, 10, 10, 50);
   gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70);
   gp.CloseFigure();
   gp.StartFigure();
   gp.AddLine(60, 110, 40, 160);
   g.DrawPath(Pens.Black, gp);
   gp.Dispose();
   }
   public static void Main() {
       Application.Run(new Form1());
   }

}</source>

Draw Path and get mouse click

<source lang="csharp">using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class DrawPathMouseClick : System.Windows.Forms.Form {

 private System.ruponentModel.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;
     }
   }      
 }

}</source>

GraphicsPath.CloseFigure

<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form {

   protected override void OnPaint(PaintEventArgs e) {
   Graphics g = e.Graphics;
   GraphicsPath gp = new GraphicsPath();
   gp.AddLine(10, 10, 110, 15);
   gp.AddLine(110, 15, 100, 96);
   gp.AddLine(100, 96, 15, 110);
   gp.CloseFigure();
   g.FillRectangle(Brushes.White, this.ClientRectangle);
   g.DrawPath(Pens.Black, gp);
   gp.Dispose();
   }
   public static void Main() {
       Application.Run(new Form1());
   }

}</source>