Create a custom control in Corel Draw X6 using only VSTA

3.3k views Asked by At

I am in the process of creating a plugin (or addin) for CorelDraw X6 using VSTA (Visual Studio for Applications) because I like C# and .NET library. I would like to have a button in CorelDraw toolbar, so when a user clicks this button some action happens, for example, a form is showed. For that I use predefined solution VSTAGlobal, that is there for me when I start CorelDraw. Unfortunately, there is NO official documentation (WTF!!!!!) for VSTA in CorelDraw, instead we have VBA (Visual Basic for Applications) documentation and CorelDraw Object Model. I googled a lot and found a few links: some forum post and YouTube video tutorial. The problem is, both guys there create their CustomControl (a buton for example) and simply build it as *.dll and then use VBA script to add the CustomControl to CorelDraw toolbar like this

Sub addLineWidthControl()
    Call FrameWork.CommandBars("Standard").Controls. '
         AddCustomControl("MyNameSpace.MyCustomControlClass", '
                          "MyCustomControlAssembly.dll")
End Sub

So, my question is: is there any way to do this using only VSTA?

Additional info:

For example, in the default solution VSTAGlobal there is a Main class with [CgsAddInModule] attribute:

[CgsAddInModule]
public partial class Main
{
    private Corel.Interop.VGCore.Application app;

    // some other code here...
}

This class has a constructor (note, default and provided by CorelDraw):

[CgsAddInConstructor]
public Main(object _app)
// this constructor probably gets an instance
// of CorelDraw application object.
{
    app = _app as Corel.Interop.VGCore.Application;
    // will it work if I add some code here?
}

Maybe this is the place where I should add something like this:

app.FrameWork.CommandBars["Standard"]
    .Controls.AddCustomControl("MyCustomControlClass");

I did some experiments with this last line of code. I obtained that the Count of Controls is increasing, but still MyCustomControl does not show up in the toolbar.

1

There are 1 answers

1
Ben Hoffman On BEST ANSWER

There is a way to use only C# (I tested this in Corel X8 but it should work in any version that has VSTA). It is a 2 part process though. First you will need to open Macro Manager in Corel and edit the VSTAGlobal solution. For example, if you want to add a button and a slider, add this method:

[CgsAddInMacro]
        public void Add()
        {
            string controlAssembly = @"C:\VSTS\ScratchProjects\CoreDrawPoC\CoreDrawPoC\bin\Debug\CoreDrawPoC.dll";
            var mySlider = app.FrameWork.CommandBars["Standard"].Controls.AddCustomControl("CoreDrawPoC.MySlider", controlAssembly);
            mySlider.Caption = "Border Sizing Slider Caption";
            mySlider.ToolTipText = "Border Sizing Slider Tooltip";

            var myButton = app.FrameWork.CommandBars["Standard"].Controls.AddCustomControl("CoreDrawPoC.ButtonSample", controlAssembly);
            myButton.Caption = "Rectanlge Selector";
            mySlider.ToolTipText = "Rectanlge Selector Tooltip";

        }

Once you add this code, you will need to add a .dll for a solution to the folder listed above. Open Visual Studio 2015 and create a new project (mine was named CoreDrawPoC). It needs to be a WPF User Control Library project. Create 2 .xaml files with. First will be the slider:

<UserControl x:Class="CoreDrawPoC.MySlider"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CoreDrawPoC"
             mc:Ignorable="d" >
    <Slider x:Name="mySlider" Width="100" Minimum=".5" Maximum="10" TickFrequency=".5" IsSnapToTickEnabled="True" ValueChanged="mySlider_ValueChanged"/>
</UserControl>

Code behind is:

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CoreDrawPoC
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class MySlider : UserControl
    {
        Corel.Interop.VGCore.Application appDraw = null;
        public MySlider()
        {
            InitializeComponent();
        }

        public MySlider(object app)
        {
            InitializeComponent();
            appDraw = (Corel.Interop.VGCore.Application)app;
        }

        private void mySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (appDraw != null && appDraw.ActiveDocument != null && appDraw.ActiveShape != null)
            {
                double width = appDraw.ConvertUnits((double)e.NewValue, Corel.Interop.VGCore.cdrUnit.cdrPoint, Corel.Interop.VGCore.cdrUnit.cdrInch);
                appDraw.ActiveSelectionRange.SetOutlineProperties(width);
            }
        }
    }
}

Then create the button:

<UserControl x:Class="CoreDrawPoC.ButtonSample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CoreDrawPoC"
             mc:Ignorable="d">
    <Button x:Name="buttonSample" Click="buttonSample_Click" Width="24" Height="24" >
        <Button.Template>
            <ControlTemplate>
                <Image Source="C:\CorelIcons\Two.bmp"/>
            </ControlTemplate>
        </Button.Template>
    </Button>

</UserControl>

Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CoreDrawPoC
{
/// <summary>
/// Interaction logic for ButtonSample.xaml
/// </summary>
public partial class ButtonSample : UserControl
{
    Corel.Interop.VGCore.Application appDraw = null;
    public ButtonSample()
    {
        InitializeComponent();
    }
    public ButtonSample(object app)
    {
        InitializeComponent();
        appDraw = (Corel.Interop.VGCore.Application)app;
    }

    private void buttonSample_Click(object sender, RoutedEventArgs e)
    {
        SelectOfType("rectangle");
    }

    private void SelectOfType(string strType)
    {
        string strQuery = null;
        Corel.Interop.VGCore.ShapeRange srGroup = default(Corel.Interop.VGCore.ShapeRange);
        Corel.Interop.VGCore.ShapeRange srTopOnly = default(Corel.Interop.VGCore.ShapeRange);

        strQuery = "@type='" + strType + "'";
        srGroup = appDraw.ActivePage.Shapes.FindShapes("", 0, true, strQuery);
        srTopOnly = appDraw.ActivePage.Shapes.FindShapes("", 0, false, strQuery);
        srTopOnly.CreateSelection();
        appDraw.ActivePage.Shapes.FindShapes("", 0, false, strQuery).CreateSelection();

        //if (srTopOnly.Count == srGroup.Count)
        //{
        //    lblWarning.Visibility = System.Windows.Visibility.Hidden;
        //}
        //else
        //{
        //    lblWarning.Visibility = System.Windows.Visibility.Visible;
        //}
    }

}

}

Once that is done, you will want to compile the code. Also, you will want to create the image C:\CorelIcons\Two.bmp. It is just a 24x24 bitmap that looks however you want it to look. Then compile the project. You will need to this will CorelDraw closed.

Once it compiles successfully, you will want to go and run your VSTA macro to add the button and slider. This macro will only need to be run 1 time! After than, it will connect directly to the code in your dll. If you change anything in the dll and update it while Corel is closed, it changes it. This includes the images.

Finally, I learned how to do this from 2 blog posts and I only changed it to add a button to the Standard command bar and to use purely C#. Those posts are: