Csharp/C Sharp by API/System.Drawing/Pen

Материал из .Net Framework эксперт
Версия от 12:11, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

new Pen(Color.Black, 2)

  
    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 Image bMapImageA = new Bitmap("winter.jpg");
    private Image bMapImageB = new Bitmap("winter.jpg");
    private Image bMapImageC = new Bitmap("winter.jpg");
    private Rectangle rectA = new Rectangle(10, 10, 90, 90);
    private Rectangle rectB = new Rectangle(10, 110, 90, 90);
    private Rectangle rectC = new Rectangle(10, 210, 90, 90);
    private bool isImageClicked = false;
    private int imageClicked;
    public Form1()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Form1";
      this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      Point mousePt = new Point(e.X, e.Y);
      if(rectA.Contains(mousePt)) {
        isImageClicked = true;
        imageClicked = 0;
        this.Text = "You clicked image A";
      } else if(rectB.Contains(mousePt)) {
        isImageClicked = true;
        imageClicked = 1;
        this.Text = "You clicked image B";
      } else if(rectC.Contains(mousePt)) {
        isImageClicked = true;
        imageClicked = 2;
        this.Text = "You clicked image C";
      } else {
        isImageClicked = false;
        this.Text = "Images";
      }
      Invalidate();
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.DrawImage(bMapImageA, rectA);
      g.DrawImage(bMapImageB, rectB);
      g.DrawImage(bMapImageC, rectC);
      if(isImageClicked == true) {
        Pen outline = new Pen(Color.Black, 2);
        switch(imageClicked) {
          case 0:
            g.DrawRectangle(outline, rectA);
            break;
          case 1:
            g.DrawRectangle(outline, rectB);
            break;
          case 2:
            g.DrawRectangle(outline, rectC);
            break;
          default:
            break;
        }
      }
    }
  }


new Pen(ForeColor)

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class XMarksTheSpot: Form
{
     public static void Main()
     {
          Application.Run(new XMarksTheSpot());
     }
     public XMarksTheSpot()
     {
          Text = "X Marks The Spot";
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
          Pen      pen  = new Pen(ForeColor);
   
          grfx.DrawLine(pen, 0, 0, 
                             ClientSize.Width - 1, ClientSize.Height - 1);
          grfx.DrawLine(pen, 0, ClientSize.Height - 1, 
                             ClientSize.Width - 1, 0);
     }
}


Pen.DashCap.Triangle

 
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace PenProps_c
{
    public class PenProps : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label lblType;
        private System.ruponentModel.Container components = null;
        public PenProps()
        {
      this.lblType = new System.Windows.Forms.Label();
      this.SuspendLayout();
      this.lblType.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.lblType.Location = new System.Drawing.Point(56, 184);
      this.lblType.Name = "lblType";
      this.lblType.Size = new System.Drawing.Size(328, 16);
      this.lblType.TabIndex = 0;
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(464, 237);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.lblType});
      this.Name = "PenProps";
      this.Text = "PenProps";
      this.Load += new System.EventHandler(this.PenProps_Load);
      this.ResumeLayout(false);
    }
        static void Main() 
        {
            Application.Run(new PenProps());
        }
    private void PenProps_Load(object sender, System.EventArgs e)
    {
    
    }
    protected override void OnPaint(PaintEventArgs e) 
    {
      Graphics G  = e.Graphics;
//      Pen P1=new Pen(Color.Blue, 10);
//
//      G.DrawLine(P1, 20, this.Height/2, this.Width - 20, this.Height/2);
//
//      //Change color and width
//      P1.Color=Color.DarkOrange;
//      P1.Width=5;
//      G.DrawLine(P1, 20, this.Height/2, this.Width - 20, this.Height/2);
//      P1.Width=20;
//
//      //Change brush
//      Pen P2=new Pen(Color.Blue, 10);
//      G.DrawLine(P2, 20, this.Height/3, this.Width - 20, this.Height/3);
//      P2.Brush=new TextureBrush(new Bitmap("colorbars.jpg"));
//      G.DrawLine(P2, 20, this.Height/2, this.Width - 20, this.Height/2);
//      P1.Dispose();
      Pen P2=new Pen(Color.Blue, 10);
      float[] Pts = {3, 1, 2, 5};
      P2.DashStyle=System.Drawing.Drawing2D.DashStyle.Dash;
      P2.DashPattern=Pts;
   //"   P2.DashOffset=40;
      P2.DashCap=System.Drawing.Drawing2D.DashCap.Triangle;
      P2.StartCap = System.Drawing.Drawing2D.LineCap.Round;
      P2.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
      G.DrawLine(P2, 20, this.Height/2, this.Width - 20, this.Height/2);
      //pentype
      G.Clear(Color.Khaki);
      Pen P3=new Pen(Color.Blue, 10);
      P3.Brush=new TextureBrush(new Bitmap("colorbars.jpg"));
      G.DrawLine(P3, 20, this.Height/2, this.Width - 20, this.Height/2);
      lblType.Text = "PenType is " + P3.PenType.ToString();
      //Compoundarray
      P3.Dispose();
      G.Clear(Color.Khaki);
      Single[] lines = {0.0f, 0.1f, 0.9f, 1.0f};
      P3=new Pen(Color.Blue, 20);
      P3.rupoundArray=lines;
      G.DrawLine(P3, 20, this.Height/2, this.Width - 20, this.Height/2);
      //pens class
      P3.Dispose();
      G.Clear(Color.Khaki);
      P3 = Pens.LightSlateGray;
      G.DrawLine(P3, 20, this.Height/2, this.Width - 20, this.Height/2);
      G.DrawLine(Pens.Violet, 20, this.Height/2, 
                this.Width - 20, this.Height/2);
      if (P2 != null)
        P2.Dispose();
    }
    }
}


Pen.DashPattern

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
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);
    Pen p = new Pen(Color.Black, 1);
    float[] f = { 15, 5, 10, 5 };
    p.DashPattern = f;
    g.DrawRectangle(p, 10, 10, 80, 100);
    p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Pen.DashStyle

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) {
        Pen p = new Pen(Color.HotPink, 10);
        p.DashStyle = DashStyle.DashDot;
        Graphics g = this.CreateGraphics();
        g.DrawEllipse(p, 10, 15, 105, 250);
    }
}


Pen.Dispose()

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
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);
        Pen p = new Pen(Color.Black);
        g.DrawLine(p, 0, 0, 100, 100);
        p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Pen.LineJoin

 
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.SmoothingMode = SmoothingMode.AntiAlias;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Pen p = new Pen(Color.Black, 10);
    p.LineJoin = LineJoin.Bevel;
    e.Graphics.DrawRectangle(p, 20, 20, 60, 60);
    p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Pen.StartCap

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Pen p = new Pen(Color.Brown, 15);
        p.StartCap = LineCap.SquareAnchor;
        p.EndCap = LineCap.SquareAnchor;
        g.DrawLine(p, 30, 190, Width - 50, 190);
    }
}