The day before yesterday I was surfing my blogroll and came across an interesting post by Calvin Hsia — “Enable crop and zooming in on your digital photograph display form“. It was using GDI+ in a Visual FoxPro form and, since I am writing a series of articles for FoxTalk on the subject, it caught my eye. One of the things I liked the most about Calvin’s entry was the idea of using a shape in order to create a rubberband selection area; basically a rectangle with a dotted line border that you may have noticed once or twice in various Windows applications (still don’t know what I’m talking about? Go to your desktop and left click and drag a rubberband selection area around some icons — or just look at the screen shot below). I came away from Calvin’s entry thinking he had a great idea, so I’ve implemented my own and expanded on it.

I created a class called “rubberband” that can be added through code to any form during runtime. It facilitates a rubberband selector when the user drags their mouse over the surface of the form. This class has several features that I thought would be useful to a developer.

  • The rubberband is reversible, meaning it can follow the mouse no matter which way the user maybe dragging their selection area.
  • It provides feedback when the selection is started, when the selection area is changed, and also when the selection has been completed.
  • It has an optional feature that will allow you to get a collection of the selected objects as well as a collection of objects that aren’t selected based on the selection area. This is turned on or off via a property of the class named “selectobjects”
  • Left or right mouse button can be specified as the active mouse button that creates the rubberband selection areas.
  • Optional keys (shift, alt, or ctrl) can also be specified as required during the selection (such as, the user must hold the alt key down while dragging with the left mouse button depressed).

The class makes liberal use of Visual FoxPro’s Bindevent function to monitor the user’s activity on the form’s surface. And, the example form I’m including in the download uses bindevents as well to receive feedback from the rubberband class. All-in-all, it turned out to be a pretty workable solution and implementation of Calvin Hsia’s idea.

One of the problems I ran into however, was when the mouse was over a particular object on the form, the form obviously didn’t receive the mouse events… so the rubberband just stops. The solution for this example was to send the mouse events up the chain to the parent form. However, in perhaps a future blog entry I will use the vfpex.fll with its BindEventEx/UnBindEventEx functions (if interested you can read my earlier blog post regarding vfpex.fll) in order to create a mouse hook and keyboard hook in order to facilitate this without having to send mouseevents up to the parent object. I’m not sure if BindEventEx would be needed or not, perhaps Visual FoxPro 9.0’s BindEvent would be enough… never hit something with a sledgehammer when a simple tap will do.

Before I go, I just want to say that I highly recommend Calvin Hsia’s WebLog, there’s a lot of great content there… especially in the archives. Who knows, if enough people take an interest in his blog, he might give us more secrets. 🙂 Now, without any further ado here’s the usual screen shot and download link. (oh, and by the way, that weird thing with the fonts getting bolder as the form repaints definitely needs to be fixed in Visual FoxPro 9.0 — I’ve left it in this example to maybe draw some more attention to it, see the checkboxes as you play with the example).

Download Rubberband Selector Class and Example Form (9 KB approx.)