Dragging and Dropping


Dragging and dropping is an important feature that you can include in your software, although, frankly, it is not a required feature. But when done properly, it can be a handy feature that adds a professional touch. But before I get started, let me say this:

RULE

Whatever drag-and-drop feature you include, make sure you also make the same feature available through some means other than drag and drop!

In the following two sections I cover drag-and-drop operations.

Including a Drag-and-Drop Feature

I’m not going to go into the details of what exactly drag and drop is, because you, as a programmer, already know that. However, here are some tips to follow when implementing a drag-and-drop system:

  • Change the mouse pointer to represent that a drag-and-drop action is in progress.

  • Change the mouse pointer to indicate whether a drop is allowed or not allowed.

  • Enable the Esc key to cancel the drag-and-drop action.

Fortunately, however, if you’re using Windows and you use one of the two built-in technologies (Windows Explorer and OLE/COM, which I discuss in the following section, “Drag-and-Drop Technologies in Windows”), then you don’t need to change the mouse pointer, because Windows already does it for you. And the same is true for your handling of the Esc key. In other words, when you use these two technologies, some of the work is already done for you!

You might want to allow users to drag items within your program. For example, in Microsoft Word, you can select some text and then use the mouse to drag the text to a different position within your document. Or you might want to be able to drag data items, such as those found in a listbox.

One issue that comes up with drag and drop is that of scrolling. Microsoft has some very strange (and somewhat annoying) guidelines they seem to follow with scrolling in their own Office programs. For example, if you highlight some text in Microsoft Word and drag to the bottom of the editor window, the editor window will scroll. It used to be that this window would scroll way too fast to be practical, although you could apparently slow down the scrolling by not dragging too far out of the window. As of Word 2002, the speed seems to be slower and more reasonable, but the moment you move the mouse outside the editor window, the scrolling stops and the pointer turns into a circle with a line through it, indicating that a drop is not allowed.

Think through the scrolling issue carefully if you have a need for your program to scroll on a drag-and-drop operation. Make sure the scrolling doesn’t happen at an insane speed, and make sure your users are able to drop where they want to drop.

You might also want to be able to drag between programs. For this, you will need to ask some questions:

  • Can users initiate drags from data within your program?

  • Can your program receive drops from other programs?

The choice is yours on each of these issues, but whatever you choose, make sure you document it so your users are aware of the features. And if you’re programming for Windows, I provide you with some insight into these issues in the following section, “Drag-and-Drop Technologies in Windows.”

Drag-and-Drop Technologies in Windows

If you’re programming for Windows, three different types of drag-and-drop technologies are available to you, each with its benefits. Here they are:

  • Manual drag and drop

  • Windows Explorer drag and drop

  • OLE/COM drag and drop

Manual drag and drop simply refers to how you can write your own drag-and-drop system by detecting the mouse-down event, setting a Boolean variable to true, then detecting the mouse-move event, and checking to see if the Boolean variable is true; if so, a drag operation is under way. Then, in response to a mouse-up event, you can again check to see if the Boolean variable is true; if so, the drop operation has taken place. You can also store the beginning coordinates during the mouse-down operation and check the end coordinates in the mouse-up operation. (To make this process work effectively, however, you need to capture the mouse. Different languages and GUI tools have different ways of doing this, but at heart any of these libraries simply calls the SetCapture API function.) Note that if you use Delphi or C++Builder, and you use the drag-and-drop functionality in the controls, the underlying code is implementing a drag-and-drop approach using this method I just described.

The second method lets you receive files. This means that if a Windows Explorer window is open (remember, that’s Windows Explorer and not Internet Explorer), users can drag filenames from the Explorer window onto your application. Your application can then detect this drop operation and obtain the name of the file. When this happens, your program doesn’t actually receive the file; all your program receives is the name of the file (or the list of filenames if the user first selected multiple files in Windows Explorer). Most GUI tools have different ways of allowing you to include this feature, but if they don’t, you can easily program the feature yourself by calling into the Windows API. All you do is call the DragAcceptFiles API function, passing the handle for your window that will accept the files, along with a True value. Then, when the user drops a file on your window, you will receive a WM_DROPFILES message.

Using this technique, you can actually do some pretty interesting things. For example, you might not want to accept files on your entire window. Instead, you might want to accept files only in a certain area. To do this, you simply create a child window that is nothing more than a rectangular area (or whatever you want, really), and you then register the child window’s handle in the DragAcceptFiles call instead of your main window. The only catch is that you have to have access to the child window’s message handler (or window procedure). That means writing your own custom control and window class (or, heaven forbid, subclassing a standard control, which is a throwback to the old days of Windows programming).

The third technology for drag and drop is using the OLE/COM system. If you try out the previous technology using the DragAcceptFiles function, you may be disappointed when you figure out that users can’t drag items from Internet Explorer. For example, you’ve seen this little icon in the address bar of Internet Explorer:

For many applications (including all of Microsoft Office), you can drag that little icon from IE into the application. The different applications will then handle the drop in different ways. If you drag the icon into Microsoft Word, you will see the address appear in Word, and the address will be clickable. (You have to hold down the Ctrl key, however, to click a link in Word.)

In order to provide for this kind of functionality, you need to make use of the drag-and-drop features of the OLE/COM system in Windows. This is, frankly, a rather complex process if you’re programming in straight C++ and beyond what I have room to show you here. (It’s easier in other languages such as Visual Basic where you can find controls that implement the behavior for you.) If you’re interested in programming this sort of thing in C++, first study a bit of OLE/COM programming, and then do a web search for “OLE drag drop.” I’ve found several pages that talk about it.




Designing Highly Useable Software
Designing Highly Useable Software
ISBN: 0782143016
EAN: 2147483647
Year: 2003
Pages: 114

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net