Csharp/C Sharp by API/System.Drawing.Drawing2D/GraphicsPath — различия между версиями

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

Текущая версия на 12:10, 26 мая 2010

GraphicsPath.AddArc

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class Form1 : Form
{
        GraphicsPath path;
      public Form1() {
            InitializeComponent();
            
      }
    private void GraphicsPathHit_Paint(object sender, PaintEventArgs e)
    {
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
      path = new GraphicsPath();
      path.StartFigure();
      path.AddArc(10, 10, 100, 100, 20, 50);
      path.AddLine(20, 50, 70, 230);
      path.CloseFigure();
      path.AddEllipse(120, 50, 80, 80);
      e.Graphics.FillPath(Brushes.White, path);
      e.Graphics.DrawPath(Pens.Black, path);
    }
    private void GraphicsPathHit_MouseDown(object sender, MouseEventArgs e)
    {
      if (path.IsVisible(e.X, e.Y))
      {
        MessageBox.Show("You clicked inside the figure.");
      }
    }
    private void InitializeComponent()
    {
      this.SuspendLayout();
      // 
      // GraphicsPathHit
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.Name = "GraphicsPathHit";
      this.Text = "GraphicsPathHit";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.GraphicsPathHit_Paint);
      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GraphicsPathHit_MouseDown);
      this.ResumeLayout(false);
    }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }
}


GraphicsPath.AddBezier

 
  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);
      GraphicsPath gp = new GraphicsPath();
      // Create a figure
      gp.AddLine(10, 10, 10, 50);
      gp.AddBezier(10, 50, 130, 55, 25, 70, 30, 70);
      gp.AddLine(30, 70, 60, 70);
      gp.AddBezier(60, 70, 555, 70, 90, 55, 90, 50);
      gp.AddLine(90, 50, 90, 30);
      // Create another figure
      gp.StartFigure();
      gp.AddLine(60, 110, 40, 160);
      gp.AddLine(40, 160, 60, 180);
      g.DrawPath(Pens.Black, gp);
      gp.Dispose();
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


GraphicsPath.AddEllipse

 
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.AddRectangle(new Rectangle(10, 50, 80, 20));
    gp.AddEllipse(50, 10, 20, 80);
    g.DrawPath(Pens.Black, gp);
    gp.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


GraphicsPath.AddLine

 
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.AddLine(30, 70, 60, 70);
    gp.StartFigure();
    gp.AddLine(60, 110, 40, 160);
    gp.CloseAllFigures();
    g.DrawPath(Pens.Black, gp);
    gp.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


GraphicsPath.AddRectangle

  

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;  // PathGradientBrush 
namespace PathGradient
{
    public class PathGradient : System.Windows.Forms.Form
    {
        private System.ruponentModel.Container components = null;
        public PathGradient()
        {
            this.Text = "PathGradient - CenterPoint";
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(256, 213);
            this.Name = "PathGradient";
            this.Text = "PathGradient";
        }
        static void Main() 
        {
            Application.Run(new PathGradient());
        }
        protected override void OnPaint(PaintEventArgs e)
        {   
            Graphics g = e.Graphics;
            Font f = new Font(new FontFamily("Times New Roman"), 10);
            Brush fb = new SolidBrush(Color.Black);
            GraphicsPath gp;
            PathGradientBrush pGB;  // namespace System.Drawing.Drawing2D;
            Rectangle rec;
            Color cR = Color.Red, cW = Color.White, cY = Color.Yellow;
            int w = 100, h = 70;
            // Left upper rectangle:
            g.DrawString("Center", f, fb, 10, 5);
            gp = new GraphicsPath();
            rec = new Rectangle(10, 20, w, h);
            gp.AddRectangle(rec);
            pGB = new PathGradientBrush(gp);
            pGB.CenterPoint = new Point(10 + w/2, 20 + h/2);
            pGB.CenterColor = cR;
            pGB.SurroundColors = new Color[1]{cW};
            g.FillRectangle(pGB, rec);
            // Right upper rectangle:
            g.DrawString("Center - 2 x 2 Colors", f, fb, w + 20, 5);
            gp = new GraphicsPath();
            rec = new Rectangle(20 + w, 20, w, h);
            gp.AddRectangle(rec);
            pGB = new PathGradientBrush(gp);
            pGB.CenterPoint = new Point(w + 20 + w/2, 20 + h/2);
            pGB.CenterColor = cR;
            pGB.SurroundColors = new Color[4]{cW, cY, cW, cY};
            g.FillRectangle(pGB, rec);
            // Left down rectangle:
            g.DrawString("LefTopCenter", f, fb, 10, h + 25);
            gp = new GraphicsPath();
            rec = new Rectangle(10, h + 40, w, h);
            gp.AddRectangle(rec);
            pGB = new PathGradientBrush(gp);
            pGB.CenterPoint = new Point(10, h + 40);
            pGB.CenterColor = cR;
            pGB.SurroundColors = new Color[1]{cW};
            g.FillRectangle(pGB, rec);
            // Ellipse
            g.DrawString("Top", f, fb, w + 20, h + 25);
            gp = new GraphicsPath();
            rec = new Rectangle(w + 20, h + 40, w, h);
            gp.AddEllipse(rec);
            pGB = new PathGradientBrush(gp);
            pGB.CenterPoint = new Point(w + 20 + w/2, h + 40);
            pGB.CenterColor = cR;
            pGB.SurroundColors = new Color[1]{cW};
            g.FillRectangle(pGB, rec);
            g.Dispose();    
            fb.Dispose();
        }
    }
}


GraphicsPath.AddString

 
  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);
      GraphicsPath gp = new GraphicsPath();
      gp.AddString("www.nfex.ru", new FontFamily("Times New Roman"),
            (int)(FontStyle.Bold | FontStyle.Italic),
            144, new Point(5, 5), StringFormat.GenericTypographic);
      g.SetClip(gp);
      g.DrawImage(new Bitmap("winter.jpg"), this.ClientRectangle);
      gp.Dispose();
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


GraphicsPath.CloseFigure()

 
  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);
      GraphicsPath gp = new GraphicsPath();
      // Create a figure
      gp.AddLine(10, 10, 10, 50);
      gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70);
      gp.AddLine(30, 70, 60, 70);
      gp.CloseFigure();
      gp.StartFigure();
      gp.AddLine(60, 110, 40, 160);
      g.DrawPath(Pens.Black, gp);
      gp.Dispose();
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


GraphicsPath.StartFigure()

 
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.StartFigure();
    gp.AddLine(60, 110, 40, 160);
    g.DrawPath(Pens.Black, gp);
    gp.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


GraphicsPath.Transform

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class FontMenuForm: Form
{
     protected string strText = "Tall in the Center";
     protected Font font = new Font("Times New Roman", 24);
   
     public static void Main()
     {
          Application.Run(new FontMenuForm());
     }
     public FontMenuForm()
     {
          ResizeRedraw = true;
     }
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          GraphicsPath path = new GraphicsPath();
          float fFontSize = PointsToPageUnits(grfx, font);
   
          path.AddString(strText, font.FontFamily, (int) font.Style,
                         fFontSize, new PointF(0, 0), new StringFormat());
   
          RectangleF rectf = path.GetBounds();
   
          path.Transform(new Matrix(1, 0, 0, 1, 
                                    -(rectf.Left + rectf.Right) / 2,
                                    -(rectf.Top + rectf.Bottom) / 2));
          rectf = path.GetBounds();
          PointF[] aptf = path.PathPoints;
   
          for (int i = 0; i < aptf.Length; i++)
               aptf[i].Y *= 2 * (rectf.Width - Math.Abs(aptf[i].X)) / 
                                                                 rectf.Width; 
          path = new GraphicsPath(aptf, path.PathTypes);
   
          grfx.TranslateTransform(cx / 2, cy / 2);
          grfx.FillPath(new SolidBrush(clr), path);
                                            
     }
     public float GetAscent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellAscent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float GetDescent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellDescent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float PointsToPageUnits(Graphics grfx, Font font)
     {
          float fFontSize;
   
          if (grfx.PageUnit == GraphicsUnit.Display)
               fFontSize = 100 * font.SizeInPoints / 72;
          else
               fFontSize = grfx.DpiX * font.SizeInPoints / 72;
   
          return fFontSize;
     }
}


new GraphicsPath()

  
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
  private System.ruponentModel.Container components = null;
  public Form1()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  #region Windows Form Designer generated code
  private void InitializeComponent()
  {
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.ClientSize = new System.Drawing.Size(472, 352);
      this.Name = "Form1";
      this.Text = "Shaped Form Demo";
      this.Load += new System.EventHandler(this.Form1_Load);
    }
  #endregion
  static void Main() 
  {
    Application.Run(new Form1());
  }
    private void Form1_Load(object sender, System.EventArgs e)
    {
      BackColor = Color.Red;
      GraphicsPath path = new GraphicsPath();
      path.AddString("www.nfex.ru",Font.FontFamily, 1, 75, new Point(0, 0), new StringFormat());
      Region = new Region(path);
    }
}