有一种只使用 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);
}
}
}
}
然后创建按钮:
<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#。这些帖子是: