Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅使用VSTA在Corel Draw X6中创建自定义控件_C#_Vba_Coreldraw_Vsta - Fatal编程技术网

C# 仅使用VSTA在Corel Draw X6中创建自定义控件

C# 仅使用VSTA在Corel Draw X6中创建自定义控件,c#,vba,coreldraw,vsta,C#,Vba,Coreldraw,Vsta,我正在使用VSTA(Visual Studio for Applications)为CorelDraw X6创建插件(或加载项),因为我喜欢C和.NET库。我希望在CorelDraw工具栏中有一个按钮,因此当用户单击此按钮时,会发生一些动作,例如,显示一个表单。为此,我使用了预定义的解决方案VSTAGlobal,当我开始CorelDraw时,它就在那里。不幸的是,在CorelDraw中没有关于VSTA的官方文档(WTF!!!),而我们有VBA(Visual Basic for Applicati

我正在使用VSTA(Visual Studio for Applications)为CorelDraw X6创建插件(或加载项),因为我喜欢C.NET库。我希望在CorelDraw工具栏中有一个按钮,因此当用户单击此按钮时,会发生一些动作,例如,显示一个表单。为此,我使用了预定义的解决方案VSTAGlobal,当我开始CorelDraw时,它就在那里。不幸的是,在CorelDraw中没有关于VSTA官方文档(WTF!!!),而我们有VBA(Visual Basic for Applications)文档和CorelDraw对象模型。我在谷歌上搜索了很多,找到了一些链接:和。问题是,这两个家伙都创建了他们的CustomControl(例如buton),并简单地将其构建为*.dll,然后使用VBA脚本将CustomControl添加到CorelDraw工具栏,如下所示

Sub addLineWidthControl()
    Call FrameWork.CommandBars("Standard").Controls. '
         AddCustomControl("MyNameSpace.MyCustomControlClass", '
                          "MyCustomControlAssembly.dll")
End Sub
所以,我的问题是:有没有办法只使用VSTA来实现这一点

其他信息

[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?
}
例如,在默认解决方案VSTAGlobal中,有一个具有[CgsAddInModule]属性的主类:

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

    // some other code here...
}
此类有一个构造函数(注意,默认值,由CorelDraw提供):

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

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

我用最后一行代码做了一些实验。我发现控件的数量在增加,但我的自定义控件仍然没有显示在工具栏中。

有一种方法可以只使用C#(我在Corel X8中测试了这一点,但它应该可以在任何具有VSTA的版本中工作)。但这是一个由两部分组成的过程。首先,您需要在Corel中打开宏管理器并编辑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);
            }
        }
    }
}
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间CoreDrawPoC
{
/// 
///UserControl1.xaml的交互逻辑
/// 
公共部分类MySlider:UserControl
{
Corel.Interop.VGCore.Application-appDraw=null;
公共MySlider()
{
初始化组件();
}
公共MySlider(对象应用程序)
{
初始化组件();
appDraw=(Corel.Interop.VGCore.Application)app;
}
私有void mySlider_值已更改(对象发送方,RoutedPropertyChangedEventArgs e)
{
if(appDraw!=null&&appDraw.ActiveDocument!=null&&appDraw.ActiveShape!=null)
{
double width=appDraw.ConvertUnits((双精度)e.NewValue,Corel.Interop.VGCore.cdrUnit.cdrPoint,Corel.Interop.VGCore.cdrUnit.cdrInch);
appDraw.ActiveSelectionRange.SetOutlineProperties(宽度);
}
}
}
}
然后创建按钮:

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

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间CoreDrawPoC
{
/// 
///ButtonSample.xaml的交互逻辑
/// 
公共部分类按钮示例:UserControl
{
Corel.Interop.VGCore.Application-appDraw=null;
公共按钮示例()
{
初始化组件();
}
公共按钮示例(对象应用程序)
{
初始化组件();
appDraw=(Corel.Interop.VGCore.Application)app;
}
私有无效按钮采样单击(对象发送者,路由目标)
{
选择字体(“矩形”);
}
私有void SelectOfType(字符串strType)
{
字符串strQuery=null;
Corel.Interop.VGCore.ShapeRange srGroup=默认值(Corel.Interop.VGCore.ShapeRange);
Corel.Interop.VGCore.ShapeRange srtopoly=默认值(Corel.Interop.VGCore.ShapeRange);
strQuery=“@type=”+strType+”;
srGroup=appDraw.ActivePage.Shapes.FindShapes(“”,0,true,strQuery);
srtopoly=appDraw.ActivePage.Shapes.FindShapes(“”,0,false,strQuery);
srtopoly.CreateSelection();
appDraw.ActivePage.Shapes.FindShapes(“”,0,false,strQuery).CreateSelection();
//if(srtopoly.Count==srGroup.Count)
//{
//lblWarning.Visibility=System.Windows.Visibility.Hidden;
//}
//否则
//{
//lblWarning.Visibility=System.Windows.Visibility.Visible;
//}
}
}
}

完成后,您将需要编译代码。此外,您还需要创建图像C:\CorelIcons\Two.bmp。它只是一个24x24位图,按您希望的方式显示。然后编译项目。您将需要将此CorelDraw关闭

一旦编译成功,您将需要运行VSTA宏来添加按钮和滑块。此宏只需运行一次!之后,它将直接连接到dll中的代码。如果您更改了dll中的任何内容并在