I saw the question coming up in a newsgroup today: how do you draw a selection rubber band just like the one Explorer uses, in C#? The first thing that came to mind was the ControlPaint.DrawReversibleFrame() method, but that’s rather restricted. It’s fine to draw a rectangle around an area to select, but it can’t do anything more than that and it also has problems because it draws over windows that are supposed to be in front of the current app window.
Here are screenshots of a selection made in Explorer and a selection made in my sample program. Nice, huh?
So, here’s the code. I created this in VS 2005 beta 2, but I don’t think I used any .NET 2 specific things - if you find any, let me know. To use the sample, just create a new project and replace your newly created Form1 mainform by this code (and throw away the .Designer.cs and resource files).
/* using System.Collections.Generic; */ using System.Collections;
/* DoubleBuffered = true; */ SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
/* e.Location; */ mouseDownPos = new Point(e.X, e.Y);
And you have to add a Program.cs to the project:
using System;
using System.Windows.Forms;
namespace SelectionRectTest
{
class Program
{
[STAThread]
static public void Main(string[] args) {
Application.Run(new Form1());
}
}
}
Comment by Larry — 26/8/2005 @ 3:57 pm - 3 years, 1 month ago
Larry, you are of course correct. It’s unbelievable how many details there are that are easily overlooked after using VS 2005 for a long while Thanks for pointing this out.
Also, you must add the following line as the first line of the Form1 constructor:
InitializeComponent();
Probably caused by the partial classes in VS 2005.
Comment by Larry — 26/8/2005 @ 4:33 pm - 3 years, 1 month ago
No, that’s not right. You don’t need that because the class is a complete derived form in it’s own right. You need that only if you are going to design the class in VS, which you might do in a real-world app, but which is not intended for the sample.
[…] I heard criticism about my first post on the topic: if a control was on the form, apart from the background drawing, the rubber rectangle would appear behind the control, not in front of it. Of course there are several ways to change this, I decided to implement one of them just for fun […]
The following lines needed changing for VS 2003:
And you have to add a Program.cs to the project:
using System; using System.Windows.Forms; namespace SelectionRectTest { class Program { [STAThread] static public void Main(string[] args) { Application.Run(new Form1()); } } }Comment by Larry — 26/8/2005 @ 3:57 pm - 3 years, 1 month ago
Larry, you are of course correct. It’s unbelievable how many details there are that are easily overlooked after using VS 2005 for a long while
Thanks for pointing this out.
Comment by Oliver Sturm — 26/8/2005 @ 4:22 pm - 3 years, 1 month ago
Also, you must add the following line as the first line of the Form1 constructor:
InitializeComponent();
Probably caused by the partial classes in VS 2005.
Comment by Larry — 26/8/2005 @ 4:33 pm - 3 years, 1 month ago
No, that’s not right. You don’t need that because the class is a complete derived form in it’s own right. You need that only if you are going to design the class in VS, which you might do in a real-world app, but which is not intended for the sample.
Comment by Oliver Sturm — 26/8/2005 @ 4:36 pm - 3 years, 1 month ago
But, that’s exactly what I was doing - designing it in VS 2003.
I added a StatusBar to show the selection rectangle - live!
Without the InitializeComponent(), my new StatusBar was not being instantiated.
Thanks for the sample.
Comment by Larry — 26/8/2005 @ 4:41 pm - 3 years, 1 month ago
True. I meant that you don’t need the InitializeComponent to get my sample to work. I don’t care what else you do with your own copy of it
Comment by Oliver Sturm — 26/8/2005 @ 4:42 pm - 3 years, 1 month ago
[…] I heard criticism about my first post on the topic: if a control was on the form, apart from the background drawing, the rubber rectangle would appear behind the control, not in front of it. Of course there are several ways to change this, I decided to implement one of them just for fun […]
Pingback by Oliver Sturm’s weblog - Drawing a selection rubber band like Explorer - improved — 26/8/2005 @ 7:22 pm - 3 years, 1 month ago