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
This is what things look like before and after my changes:
Here’s the complete code again, which will need the same changes as before if it is to be used in VS 2003 (see the comments on the first post for this).
The DrawToBitmap method is unavailable before .NET V2.
Comment by Larry — 26/8/2005 @ 8:49 pm - 3 years ago
You are right, but that’s easy to work around. Just put the following code in the form class and call the DotNet1DrawToBitmap method instead of the “normal” DrawToBitmap. You don’t need the targetRect in that case.
Nice code. However, windows explorer updates the selected items while the user is selecting them. It would be nice if you could implement that in this control
Comment by Garf — 12/3/2007 @ 10:54 am - 1 year, 6 months ago
Hey Garf - well, this is not a control, it’s just a sample to show you how it can be done. It’s also rather generic - it doesn’t assume what you’re going to do with it. If your own application is something like Windows Explorer, then sure, selection like Explorer makes sense to you. For others it wouldn’t…
That said, it’s really rather easy to do. You just have to have a list of rects for the elements you want to make selectable, and every time the selection rect changes, you check for overlaps between any of the selectable rects in your list and the actual selection rect.
If I ever find the time, I might have a shot at this… but I remember there were still other ideas I was planning to follow up on, so this may not be highest on my priority list. Plus, if I ever have time…
The DrawToBitmap method is unavailable before .NET V2.
Comment by Larry — 26/8/2005 @ 8:49 pm - 3 years ago
You are right, but that’s easy to work around. Just put the following code in the form class and call the DotNet1DrawToBitmap method instead of the “normal” DrawToBitmap. You don’t need the targetRect in that case.
[DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, DrawingOptions drawingOptions); const int WM_PRINT = 0x317; [Flags] enum DrawingOptions { PRF_CHECKVISIBLE = 0x01, PRF_NONCLIENT = 0x02, PRF_CLIENT = 0x04, PRF_ERASEBKGND = 0x08, PRF_CHILDREN = 0x10, PRF_OWNED = 0x20 } void DotNet1DrawToBitmap(Bitmap bitmap) { using (Graphics gr = Graphics.FromImage(bitmap)) { IntPtr hdc = gr.GetHdc( ); SendMessage(Handle, WM_PRINT, hdc, DrawingOptions.PRF_CHILDREN | DrawingOptions.PRF_ERASEBKGND | DrawingOptions.PRF_CLIENT | DrawingOptions.PRF_NONCLIENT); gr.ReleaseHdc(hdc); } }Comment by Oliver Sturm — 27/8/2005 @ 12:27 pm - 3 years ago
Nice code. However, windows explorer updates the selected items while the user is selecting them. It would be nice if you could implement that in this control
Comment by Garf — 12/3/2007 @ 10:54 am - 1 year, 6 months ago
Hey Garf - well, this is not a control, it’s just a sample to show you how it can be done. It’s also rather generic - it doesn’t assume what you’re going to do with it. If your own application is something like Windows Explorer, then sure, selection like Explorer makes sense to you. For others it wouldn’t…
That said, it’s really rather easy to do. You just have to have a list of rects for the elements you want to make selectable, and every time the selection rect changes, you check for overlaps between any of the selectable rects in your list and the actual selection rect.
If I ever find the time, I might have a shot at this… but I remember there were still other ideas I was planning to follow up on, so this may not be highest on my priority list. Plus, if I ever have time…
Comment by Oliver Sturm — 12/3/2007 @ 11:10 am - 1 year, 6 months ago