Draw Square : Rectangle « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » 2D Graphics » RectangleScreenshots 
Draw Square
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.ComponentModel;
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.ComponentModel.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 Disposebool disposing )
        {
            ifdisposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Disposedisposing );
        }

        #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.ComponentModel.ISupportInitialize)(this.pnlSquares)).BeginInit();
            this.SuspendLayout();
            // 
            // StatusBar1
            // 
            this.StatusBar1.Location = new System.Drawing.Point(0244);
            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(29222);
            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(513);
            this.ClientSize = new System.Drawing.Size(292266);
            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.ComponentModel.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, 2020);
                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());
        }
    }
}


           
       
Related examples in the same category
1.Work with Rectangle type
2.Simplest code to draw a RectangleSimplest code to draw a Rectangle
3.Draw RectangleDraw Rectangle
4.Add size and pointAdd size and point
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.