我正在使用VSTA(适用于Visual Studio的应用程序)为CorelDraw X6创建插件(或插件)的过程,因为我喜欢C#和.NET库。我想在CorelDraw工具栏上有一个按钮,因此当用户单击此按钮时,会发生一些动作,例如,显示一个表单。为此,我使用了预定义的解决方案VSTAGlobal,在我启动CorelDraw时就可以使用它。不幸的是,CorelDraw中没有VSTA的官方文档(WTF !!!!!),相反,我们有VBA(应用程序的Visual Basic)文档和CorelDraw对象模型。我在Google上搜索了很多,发现了一些链接:some forum postYouTube video tutorial。问题是,那里的两个人都创建了他们的CustomControl(例如,一个按钮),并简单地将其构建为* .dll,然后使用VBA脚本将CustomControl添加到CorelDraw工具栏中,如下所示

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


所以,我的问题是:有什么方法可以仅使用VSTA来做到这一点?

附加信息:

例如,在默认解决方案VSTAGlobal中,存在具有[CgsAddInModule]属性的Main类:

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

    // some other code here...
}


此类具有构造函数(请注意,默认值由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?
}


也许这是我应该添加以下内容的地方:

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


我对这最后一行代码做了一些实验。我知道控件的数量在增加,但是MyCustomControl仍未显示在工具栏中。

最佳答案

有一种方法可以仅使用C#(我在Corel X8中对此进行了测试,但是它可以在具有VSTA的任何版本中使用)。这是一个两部分的过程。首先,您需要在Corel中打开Macro Manager并编辑VSTAGlobal解决方案。例如,如果要添加按钮和滑块,请添加以下方法:

[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";

        }


添加此代码后,将需要为上面列出的文件夹添加.dll作为解决方案。打开Visual Studio 2015并创建一个新项目(我的项目名为CoreDrawPoC)。它必须是WPF用户控件库项目。创建2个.xaml文件。首先将是滑块:

<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>


后面的代码是:

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);
            }
        }
    }
}


然后创建按钮:

<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>


后面的代码:

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;
        //}
    }

}


}

完成后,您将要编译代码。另外,您将要创建映像C:\ CorelIcons \ Two.bmp。它只是一个看起来像是24x24的位图,但是您希望它看起来像。然后编译项目。您将需要关闭CorelDraw。

成功编译后,您将需要运行VSTA宏以添加按钮和滑块。该宏只需要运行1次!之后,它将直接连接到您dll中的代码。如果您更改了dll中的任何内容并在Corel关闭时对其进行了更新,它将对其进行更改。这包括图像。

最后,我从2篇博文中学习了如何执行此操作,并且只对其进行了更改,以在“标准”命令栏中添加一个按钮并仅使用C#。这些职位是:


https://forum.oberonplace.com/blog.php?b=165
https://forum.oberonplace.com/blog.php?b=166

关于c# - 仅使用VSTA在Corel Draw X6中创建自定义控件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23733516/

10-11 17:14