WPF中使用WinForm控件预览DWG文件(学习笔记)

操作环境:XP,C# ,.Net4.0和VS2010中测试


WinForm中使用DWGThumbnail不用这么麻烦,下面讨论的是在WPF中使用,WPF中无法直接引用DWGThumbnail.ocx来操作

新建一个WPF项目,
添加一个WinForm窗口(即Form1.cs),在Form1窗口(这里注意不是MainWindow窗口)的工具箱中点右键“选择项”,
弹出选择工具箱项,切换到COM组件,
点击“浏览”打开你下载的这个控件DWGThumbnail.ocx,在列表中找到AutoCAD DwgThumbnail Control打上勾,确定。
这时你的Form1设计界面的工具箱中会出现AutoCAD DwgThumbnail Control这个控件的图标,拖一个至Form1窗口中,
然后关闭这个Form1窗口即可,这一步骤的目的就是为了在项目引用集里加入AxDWGTHUMBNAILLib这个类库,
自己直接“添加引用”不行,会报错。这时这个Form1.cs使命完成可以删除了,当然你不删也随便你。


1、在WPF中使用WinForm控件,需添加三个引用:
WindowsFormsIntegration.dll(负责整合WPF和Windows)
System.Windows.Forms
System.Drawing
2、在XAML文件中也添加两个引用(粗体部分):


//添加下面这两行
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
//
xmlns="https://www.360docs.net/doc/fe4205980.html,/winfx/2006/xaml/presentation"
xmlns:x="https://www.360docs.net/doc/fe4205980.html,/winfx/2006/xaml"
Title="WPF中使用WinForm控件预览DWG文件" Height="350" Width="525">


//这里随便添个控件占位,不加会出错





3、然后写个方法在事件中调用就行了。
private void DWGview()
{
AxDWGTHUMBNAILLib.AxDwgThumbnail ax = new AxDWGTHUMBNAILLib.AxDwgThumbnail();
ax.Location = new System.Drawing.Point(2, 2); //坐标
ax.Size = new System.Drawing.Size(203, 252); //坐标跟尺寸跟下面的width、height可灵活用,不是非写不可
ddd.Child.Controls.Add(ax);
//ax.Width = 600;
//ax.Height = 300;
ax.HasFocusRect = false; //不要焦点虚线
ax.BackColor = System.Drawing.Color.Black; //背景色
ax.DwgFileName = @"C:\aa.dwg";
ax.Refresh();

//刷新这行也可略,不太清楚不用有什么不同,知道的可告诉我。
}

相关文档
最新文档