Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- private bool isMouseDown = false;
- private bool isInsideRect = false;
- private bool lastInside = false;
- Point[] rPoints = new Point[2];
- Rectangle rect;
- public Form1()
- {
- InitializeComponent();
- this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
- }
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- {
- rPoints[0] = rPoints[1] = e.Location;
- isMouseDown = true;
- }
- private void Form1_MouseUp(object sender, MouseEventArgs e)
- {
- rPoints[1] = e.Location;
- isMouseDown = false;
- }
- private void Form1_MouseMove(object sender, MouseEventArgs e)
- {
- rPoints[1] = e.Location;
- if (isMouseDown)
- {
- rect = getRectanlge(rPoints[0], rPoints[1]);
- }
- isInsideRect = isInRectangle(e.Location, rect);
- DrawRectangles();
- lastInside = isInsideRect;
- UpdateInfo(e.Location, rect);
- }
- private void DrawRectangles()
- {
- Brush brush = !isInsideRect ? new SolidBrush(Color.LightBlue) : new SolidBrush(Color.Blue);
- if ((isInsideRect != lastInside) || isMouseDown)
- {
- Refresh();
- using (Graphics g = this.CreateGraphics())
- g.FillRectangle(brush, rect);
- }
- }
- private void UpdateInfo(Point mouse, Rectangle rectangle)
- {
- textBox1.Text = string.Format(
- "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}",
- rectangle.X,
- rectangle.Y,
- rectangle.Width,
- rectangle.Height,
- mouse.X,
- mouse.Y,
- isMouseDown,
- isInsideRect);
- }
- public Rectangle getRectanlge(Point StartP, Point EndP)
- {
- return new Rectangle(
- Math.Min(StartP.X, EndP.X),
- Math.Min(StartP.Y, EndP.Y),
- Math.Abs(StartP.X - EndP.X),
- Math.Abs(StartP.Y - EndP.Y)
- );
- }
- private bool isInRectangle(Point mp, Rectangle rect)
- {
- if (mp.X > rect.X && mp.X < (rect.X + rect.Width)
- &&
- mp.Y > rect.Y && mp.Y < (rect.Y + rect.Height))
- return true;
- else
- return false;
- }
- }
- }
- namespace WindowsFormsApplication2
- {
- partial class Form1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (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.textBox1 = new System.Windows.Forms.TextBox();
- this.button1 = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // textBox1
- //
- this.textBox1.Location = new System.Drawing.Point(12, 183);
- this.textBox1.Multiline = true;
- this.textBox1.Name = "textBox1";
- this.textBox1.Size = new System.Drawing.Size(280, 77);
- this.textBox1.TabIndex = 0;
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(298, 183);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 1;
- this.button1.Text = "Reset";
- this.button1.UseVisualStyleBackColor = true;
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(380, 272);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.textBox1);
- this.Name = "Form1";
- this.Text = "Form1";
- this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
- this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
- this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.TextBox textBox1;
- private System.Windows.Forms.Button button1;
- }
- }
- namespace WindowsFormsApplication2
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Form1());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement