Advertisement
benshepherd

Create Rectangle

Dec 25th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication2
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         private bool isMouseDown = false;
  15.         private bool isInsideRect = false;
  16.         private bool lastInside = false;
  17.         Point[] rPoints = new Point[2];
  18.         Rectangle rect;
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.             this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  24.         }
  25.  
  26.         private void Form1_MouseDown(object sender, MouseEventArgs e)
  27.         {
  28.             rPoints[0] = rPoints[1] = e.Location;
  29.             isMouseDown = true;
  30.         }
  31.  
  32.         private void Form1_MouseUp(object sender, MouseEventArgs e)
  33.         {
  34.             rPoints[1] = e.Location;
  35.             isMouseDown = false;
  36.         }
  37.  
  38.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  39.         {
  40.             rPoints[1] = e.Location;
  41.             if (isMouseDown)
  42.             {
  43.                 rect = getRectanlge(rPoints[0], rPoints[1]);
  44.             }
  45.  
  46.             isInsideRect = isInRectangle(e.Location, rect);
  47.             DrawRectangles();
  48.             lastInside = isInsideRect;
  49.  
  50.             UpdateInfo(e.Location, rect);
  51.         }
  52.  
  53.         private void DrawRectangles()
  54.         {
  55.             Brush brush = !isInsideRect ? new SolidBrush(Color.LightBlue) : new SolidBrush(Color.Blue);
  56.  
  57.             if ((isInsideRect != lastInside) || isMouseDown)
  58.             {
  59.                 Refresh();
  60.                 using (Graphics g = this.CreateGraphics())
  61.                     g.FillRectangle(brush, rect);
  62.             }
  63.         }
  64.  
  65.         private void UpdateInfo(Point mouse, Rectangle rectangle)
  66.         {
  67.             textBox1.Text = string.Format(
  68.                 "Rectangle Location: {0}x{1}\r\nRetangle Size: {2}x{3}\r\nMouse Location: {4}x{5}\r\nMouse down: {6}\r\nInside Rectangle: {7}",
  69.                 rectangle.X,
  70.                 rectangle.Y,
  71.                 rectangle.Width,
  72.                 rectangle.Height,
  73.                 mouse.X,
  74.                 mouse.Y,
  75.                 isMouseDown,
  76.                 isInsideRect);
  77.         }
  78.  
  79.         public Rectangle getRectanlge(Point StartP, Point EndP)
  80.         {
  81.             return new Rectangle(
  82.                 Math.Min(StartP.X, EndP.X),
  83.                 Math.Min(StartP.Y, EndP.Y),
  84.                 Math.Abs(StartP.X - EndP.X),
  85.                 Math.Abs(StartP.Y - EndP.Y)
  86.                 );
  87.         }
  88.  
  89.         private bool isInRectangle(Point mp, Rectangle rect)
  90.         {
  91.             if (mp.X > rect.X && mp.X < (rect.X + rect.Width)
  92.                 &&
  93.                 mp.Y > rect.Y && mp.Y < (rect.Y + rect.Height))
  94.                 return true;
  95.             else
  96.                 return false;
  97.         }
  98.     }
  99. }
  100. namespace WindowsFormsApplication2
  101. {
  102.     partial class Form1
  103.     {
  104.         /// <summary>
  105.         /// Required designer variable.
  106.         /// </summary>
  107.         private System.ComponentModel.IContainer components = null;
  108.  
  109.         /// <summary>
  110.         /// Clean up any resources being used.
  111.         /// </summary>
  112.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  113.         protected override void Dispose(bool disposing)
  114.         {
  115.             if (disposing && (components != null))
  116.             {
  117.                 components.Dispose();
  118.             }
  119.             base.Dispose(disposing);
  120.         }
  121.  
  122.         #region Windows Form Designer generated code
  123.  
  124.         /// <summary>
  125.         /// Required method for Designer support - do not modify
  126.         /// the contents of this method with the code editor.
  127.         /// </summary>
  128.         private void InitializeComponent()
  129.         {
  130.             this.textBox1 = new System.Windows.Forms.TextBox();
  131.             this.button1 = new System.Windows.Forms.Button();
  132.             this.SuspendLayout();
  133.             //
  134.             // textBox1
  135.             //
  136.             this.textBox1.Location = new System.Drawing.Point(12, 183);
  137.             this.textBox1.Multiline = true;
  138.             this.textBox1.Name = "textBox1";
  139.             this.textBox1.Size = new System.Drawing.Size(280, 77);
  140.             this.textBox1.TabIndex = 0;
  141.             //
  142.             // button1
  143.             //
  144.             this.button1.Location = new System.Drawing.Point(298, 183);
  145.             this.button1.Name = "button1";
  146.             this.button1.Size = new System.Drawing.Size(75, 23);
  147.             this.button1.TabIndex = 1;
  148.             this.button1.Text = "Reset";
  149.             this.button1.UseVisualStyleBackColor = true;
  150.             //
  151.             // Form1
  152.             //
  153.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  154.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  155.             this.ClientSize = new System.Drawing.Size(380, 272);
  156.             this.Controls.Add(this.button1);
  157.             this.Controls.Add(this.textBox1);
  158.             this.Name = "Form1";
  159.             this.Text = "Form1";
  160.             this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
  161.             this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
  162.             this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
  163.             this.ResumeLayout(false);
  164.             this.PerformLayout();
  165.  
  166.         }
  167.  
  168.         #endregion
  169.  
  170.         private System.Windows.Forms.TextBox textBox1;
  171.         private System.Windows.Forms.Button button1;
  172.     }
  173. }
  174. namespace WindowsFormsApplication2
  175. {
  176.     static class Program
  177.     {
  178.         /// <summary>
  179.         /// The main entry point for the application.
  180.         /// </summary>
  181.         [STAThread]
  182.         static void Main()
  183.         {
  184.             Application.EnableVisualStyles();
  185.             Application.SetCompatibleTextRenderingDefault(false);
  186.             Application.Run(new Form1());
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement