If you're into creating macros or addons for CorelDRAW, I'm about to blow your mind! New to CorelDRAW X7 Update 1 is the ability to create your own tools in VBA, C# or C++. We now expose a new interface that anyone can implement -- ToolState. We've also implemented many new functions and classes that are specifically geared towards creating tools.
While you can implement new tools in any language that supports COM, in this post, I'll be focusing on C#. I'll share some VBA and C++ samples in a future post.
First, fire up your copy of Visual Studio 2012 or 2013. If you don't have a Visual Studio, fret not, you can grab a copy of Visual Studio Express here. Once you've done so, install the CorelDRAW Tool Addon extension. In Visual Studio, open up Tools->Extensions and Updates, expand the Online tab and search for "CorelDRAW". Select CorelDRAW Tool Addon and install it!
Next, create a new project (File->New Project), and navigate to Installed->Templates->Other Languages->C#, and select CorelDRAW Tool Addon from the list. Give your tool any name (I'm using "MyTool" here) and create your project.
In the menu, select Build->Build Solution (or hit F7). Congrats, you've just created your first tool. Want to try it out? Press F5 to run it. It will launch CorelDRAW and your new tool will be added to the toolbox.
If you can't tell, that's a line and a gear. Sorry, it's hard to fit anything detailed in a 16x16 image.
The sample tool has on screen preview UI, snapping, constraints, a property bar, and everything else you would expect. It is a fully fledged tool. At this point, you're probably thinking that it was too easy.
Let's crack open the code, I'll do my best to explain how it all works.
The project has two .cs files. Addon.cs contains the entry point. This class will be instantiated by CorelDRAW when you start the application, and it's here where you can register your new tool, or data source, or other tasks you'd like to perform on startup.
/// Constructor for the Addon. This is called by CorelDRAW when it is discovered.
public Addon(Application Application)
// Create and register our tool
ToolState toolState = new CGS.MyTool(Application);
Application.RegisterToolState("b17f7ca0-0062-4dd8-adf6-1ea2192a3d35", "MyTool", toolState);
The function RegisterToolState tells that application that the guid "b17f7ca0-0062-4dd8-adf6-1ea2192a3d35" identifies your tool. This guid must be different for every tool (don't worry the Visual Studio extension generates a new one for every project). You can create multiple tools within your project and register them all at this spot. If you create a second tool within the project, you'll need a GUID generator. I use https://gist.github.com/ijprest/3845947 which is an AutoHotKey script, but you can find other GUID generators online (google it).
MyTool.cs contains the guts of the tool. The tool is less than 200 lines long, not too bad. MyTool inherits the ToolState interface, when CorelDRAW is using your tool, it will call into the functions that you implement. For example, when the tool starts, your OnStartState() will be executed, allowing you to set up your tool for use. When you move the mouse across the view, OnMouseMove() will be called, which allows you to update the UI, or collect mouse positions for curve creation.
The code for the sample tool is documented well, please take a few moments to read through the code and the comments to gain a basic understanding of the functions. The pseudo code for the line tool is as follows:
OnLButtonDownLeaveGrace save the left mouse button down positionOnMouseMove if the left mouse is down update the xor'd on screen UI with a line from the saved location to the current positionOnLButtonUp Create a line from the saved position to the current position.
In your project you'll find two xslt files, AppUI.xslt and UserUI.xslt. Xslt is an XML transformation file. It's job is to update the users workspace to include your new tool. You can do much more complex changes to the workspace using XSLT, but in this post I'll be limiting it to adding a new tool Icon and a new Toolbar for your tool. If you're attempting to create any complicated UI, you may want to read up on xslt to better understand it.
AppUI.xslt is responsible for defining new items (e.g. buttons) and adding dockers or dialogs. Anything that isn't customizable. In the sample line tool, it adds one new item.
<!-- C# Test Line Tool -->
A guid (a unique identifier) is used to identify the tool button ("b17f7ca0-0062-4dd8-adf6-1ea2192a3d35"). This guid set for both the item and the identifier. This is also the same guid that we've registered MyTool with the application. This is important as it causes MyTool activate when the button is clicked.
UserUI.xslt is for modifying any UI that is customizable, for example, adding buttons to existing toolbars or menus, or creating new toolbars for the tools.
<modeData guid="fd9a53f7-05cc-4cdc-b2ad-0fd6a7ea3480" captionRef="01965a34-ae38-01a0-4271-cd903b48e518">
The first section defines a new property bar mode for the tool property bar. The guid "fd9a53f7-05cc-4cdc-b2ad-0fd6a7ea3480" is unique for this tool as well, and in MyTool::StartAtState(), you'll find the following line:
currentAttribs.PropertyBarGuid = "fd9a53f7-05cc-4cdc-b2ad-0fd6a7ea3480";
This tells CorelDRAW that we want to use this property bar when the tool starts. The items on this property bar come from the application, mostly from the real line tool. I'll describe how to create your own items in a future post.
The second section adds the item to the toolbox. The code specifically places the new tool after the Smart Fill tool in the tool box.
The three types of resources that we can expose to CorelDRAW are icons, cursors and strings. config.xml ties a guid to a particular resource. In this case we tie "b17f7ca0-0062-4dd8-adf6-1ea2192a3d35" to string "10223" and icon "104. You can find these resources in your project by double-clicking on resources.rct.
A note on adding icons or cursors: I find it easiest to create the icon in an external editor and then use "Add Resource", "Icon", Import, instead of using the built-in editor.
Below is a list of all of the new functions added to CorelDRAW's object model in Update 1. Have fun playing with them!
Things to look out for
For more information on creating custom tools, please see: http://community.coreldraw.com/blogs/insider/archive/2014/06/06/tutorial-make-a-zig-zag-tool-for-coreldraw-x7-update-1.aspx
Your list of utilities looks impressive. I would especially be interested in a replacement for CorelDraw's GetMatrix and SetMatrix routines, which don't behave in a way I can figure out. In particular, I can't get the tx and ty parameters (which are not explained in the help files, but presumably are for translation of a shape) to work as expected.
I followed your instructions and installed Visual Studio. However, a seach failed to show anything related to CorelDraw.
Is your Addon not available any more?
Best regards, Holger Nielsen
How to make it work on Corel Designer? (it doesn't work for me even if I add the Designer.addon in my pluggin folder)
Building a WPF menu in the toolbar, and when floating, it should be an event, but fixed to the top toolbar event is not responding. I don't know why?