Skip to content

Instantly share code, notes, and snippets.

Created October 28, 2016 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6788f9f3095ed40ec06b4f5795c2d1c2 to your computer and use it in GitHub Desktop.
Save anonymous/6788f9f3095ed40ec06b4f5795c2d1c2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DragSelectBox
{
public partial class Form1 : Form
{
public bool leftMouseButtonIsDown;
public Point leftMouseButtonDownLocation = Point.Empty;
public Point currentMouseLocation = Point.Empty;
public Form1()
{
InitializeComponent();
pictureBox1.BackgroundImage = Image.FromFile(@"C:\Users\chris\Desktop\post\backgroundimage.jpg");
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
leftMouseButtonIsDown = true;
leftMouseButtonDownLocation = currentMouseLocation = e.Location;
pictureBox1.Invalidate();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
leftMouseButtonIsDown = false;
pictureBox1.Invalidate();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(leftMouseButtonIsDown)
{
currentMouseLocation = e.Location;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (leftMouseButtonIsDown)
{
Rectangle window = new Rectangle(
Math.Min(leftMouseButtonDownLocation.X, currentMouseLocation.X),
Math.Min(leftMouseButtonDownLocation.Y, currentMouseLocation.Y),
Math.Abs(leftMouseButtonDownLocation.X - currentMouseLocation.X),
Math.Abs(leftMouseButtonDownLocation.Y - currentMouseLocation.Y));
Graphics G = e.Graphics;
Pen pen = new Pen(Color.Red, 2);
G.DrawRectangle(pen, window);
pen.Dispose();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment