Introduction
While working on one of my latest projects, I found myself in need of a simple region selection tool. Pressed with more important functions that my app should provide, I tried searching around the web for a code, just like many times before. After a while, it was clear to me that I’ll have to do the job myself and here it is! You can take the code and implement it however you prefer.
Using the code
It doesn’t really require a lot of code, just some creative thinking to get you on the right track. To give you a basic idea of what it’s all about, here’s a code that allows you to define a selection component into a simple form.
Selection selection = null;
public Form1()
{
InitializeComponent();
selection = new Selection(this);
}
The selection is then implemented in a picture box class with defined methods events regarding specific mouse movements.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
selection.DrawSelection(e.Graphics);
pictureBox1.Refresh();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
selection.mouseDown(e);
pictureBox1.Refresh();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
selection.mouseUp(e);
pictureBox1.Refresh();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
selection.mouseMoved(e);
pictureBox1.Refresh();
}
Of course, this is not all the work you need to do. It’s not all that simple, but it explains the logic behind solving the problem. The form above above just calls for a
Selection.cs file which handles the rest. If you believe this can help you, the full code and the demo are available to the rest of the community here at CodeProject. Play around with it, use it in your apps, or just show off in front of your friends. A lot of the guys here have helped me on more than one occasion, so this is my way of saying: “Thank you!”