In this post I'll describe how to create a tool that allows you to draw a curve that zig zags along the path. You'll learn how to set up a new tool, modify the code to create a curve, set up custom application preferences, and bind those preferences to UI in property bar.

Step 1 - Backup your workspace:

Navigate to C:\Users\<you>\AppData\Roaming\Corel\CorelDRAW Graphics Suite X7\Draw, and zip up the Workspace folder. We'll eventually need to hold F8 to reset your workspace after we make UI changes. After you're done developing your tool, you can restore your old workspaces by unzipping the backup.

Step 2 - Create the project:

Open up Visual Studio. Create a new tool using the CorelDRAW Tool Addon project template (to install the template, see here). Name it "ZigZagTool". At this point, you can build the project and press F5 to test it out if you like, just to make sure everything is working.

Step 3 - Write the code:

The template code is a tool to create a line segment between two points. We want our zig zags to follow a path. Delete 'Point save' and all code that refers to the 'save' variable.

We don't want the mouse to snap when we're drawing, in the function OnMouseSnap only keep 'Handled = isDrawing'.

While the mouse is dragged across the document, we want to keep an array of points of each mouse move position. Add a new variable to the bottom of the class.

PointRange pts; // mouse move points

We must construct the PointRange object. In OnStartState, add:

pts = Application.Math.CreatePointRange();

When the left mouse button is pressed, we want to add the first point to the point range. In OnLButtonDownLeaveGrace, add the following:

pts.AddPoint(pt);

When the mouse moves, we need to add a point, but only if we're drawing.

if (isDrawing)
{
pts.AddPoint(pt);

When the tool is done drawing, or the user hits the escape key, we'd need to clear the previous points. Go to the Reset() function and add the following:

pts.RemoveAll();

When we create the zig zag shape, we need to know the height of each zig-zag, and the distance between each zig-zag. We'll do this by defining two new application preferences. In the constructor, register the two preferences with the application.

This will be stored within the registry and the workspace. When the application restarts, it will use the last value that the user set.

Next we need a function to generate the curve. This function will fit the points to a curve, then divide the curve into equal parts, then create line segments alternate from left to right perpendicular to each of the divided subpaths.

Cool, try it out! In the interest of keeping the code simple, the zig-zags fit to the length of the curve, as a result, the XOR'd preview jumps around while you draw.

Step 4 - Property bar UINow that the tool is working, we need to update the toolbar to allow the user to change the height and distance of the zig-zags. Open up AppUI.xslt and add the following beneath the existing tool item:

The value of first two items refer to the preference we registered in the tool. The last item uses pre-existing bindings to allow us to create a control that changes the outline color.

Next, change the tool's property bar to use these new items. Open up UserUI.xslt and replace the existing property bar ("uiConfig/commandBars/commandBarData[@guid='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx']/toolbar) with:

Make sure you don't change the guid from the one already found in the xslt (3rd line in the above code).

Try it out. Rebuild the project, Hold F8 and launch CorelDRAW. Any time we change UserUI.xslt, the workspace must be reset so that it loads the new UI changes into the current workspace.

Step 5 - Update the Icon and String resources

The last thing we're missing is an icon and string for the toolbox item.

Download the zip file and extract the zigzag.ico file to your hard drive. In Visual Studio, double-click on resources.rct. Right+click on the icon folder and select "Add Resources...", Select "Icon" and choose "Import...". Navigate to the icon file on your hard drive and open it. It will add a new IDI_ICON1 resource. Right+click on IDI_ICON1, select "Properties", then rename the ID to IDI_ZIGZAG. Right+click on IDI_ZIGZAG in the .rct file, and select "Resource Symbols". Take note of the ID number (it should be 105). Close the dialog. Open up config.xml, and change 104 to 105.

Go back to the .rct file, and open the string table. Change the caption to "Zig-Zag Tool".

Build it and run! There you go, you now have a fully functioning tool with custom UI.

The tool can be enhanced further, for an extra challenge, here's some stuff to try:

Hook up the right+mouse drag so that you can move the object while drawing

Allow it to join to existing curves

Allow it to close the curve when you connect

Improve the curve creation to not stretch the curve based on the length of the curve

Add more custom icons and strings for the distance and height controls

Add new modes, like sin or square wave

Make it faster - we don't need to recalculate the entire curve on each mouse move - we can instead update the last zig-zag and keep track of the previous portion of the curve

I've attached the full source. You'll need to change the post build events to point to your own install of CorelDRAW to compile this.