Create a gist now

Instantly share code, notes, and snippets.

anonymous /Form1.cs
Created Oct 28, 2016

What would you like to do?
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