Csharp/C Sharp/2D Graphics/Rectangle — различия между версиями

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

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

Add size and point

using System;
using System.Drawing;
class Class1 {
    static void Main(string[] args) {
        Point topLeft = new Point(10,10);
        Size rectangleSize = new Size(50,50);
        Point bottomRight = topLeft + rectangleSize;
        Console.WriteLine("topLeft = " + topLeft);
        Console.WriteLine("bottomRight = " + bottomRight);
        Console.WriteLine("Size = " + rectangleSize);
    }
}


Draw Rectangle

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace AllCornerRect
{
    public class AllCornerRect : System.Windows.Forms.Form
    {
    #region Class Local Variables
    RealRect MyRect;
    #endregion

    private System.ruponentModel.Container components = null;
    public AllCornerRect() {
        InitializeComponent();
        this.SetStyle( ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle( ControlStyles.DoubleBuffer, true);
        this.MouseDown  += new MouseEventHandler(this.M_down);
        this.MouseUp    += new MouseEventHandler(this.M_up);
        this.MouseMove  += new MouseEventHandler(this.M_move);
        MyRect = new RealRect();
    }
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
        private void InitializeComponent()
        {
      // 
      // AllCornerRect
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(320, 301);
      this.MaximizeBox = false;
      this.MinimizeBox = false;
      this.Name = "AllCornerRect";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "AllCornerRect";
      this.Load += new System.EventHandler(this.AllCornerRect_Load);
    }
        #endregion
        [STAThread]
        static void Main() 
        {
            Application.Run(new AllCornerRect());
        }
    private void AllCornerRect_Load(object sender, System.EventArgs e)
    {
    }
    protected override void OnPaint(PaintEventArgs e)
    {
      base.OnPaint(e);
      Graphics G = e.Graphics;
      G.DrawRectangle(Pens.Red, MyRect.Rect);
    }
    #region Squeek
    private void M_up(object sender, MouseEventArgs e)
    {
      Invalidate();
    }
    private void M_down(object sender, MouseEventArgs e)
    {
      if (e.Button != MouseButtons.Left)
        return;
      MyRect = new RealRect(e.X, e.Y);
    }
    private void M_move(object sender, MouseEventArgs e)
    {
      if (e.Button != MouseButtons.Left)
        return;
      MyRect.EndX = e.X;
      MyRect.EndY = e.Y;
      Invalidate();
    }
    #endregion
    }
    public class RealRect
    {
    #region Class Local Variables
    private Point mStart;
    private Point mEnd;
    private Point mRealStart;
    private Point mRealEnd;
    private Size  mRealSize;
    private Rectangle mRect;
    #endregion

    public RealRect(int X, int Y)
        {
      mStart        = Point.Empty;
      mEnd          = Point.Empty;
      mRealStart    = Point.Empty;
      mRealEnd      = Point.Empty;
      mRealSize     = Size.Empty;
      mStart.X      = X;
      mStart.Y      = Y;
      mRealStart.X  = X;
      mRealStart.Y  = Y;
      mRect = Rectangle.Empty;
    }
    public RealRect()
    {
      mStart        = Point.Empty;
      mEnd          = Point.Empty;
      mRealStart    = Point.Empty;
      mRealEnd      = Point.Empty;
      mRealSize     = Size.Empty;
      mStart.X      = 0;
      mStart.Y      = 0;
      mRealStart.X  = 0;
      mRealStart.Y  = 0;
      mRect = Rectangle.Empty;
    }
    /// <summary>
    /// Ending X Value of rectangle
    /// </summary>
    public int EndX
    {
      set{ mEnd.X = value; }
    }
    /// <summary>
    /// Ending Y Value of rectangle
    /// </summary>
    public int EndY
    {
      set{ mEnd.Y = value; }
    }
    /// <summary>
    /// Get the corrected rectangle
    /// </summary>
    public Rectangle Rect
    {
      get
      { 
        MakeReal();
        mRect.Location = mRealStart;
        mRect.Size = mRealSize;
        return mRect; 
      }
    }
    private void MakeReal()
    {
      //Started top left, ended bottom right
      if (mEnd.X > mStart.X && mEnd.Y > mStart.Y)
      {
        mRealStart = mStart;
        mRealEnd = mEnd;
        mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y);
        return;
      }
      //Started bottom right, ended top left
      if (mEnd.X < mStart.X && mEnd.Y < mStart.Y)
      {
        mRealEnd = mStart;
        mRealStart = mEnd;
        mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y);
        return;
      }
      //Started top right left, ended bottom left
      if (mEnd.X < mStart.X && mEnd.Y > mStart.Y)
      {
        mRealStart.X = mEnd.X;
        mRealStart.Y = mStart.Y;
        mRealEnd.X   = mStart.X;
        mRealEnd.Y   = mEnd.Y;
        mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y);
        return;
      }
      //Started bottom left, ended top right
      if (mEnd.X > mStart.X && mEnd.Y < mStart.Y)
      {
        mRealStart.X = mStart.X;
        mRealStart.Y = mEnd.Y;
        mRealEnd.X   = mEnd.X;
        mRealEnd.Y   = mStart.Y;
        mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y);
        return;
      }
    }
  }
}


Draw Square

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
namespace GDI_Basics
{
    /// <summary>
    /// Summary description for DrawSquare.
    /// </summary>
    public class DrawSquare : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.StatusBar StatusBar1;
        internal System.Windows.Forms.StatusBarPanel pnlSquares;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public DrawSquare()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.StatusBar1 = new System.Windows.Forms.StatusBar();
            this.pnlSquares = new System.Windows.Forms.StatusBarPanel();
            ((System.ruponentModel.ISupportInitialize)(this.pnlSquares)).BeginInit();
            this.SuspendLayout();
            // 
            // StatusBar1
            // 
            this.StatusBar1.Location = new System.Drawing.Point(0, 244);
            this.StatusBar1.Name = "StatusBar1";
            this.StatusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                                                                                          this.pnlSquares});
            this.StatusBar1.ShowPanels = true;
            this.StatusBar1.Size = new System.Drawing.Size(292, 22);
            this.StatusBar1.SizingGrip = false;
            this.StatusBar1.TabIndex = 1;
            this.StatusBar1.Text = "statusBar1";
            // 
            // pnlSquares
            // 
            this.pnlSquares.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
            this.pnlSquares.Width = 292;
            // 
            // DrawSquare
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.StatusBar1});
            this.Name = "DrawSquare";
            this.Text = "DrawSquare";
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawSquare_MouseDown);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawSquare_Paint);
            ((System.ruponentModel.ISupportInitialize)(this.pnlSquares)).EndInit();
            this.ResumeLayout(false);
        }
        #endregion

        // Store the squares that are painted on the form.
        ArrayList squares = new ArrayList();
        private void DrawSquare_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // Add a square and update the screen.
                Rectangle square = new Rectangle(e.X, e.Y, 20, 20);
                squares.Add(square);
                this.Invalidate(square);
            }
            else if (e.Button == MouseButtons.Right)
            {
                // Search  for the clicked square.
                int squareNumber = 0;
                foreach (Rectangle square in squares)
                {
                    squareNumber++;
                    if (square.Contains(e.X, e.Y))
                    {
                        MessageBox.Show("Point inside square #" +
                            squareNumber.ToString());
                    }
                }
            }
        }
        private void DrawSquare_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Pen drawingPen = new Pen(Color.Red, 10);
            foreach (Rectangle square in squares)
            {
                e.Graphics.DrawRectangle(drawingPen, square);
            }
            pnlSquares.Text = " " + squares.Count.ToString() + " squares";
        }

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


Simplest code to draw a Rectangle

  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
  {
    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);
      Pen p = new Pen(Color.Black);
      g.DrawRectangle(p, 3, 3, 8, 7);
      p.Dispose();

    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


Work with Rectangle type

 
using System;
using System.Drawing;

class Program {
    static void Main(string[] args) {
        Rectangle r1 = new Rectangle(0, 0, 100, 100);
        Point pt3 = new Point(101, 101);
        if (r1.Contains(pt3))
            Console.WriteLine("Point is within the rect!");
        else
            Console.WriteLine("Point is not within the rect!");
        // Now place point in rectangle"s area.
        pt3.X = 50;
        pt3.Y = 30;
        if (r1.Contains(pt3))
            Console.WriteLine("Point is within the rect!");
        else
            Console.WriteLine("Point is not within the rect!");
    }
}