Capturing mouse drags

To get from the user the position of a mouse drag, or an area or rectangle, use the GetUserArea member function of the Document object. This function pauses the macro for a specified period of time or until the user clicks, drags, and releases somewhere in the document, or until the user presses Escape. Here is an example that uses the GetUserArea function:

Dim doc As Document, retval As Long, shift As Long

Dim x1 As Double, y1 As Double, x2 As Double, y2 As Double

Set doc = ActiveDocument doc.Unit = cdrCentimeter retval = doc.GetUserArea(x1, y1, x2, y2, shift, 10, True, cdrCursorExtPick) ActivePage.SelectShapesFromRectangle x1, y1, x2, y2, False The following parameters for the GetUserArea function are coded into this example:

• In the variables x1, y1, x2, and y2, the position of the area is returned as two opposite corners of a rectangle.

• In the parameter shift, the combination of the Shift, Ctrl, and Alt keys that the user holds down when clicking the mouse is returned. The Shift, Ctrl, and Alt keys are assigned values of 1, 2, and 4 (respectively); these values are added together before being returned.

• 10 seconds is given to the user to click somewhere in the document.

• The SnapToObjects parameter is enabled (that is, set to True).

• The cursor icon to be used is defined by the last parameter.

In this example, the code ends by selecting the shapes that lie completely within the area by using the Page object's SelectShapesFromRectangle method.

The returned value is 0, 1, or 2, depending on whether the user successfully completes the area selection, the user cancels by pressing Escape, or the operation times out (respectively).

The returned coordinates of the area are relative to the origin of the page and are in the document's VBA units, so it is often necessary to explicitly set the units in which you want the value to be returned.

This method returns two points that are interpreted as the corners of a rectangle. However, the two points can also be used as the start and end points of a mouse drag.

0 0

Post a comment

  • Receive news updates via email from this site