One way to make your macro solutions more user-friendly is to optimize them for user interaction, such as mouse input. A macro that captures mouse actions gives users real-time influence on the result of that macro.
The CorelDRAW and Corel DESIGNER object model provides three main ways to receive mouse input from users, as explained in the following topics:
To get the position of a single mouse click, you can use the GetUserClick method of the Document class. This method pauses the macro until the specified period of time elapses, or until the user clicks in the document or presses Escape. Here is a VBA example that uses the Document.GetUserClick method:
Dim doc As Document, retval As Long Dim x As Double, y As Double, shift As Long Set doc = ActiveDocument doc.Unit = cdrCentimeter retval = doc.GetUserClick(x, y, shift, 10, True, cdrCursorPick)
The following parameters for the Document.GetUserClick method are coded into the preceding example:
x
y
shift
10
True
SnapToObjects
cdrCursorPick
One of the following values is returned:
The returned coordinates are relative to the origin of the page and, unless explicitly specified, are in document units.
To get the shapes under the returned click point, you can use the method SelectShapesAtPoint (which is a member of Page), as in the following VBA example:
doc.ActivePage.SelectShapesAtPoint x, y, True
A value of True selects unfilled objects, while False does not select unfilled objects.
False
To get the position of a mouse drag (or an area or rectangle), you can use the GetUserArea method of the Document class. This method pauses the macro until the specified period of time elapses, or until the user clicks, drags, and releases in the document or presses Escape. Here is a VBA example that uses the Document.GetUserArea method:
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 Document.GetUserArea method are coded into the preceding example:
x1
y1
x2
y2
cdrCursorExtPick
In the preceding example, the code ends by selecting the shapes that lie completely within the area by using the Page.SelectShapesFromRectangle method.
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 point and end point of a mouse drag.
When capturing mouse actions, or when developing a complex macro solution, you may want to convert between screen coordinates and document coordinates. This conversion is done with the methods ScreenToDocument and DocumentToScreen of the Window class.
The following VBA example converts a set of screen coordinates into a point in the document that is visible in the active window:
Dim docX As Double, docY As Double ActiveDocument.Unit = cdrMillimeter ActiveWindow.ScreenToDocument 440, 500, docX, docY
The following VBA example returns the screen coordinates of a point in the document as it appears on the screen:
Dim screenX As Long, screenY As Long ActiveDocument.Unit = cdrMillimeter ActiveWindow.DocumentToScreen 40, 60, screenX, screenY
In both examples, the converted coordinates are returned in the last two parameters.
Screen coordinates start from the upper-left corner of the screen, so positive y-values are down the screen, whereas negative y-values are up the screen.
You can test whether a set of coordinates (that is, a point) is inside, outside, or on the outline of a curve by using the Shape.IsOnShape method. For a set of document coordinates, this method returns one of the following:
For example, the following VBA code tests where the point (4, 6) is in relation to the active shape:
Dim onShape As Long ActiveDocument.Unit = cdrInch onShape = ActiveShape.IsOnShape(4, 6)