WPF使用梦想OCX插件入门
本帖最后由 MxDraw 于 2018-11-8 10:11 编辑获取控件下载控件安装包注册控件注册控件之前请先退出杀毒软件(如360杀毒软件),双击运行CopyReleaseFile.bat和CopyReleaseFileX64.bat获取批处理文件,并以管理员身份运行RegistMxDrawX.exe 实现批注册或以管理员方式运行控件 \Bin\vc100\RegistMxDrawX.exe 。 新建一个工程1、运行Visual Studio,选择Visual C#,使用Windows 窗体应用 (.NET Framework) 新建一个工程WindowsFormsApp1。在工具箱中找到MxDrawX52 Control勾选并确定,将MxDrawX52 Control拖放至WinForm窗口,此时WindowsFormsApp1\WindowsFormsApp1\obj\Debug路径下会多出两个文件(AxInterop.MxDrawXLib.dll和Interop.MxDrawXLib.dll)
3、新建一个WPF工程WpfApp1,把AxInterop.MxDrawXLib.dll、Interop.MxDrawXLib.dll和MxDrawX.ocx(64位系统复制MxDraw52\Bin\vc100x64d\MxDrawX.ocx;32位系统复制MxDraw52\Bin\vc100d\MxDrawX.ocx)分别复制到新建的WPF工程的WpfApp1\WpfApp1\bin\Debug和WpfApp1\WpfApp1\obj\Debug路径下。右键添加引用,浏览引用WpfApp1\WpfApp1\bin\Debug路径下的AxInterop.MxDrawXLib.dll、Interop.MxDrawXLib.dll;在.net下分别添加引用WindowsFormsIntegration和System.Windows.Forms。
Wpf工程添加引用完成后如下:
在工具箱中找到MxDrawX52Control勾选并确定.
完成以上操作之后,工具箱空白处右键选择全部显示常规一栏会出现MxDrawX52Control,如下所示:
添加程序集引用:using MxDrawXLib;实例化对象// 创建控件对象AxMxDrawXLib.AxMxDrawX axMxDrawX = new AxMxDrawXLib.AxMxDrawX();public MainWindow(){ InitializeComponent(); //开始初始化控件对象 ((System.ComponentModel.ISupportInitialize)axMxDrawX).BeginInit(); // 创建host 对象 System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost() { Child = axMxDrawX }; // 结束初始化 ((System.ComponentModel.ISupportInitialize)axMxDrawX).EndInit(); // 将对象加入到面板中 mxdraw.Children.Add(host);// mxdraw为Window/Grid名称(name属性) axMxDrawX.ImplementCommandEvent += new AxMxDrawXLib._DMxDrawXEvents_ImplementCommandEventEventHandler(this.axMxDrawX_ImplementCommandEvent);}在这里以打开文件和画直线为例,打开文件实现代码如下:// 打开文件.Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();ofd.Filter = "dwg 文件(*.dwg)|*.dwg";if (ofd.ShowDialog() == true){ axMxDrawX.OpenDwgFile(ofd.FileName);}axMxDrawX.ZoomAll();效果如下所示:绘制直线实现代码如下: //绘制直线private void Drawline(){ MxDrawUtility mxUtility = new MxDrawUtility(); MxDrawPoint point = (MxDrawPoint)(mxUtility.GetPoint(null, "点取第一点:")); if (point == null) { MessageBox.Show("用户取消.."); return; } MxDrawPoint point2 = (MxDrawPoint)(mxUtility.GetPoint(point, "点取第二点:")); if (point2 == null) { MessageBox.Show("用户取消.."); return; } axMxDrawX.DrawLine(point.x, point.y, point2.x, point2.y);}private void Button_Click_1(object sender, RoutedEventArgs e){ axMxDrawX.DoCommand(1);}private void axMxDrawX_ImplementCommandEvent(object sender, AxMxDrawXLib._DMxDrawXEvents_ImplementCommandEventEvent e){ if (e.iCommandId == 1) { // 执行绘直线命令。 Drawline();}}效果如下所示:
页:
[1]