|
转自:明经CAD
在http://forums.autodesk.com/t5/NET/WPF-Controls-In-PaletteSet/m-p/2311530/highlight/true#M11201文中,mikko给出了一个非常有用的入门范例
http://forums.autodesk.com/autodesk/attachments/autodesk/152/11209/1/WPFacad.zip 。这个范例文件,非常好地给出了用VB.NET开发AUTOCAD WPF程序的三种方法
(1)采用PaletteSet面板工具内嵌wpf用户控件
(2)采用建立wpf控件转为wpf应用程序的方法
(3)利用.net语言直接生成wpf窗口
以下逐一进行学习讨论,并准备将其改造为c#代码方便后续的学习,并将期望将其拓展到SAP2000的开发中(经测试,SAP2000似乎还不支持WPF开发),毕竟这两种都是采用了类库开发模式,如何启动WPF窗口是一个值得学习的问题。
(1)采用PaletteSet面板工具内嵌wpf用户控件
说明:
对于这种wpf内嵌于ps面板中,Autocad官方有一个例子在此处
http://download.autodesk.com/media/adn/ACAD_Using_WPF_in_your_applications_02June2009.zip
压缩文件中的MyPaletteWebcast文件夹就是一个面板内嵌wpf窗口的例子
Kean也写过类似的例子
http://through-the-interface.typepad.com/through_the_interface/2009/08/hosting-wpf-content-inside-an-autocad-palette.html
还有一个例子是mjtd的翔麟曾经给过更加复杂的例子,用wpf Autocad查询一下google,你会发现第二个帖子就是了
http://bbs.mjtd.com/thread-80641-1-1.html
翔麟的.net CAD开发技术是很高的,特别是国内的Autocad和wpf的研究,现在能查到的网上资料,就大部分都是他的。
过程,首先,创建一个VB.NET的类库
接着,添加 acmgd.dll和acdbmgd.dll的引用,并在项目属性中,将这两个的复制本地设为false(C#则可以直接在解决方案资源管理器中修改)。
为了WPF的应用,添加另外几个.net的引用PresentationCore, PresentationFramework, System.Windows.Form, WindowsBase, WindowsFormsIntegration
接下来,创建一个wpf控件,过程可以如下 在菜单-项目-添加用户控件 中找到wpf控件添加之,visual studio2008和2010的位置有一点点不同,并将这个用户控件重命名为 XamlCtrl.xaml
在这个用户控件的xaml中,贴入如下代码
- <UserControl x:Class="XamlCtrl"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300">
- <Grid>
- <Grid.Background>
- <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
- <GradientStop Color="#FF27323E" Offset="0"/>
- <GradientStop Color="#FFD8E8FA" Offset="1"/>
- </LinearGradientBrush>
- </Grid.Background>
- <TextBlock Text="Just a few words to take up space." Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" />
- </Grid>
- </UserControl>
复制代码
(可见此代码,可以直接用拖拉控件的方法得到,当然,加了一些线性渐变的颜色代码)
接下来,将默认生成的class1.vb中的代码,改为如下内容
- 普通浏览复制代码
- Imports Autodesk.AutoCAD.Runtime
- Imports System.Windows.Forms
- Imports System.Windows.Forms.Integration
- Public Class Class1
- Public ps As Autodesk.AutoCAD.Windows.PaletteSet
- Private eh As ElementHost
- Private ctrlXaml As XamlCtrl
- <CommandMethod("xamlPS")> Public Sub xamlPS()
- If (ps = Nothing) Then
- ps = New Autodesk.AutoCAD.Windows.PaletteSet("Pallet Set Example", New Guid("48CFD470-D15A-434b-BC94-6ADAF073025E"))
- eh = New ElementHost()
- eh.Dock = DockStyle.Fill
- ps.Add("Pallet Set Example", eh)
- ctrlXaml = New XamlCtrl()
- ctrlXaml.InitializeComponent()
- eh.Child = ctrlXaml
- End If
- ps.Visible = True
- End Sub
- End Class
复制代码
可见大部分内容还是可以接受的,就是ps这段有些复杂。不过基本也可懂,过程是创建一个面板,加入一个元素,并将XamlCtrl这个刚才创建的wpf用户自定义控件显示到此面板中。好了,可以进行一下测试。在项目属性的调试中,增加acad.exe的外部程序调试。我用2007和2011都可以调试通过。先按下F6,生成一下。
在visual studio中按F5,打开ACAD界面了,键入NETLOAD,找到你保存这个项目中的bin/debug目录,那个叫***.dll的文件就是了。加载之后,键入 xamlPS 这个我们定义的命令。看看,就是我们如下的界面了。
(2)采用建立wpf控件转为wpf应用程序的方法
在上述已有内容的基础上,增加如下内容
创建一个wpf控件(因为无法直接添加wpf窗口,只能建立控件),过程可以如下:在菜单-项目-添加用户控件中找到wpf控件添加之,visual studio2008和2010的位置有一点点不一样,并将这个用户控件重命名为 frmXaml.xaml
在这个用户控件的xaml中,贴入如下代码
- <font style="color: rgb(102, 102, 102); "><ul type="1" class="litype_1" style="margin-left: 14px; "><li style="list-style-position: initial; list-style-image: initial; "><Window x:Name="frmXaml" xmlns="<a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation" target="_blank" class="gj_safe_a" style="-webkit-transition: background-color 0.3s linear; ">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>"</li><li style="list-style-position: initial; list-style-image: initial; ">xmlns:x="<a href="http://schemas.microsoft.com/winfx/2006/xaml" target="_blank" class="gj_safe_a" style="-webkit-transition: background-color 0.3s linear; ">http://schemas.microsoft.com/winfx/2006/xaml</a>"</li><li style="list-style-position: initial; list-style-image: initial; ">x:Class="ToDazeReality.frmXaml"</li><li style="list-style-position: initial; list-style-image: initial; ">Title="Inherits Window" WindowStartupLocation="CenterScreen" WindowStyle="ThreeDBorderWindow" ResizeMode="CanResize" Width="300" Height="400"></li><li style="list-style-position: initial; list-style-image: initial; "> <Window.Background></li><li style="list-style-position: initial; list-style-image: initial; "> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"></li><li style="list-style-position: initial; list-style-image: initial; "> <GradientStop Color="#FF27323E" Offset="0"/></li><li style="list-style-position: initial; list-style-image: initial; "> <GradientStop Color="#FFD8E8FA" Offset="1"/></li><li style="list-style-position: initial; list-style-image: initial; "> </LinearGradientBrush></li><li style="list-style-position: initial; list-style-image: initial; "> </Window.Background></li><li style="list-style-position: initial; list-style-image: initial; "> <Window.Resources></li><li style="list-style-position: initial; list-style-image: initial; "> <Style x:Key="ButtonBlue" BasedOn="{x:Null}" TargetType="{x:Type Button}"></li><li style="list-style-position: initial; list-style-image: initial; "> <Setter Property="Template"></li><li style="list-style-position: initial; list-style-image: initial; "> <Setter.Value></li><li style="list-style-position: initial; list-style-image: initial; "> <ControlTemplate TargetType="{x:Type Button}"></li><li style="list-style-position: initial; list-style-image: initial; "> <Grid></li><li style="list-style-position: initial; list-style-image: initial; "> <Rectangle Stroke="#FF000000" RadiusX="9.5" RadiusY="9.5" x:Name="rectangle"></li><li style="list-style-position: initial; list-style-image: initial; "> <Rectangle.Fill></li><li style="list-style-position: initial; list-style-image: initial; "> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"></li><li style="list-style-position: initial; list-style-image: initial; "> <GradientStop Color="#FF091A2F" Offset="0"/></li><li style="list-style-position: initial; list-style-image: initial; "> <GradientStop Color="#FF5D8DC6" Offset="1"/></li><li style="list-style-position: initial; list-style-image: initial; "> </LinearGradientBrush></li><li style="list-style-position: initial; list-style-image: initial; "> </Rectangle.Fill></li><li style="list-style-position: initial; list-style-image: initial; "> </Rectangle></li><li style="list-style-position: initial; list-style-image: initial; "> <Rectangle Stroke="{x:Null}" RadiusX="9.5" RadiusY="9.5" Margin="2,1,2,0"></li><li style="list-style-position: initial; list-style-image: initial; "> <Rectangle.Fill></li><li style="list-style-position: initial; list-style-image: initial; "> <LinearGradientBrush EndPoint="0.498,0.469" StartPoint="0.5,0"></li><li style="list-style-position: initial; list-style-image: initial; "> <GradientStop Color="#59FFFFFF" Offset="0"/></li><li style="list-style-position: initial; list-style-image: initial; "> <GradientStop Color="#00FFFFFF" Offset="1"/></li><li style="list-style-position: initial; list-style-image: initial; "> </LinearGradientBrush></li><li style="list-style-position: initial; list-style-image: initial; "> </Rectangle.Fill></li><li style="list-style-position: initial; list-style-image: initial; "> </Rectangle></li><li style="list-style-position: initial; list-style-image: initial; "> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/></li><li style="list-style-position: initial; list-style-image: initial; "> </Grid></li><li style="list-style-position: initial; list-style-image: initial; "> <ControlTemplate.Resources></li><li style="list-style-position: initial; list-style-image: initial; "> <Storyboard x:Key="Storyboard1"></li><li style="list-style-position: initial; list-style-image: initial; "> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)"></li><li style="list-style-position: initial; list-style-image: initial; "> <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF5D8DC6"/></li><li style="list-style-position: initial; list-style-image: initial; "> <SplineColorKeyFrame KeyTime="00:00:00.1300000" Value="#FF5DC6A3"/></li><li style="list-style-position: initial; list-style-image: initial; "> </ColorAnimationUsingKeyFrames></li><li style="list-style-position: initial; list-style-image: initial; "> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)"></li><li style="list-style-position: initial; list-style-image: initial; "> <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF091A2F"/></li><li style="list-style-position: initial; list-style-image: initial; "> <SplineColorKeyFrame KeyTime="00:00:00.1300000" Value="#FF0C4F39"/></li><li style="list-style-position: initial; list-style-image: initial; "> </ColorAnimationUsingKeyFrames></li><li style="list-style-position: initial; list-style-image: initial; "> </Storyboard></li><li style="list-style-position: initial; list-style-image: initial; "> <Storyboard x:Key="OutOfHere"/></li><li style="list-style-position: initial; list-style-image: initial; "> <Storyboard x:Key="Storyboard2"></li><li style="list-style-position: initial; list-style-image: initial; "> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle"></li><li style="list-style-position: initial; list-style-image: initial; "> <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF5DC6A3"/></li><li style="list-style-position: initial; list-style-image: initial; "> <SplineColorKeyFrame KeyTime="00:00:00.2560000" Value="#FF5D8DC6"/></li><li style="list-style-position: initial; list-style-image: initial; "> </ColorAnimationUsingKeyFrames></li><li style="list-style-position: initial; list-style-image: initial; "> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="rectangle"></li><li style="list-style-position: initial; list-style-image: initial; "> <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF0C4F39"/></li><li style="list-style-position: initial; list-style-image: initial; "> <SplineColorKeyFrame KeyTime="00:00:00.2560000" Value="#FF091A2F"/></li><li style="list-style-position: initial; list-style-image: initial; "> </ColorAnimationUsingKeyFrames></li><li style="list-style-position: initial; list-style-image: initial; "> </Storyboard></li><li style="list-style-position: initial; list-style-image: initial; "> </ControlTemplate.Resources></li><li style="list-style-position: initial; list-style-image: initial; "> <ControlTemplate.Triggers></li><li style="list-style-position: initial; list-style-image: initial; "> <Trigger Property="IsFocused" Value="True"/></li><li style="list-style-position: initial; list-style-image: initial; "> <Trigger Property="IsDefaulted" Value="True"/></li><li style="list-style-position: initial; list-style-image: initial; "> <Trigger Property="IsMouseOver" Value="True"></li><li style="list-style-position: initial; list-style-image: initial; "> <Trigger.ExitActions></li><li style="list-style-position: initial; list-style-image: initial; "> <BeginStoryboard Storyboard="{StaticResource Storyboard2}" x:Name="OutOfHere_BeginStoryboard"/></li><li style="list-style-position: initial; list-style-image: initial; "> </Trigger.ExitActions></li><li style="list-style-position: initial; list-style-image: initial; "> <Trigger.EnterActions></li><li style="list-style-position: initial; list-style-image: initial; "> <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/></li><li style="list-style-position: initial; list-style-image: initial; "> </Trigger.EnterActions></li><li style="list-style-position: initial; list-style-image: initial; "> </Trigger></li><li style="list-style-position: initial; list-style-image: initial; "> <Trigger Property="IsPressed" Value="True"/></li><li style="list-style-position: initial; list-style-image: initial; "> <Trigger Property="IsEnabled" Value="False"/></li><li style="list-style-position: initial; list-style-image: initial; "> </ControlTemplate.Triggers></li><li style="list-style-position: initial; list-style-image: initial; "> </ControlTemplate></li><li style="list-style-position: initial; list-style-image: initial; "> </Setter.Value></li><li style="list-style-position: initial; list-style-image: initial; "> </Setter></li><li style="list-style-position: initial; list-style-image: initial; "> </Style></li><li style="list-style-position: initial; list-style-image: initial; "> </Window.Resources></li><li style="list-style-position: initial; list-style-image: initial; "> <Grid></li><li style="list-style-position: initial; list-style-image: initial; "> <Button Style="{DynamicResource ButtonBlue}" Name="btnClose" Width="50" Height="26" Foreground="White">Close</Button></li><li style="list-style-position: initial; list-style-image: initial; "> </Grid></li><li style="list-style-position: initial; list-style-image: initial; "></Window></li></ul></font>
复制代码
(可以看出,这个代码其他地方也没有什么太特殊的,就是需要把user control的首尾改成Window而非原来的User control。同时,x:Class="ToDazeReality.frmXaml" 是一句很关键的话。)
接下来,修改原来的class1.vb,使内容有所增加如下
- <div align="right"><font style="color: rgb(119, 153, 170); "><strong>普通浏览复制代码</strong></font></div>
- <font style="color: rgb(102, 102, 102); "><ul type="1" class="litype_1" style="margin-left: 14px; "><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_waKGT" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">Imports</font> Autodesk.AutoCAD.Runtime</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_r0rop" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">Imports</font> <font color="#0066">System</font>.Windows.Forms</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_zBqKk" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">Imports</font> <font color="#0066">System</font>.Windows.Forms.Integration</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_KC8QU" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_shrink.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">Public</font> <font color="#0099">Class</font> Class1</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_wougG" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Public</font> ps <font color="#0099">As</font> Autodesk.AutoCAD.Windows.PaletteSet</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_rNkiE" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Private</font> eh <font color="#0099">As</font> ElementHost</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_zOyyV" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Private</font> ctrlXaml <font color="#0099">As</font> XamlCtrl</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_sE2jq" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_shrink.gif" border="0" alt="" style="cursor: pointer; "> <CommandMethod(<font color="#a31515">"xamlPS"</font>)> <font color="#0099">Public</font> <font color="#0099">Sub</font> xamlPS()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_hPYjR" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_shrink.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">If</font> (ps = <font color="#0099">Nothing</font>) <font color="#0099">Then</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_GfuoK" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> ps = <font color="#0099">New</font> Autodesk.AutoCAD.Windows.PaletteSet(<font color="#a31515">"Pallet Set Example"</font>, <font color="#0099">New</font> Guid(<font color="#a31515">"48CFD470-D15A-434b-BC94-6ADAF073025E"</font>))</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_FiZ4R" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> eh = <font color="#0099">New</font> ElementHost()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_mnQ8V" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> eh.<font color="#0066">Dock</font> = DockStyle.Fill</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_UbIiT" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> ps.<font color="#0066">Add</font>(<font color="#a31515">"Pallet Set Example"</font>, eh)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_D2JJH" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> ctrlXaml = <font color="#0099">New</font> XamlCtrl()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_lGIYl" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> ctrlXaml.InitializeComponent()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_kRQ1Y" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> eh.Child = ctrlXaml</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_em9WN" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">End</font> <font color="#0099">If</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_bcOM4" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> ps.<font color="#0066">Visible</font> = <font color="#0099">True</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_K5T5Z" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">End</font> <font color="#0099">Sub</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_hM9ss" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_shrink.gif" border="0" alt="" style="cursor: pointer; "> <CommandMethod(<font color="#a31515">"xamlForm"</font>)> <font color="#0099">Public</font> <font color="#0099">Sub</font> xamlForm()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_E09Io" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Dim</font> frmXaml <font color="#0099">As</font> <font color="#0099">New</font> ToDazeReality.frmXaml()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_UX0d5" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> frmXaml.ShowDialog()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_Wr8Y0" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> frmXaml = <font color="#0099">Nothing</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_V7C8o" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">End</font> <font color="#0099">Sub</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_nbCCT" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">End</font> <font color="#0099">Class</font>
- </li></ul></font>
复制代码
好了,可以进行一下测试。在项目属性的调试中,增加acad.exe的外部程序调试。我用2007和2011都可以调试通过。先按下F6,生成一下。
在visual studio中按F5,打开ACAD界面了,键入NETLOAD,找到你保存这个项目中的bin/debug目录,那个叫***.dll的文件就是了。加载之后,键入 WPFfrm 这个我们定义的命令。看看,就是我们如下的界面了。
(3)利用.net语言直接生成wpf窗口
这种方法我最早看到是在翔麟的这篇文章
http://bbs.mjtd.com/dispbbs.asp?boardid=33&Id=78758
里面采用c#直接建立wpf窗口,感觉这种方法挺好,甚至可以不用建立wpf控件了,但对使用者的要求要高些,使用者对wpf窗口界面的建立语句必须比较熟悉。
下面是一个建立过程
如上相同的步骤(刚才已做过的可以不用再做)
过程,首先,创建一个VB.NET的类库
接着,添加 acmgd.dll和acdbmgd.dll的引用,并在项目属性中,将这两个的复制本地设为false(C#则可以直接在解决方案资源管理器中修改)。
为了WPF的应用,添加另外几个.net的引用PresentationCore, PresentationFramework, System.Windows.Form, WindowsBase, WindowsFormsIntegration
接下来,添加一个类文件(具体,在项目-添加类),这个类的名字比如叫做 frmWPF.vb
那么,修改其代码如下
- <div align="right"><font style="color: rgb(119, 153, 170); "><strong>普通浏览复制代码</strong></font></div>
- <font style="color: rgb(102, 102, 102); "><ul type="1" class="litype_1" style="margin-left: 14px; "><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_ojZpG" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">Imports</font> <font color="#0066">System</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_UL203" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">Imports</font> <font color="#0066">System</font>.Windows</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_zWrOh" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">Imports</font> <font color="#0066">System</font>.Windows.Controls</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_RX9hb" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">Namespace</font> ToDazeReality</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_GN8Uy" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_shrink.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Public</font> <font color="#0099">Class</font> frmWPF</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_YTg9w" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Inherits</font> Window</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_i6BpQ" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_shrink.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Public</font> <font color="#0099">Shared</font> <font color="#0099">Sub</font> Main()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_IQ38t" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Dim</font> app <font color="#0099">As</font> <font color="#0066">Application</font> = <font color="#0099">New</font> <font color="#0066">Application</font>()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_SvohG" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> app.Run(<font color="#0099">New</font> frmWPF())</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_AS4SK" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">End</font> <font color="#0099">Sub</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_hk006" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Private</font> txtbox1 <font color="#0099">As</font> TextBox</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_u3m49" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_shrink.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Public</font> <font color="#0099">Sub</font> <font color="#0099">New</font>()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_Qya2r" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0066">Title</font> = <font color="#a31515">"WPF acad sample"</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_Uw0HB" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Width</font> = <font color="#ff800">400</font><font color="#ff800">.0</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_qOo7w" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0066">Height</font> = <font color="#ff800">80</font><font color="#ff800">.0</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_up9fC" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Dim</font> stack <font color="#0099">As</font> StackPanel = <font color="#0099">New</font> StackPanel()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_uy0zl" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> Content = stack</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_ZIU7U" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Dim</font> grid1 <font color="#0099">As</font> Grid = <font color="#0099">New</font> Grid()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_c9b2p" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> grid1.Margin = <font color="#0099">New</font> <font color="#0066">Thickness</font>(<font color="#ff800">3</font>)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_c1F1i" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> stack.Children.<font color="#0066">Add</font>(grid1)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_y6fqU" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Dim</font> rowdef <font color="#0099">As</font> RowDefinition = <font color="#0099">New</font> RowDefinition()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_jEOGc" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> rowdef.<font color="#0066">Height</font> = GridLength.<font color="#0099">Auto</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_k1NTr" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> grid1.RowDefinitions.<font color="#0066">Add</font>(rowdef)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_lQqPM" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Dim</font> lbl <font color="#0099">As</font> <font color="#0066">Label</font> = <font color="#0099">New</font> <font color="#0066">Label</font>()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_WOoOa" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> lbl.Margin = <font color="#0099">New</font> <font color="#0066">Thickness</font>(<font color="#ff800">5</font>)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_qt90Z" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> lbl.Content = <font color="#a31515">"Enter something: "</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_i9878" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> lbl.VerticalContentAlignment = VerticalAlignment.<font color="#0066">Center</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_qUb0x" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> lbl.HorizontalAlignment = HorizontalAlignment.<font color="#0066">Left</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_gqlq0" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> lbl.<font color="#0099">Width</font> = <font color="#ff800">150</font><font color="#ff800">.0</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_q2U2g" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> grid1.Children.<font color="#0066">Add</font>(lbl)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_QSEV2" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> txtbox1 = <font color="#0099">New</font> TextBox()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_JOPKP" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> txtbox1.Margin = <font color="#0099">New</font> <font color="#0066">Thickness</font>(<font color="#ff800">5</font>)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_Xe74o" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> txtbox1.<font color="#0099">Width</font> = <font color="#ff800">150</font><font color="#ff800">.0</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_A0z0f" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> txtbox1.VerticalContentAlignment = Windows.VerticalAlignment.<font color="#0066">Center</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_bP8gc" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> txtbox1.HorizontalAlignment = HorizontalAlignment.<font color="#0066">Center</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_k42GL" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> txtbox1.<font color="#0066">Name</font> = <font color="#a31515">"TextBox1"</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_NkZk3" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> grid1.Children.<font color="#0066">Add</font>(txtbox1)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_qG7jm" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Dim</font> btn <font color="#0099">As</font> Button = <font color="#0099">New</font> Button()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_iCewC" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> btn.Margin = <font color="#0099">New</font> <font color="#0066">Thickness</font>(<font color="#ff800">5</font>)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_M7L7H" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> btn.Content = <font color="#a31515">"OK"</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_xDYfd" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> btn.HorizontalAlignment = HorizontalAlignment.<font color="#0066">Right</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_Dl261" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> btn.<font color="#0099">Width</font> = <font color="#ff800">75</font><font color="#ff800">.0</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_fHdof" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> btn.IsDefault = <font color="#0099">True</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_JnIWZ" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">AddHandler</font> btn.Click, <font color="#0099">New</font> RoutedEventHandler(<font color="#0099">AddressOf</font> OK_ButtonClick)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_Tu3XO" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> grid1.Children.<font color="#0066">Add</font>(btn)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_Ir14R" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">End</font> <font color="#0099">Sub</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_MNb4p" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_shrink.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">Private</font> <font color="#0099">Sub</font> OK_ButtonClick(<font color="#0099">ByVal</font> sender <font color="#0099">As</font> <font color="#0099">Object</font>, <font color="#0099">ByVal</font> args <font color="#0099">As</font> RoutedEventArgs)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_vYakV" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0066">MsgBox</font>(txtbox1.Text)</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_WkomB" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0066">Close</font>()</li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_zUw40" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">End</font> <font color="#0099">Sub</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_WNVoa" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "> <font color="#0099">End</font> <font color="#0099">Class</font></li><li style="list-style-position: initial; list-style-image: initial; "><img id="aimg_oLQc2" class="zoom" width="11" height="16" src="http://bbs.mjtd.com/source/plugin/mc_colorcode/images/jssc_none.gif" border="0" alt="" style="cursor: pointer; "><font color="#0099">End</font> <font color="#0099">Namespace</font>
- </li></ul></font>
复制代码
可以看出,在这个vb代码中,直接用Inherits Window的方法,用VB建立了一个WPF窗口。
此时,修改class1.vb的内容如下
- 普通浏览复制代码
- Imports Autodesk.AutoCAD.Runtime
- Imports System.Windows.Forms
- Imports System.Windows.Forms.Integration
- Public Class Class1
- Public ps As Autodesk.AutoCAD.Windows.PaletteSet
- Private eh As ElementHost
- Private ctrlXaml As XamlCtrl
- <CommandMethod("xamlPS")> Public Sub xamlPS()
- If (ps = Nothing) Then
- ps = New Autodesk.AutoCAD.Windows.PaletteSet("Pallet Set Example", New Guid("48CFD470-D15A-434b-BC94-6ADAF073025E"))
- eh = New ElementHost()
- eh.Dock = DockStyle.Fill
- ps.Add("Pallet Set Example", eh)
- ctrlXaml = New XamlCtrl()
- ctrlXaml.InitializeComponent()
- eh.Child = ctrlXaml
- End If
- ps.Visible = True
- End Sub
- <CommandMethod("xamlForm")> Public Sub xamlForm()
- Dim frmXaml As New ToDazeReality.frmXaml()
- frmXaml.ShowDialog()
- frmXaml = Nothing
- End Sub
- <CommandMethod("WPFfrm")> Public Shared Sub WPFfrm()
- Dim wpffrm As New ToDazeReality.frmWPF
- wpffrm.ShowDialog()
- wpffrm = Nothing
- End Sub
- End Class
复制代码
好了,可以进行一下测试。在项目属性的调试中,增加acad.exe的外部程序调试。我用2007和2011都可以调试通过。先按下F6,生成一下。
在visual studio中按F5,打开ACAD界面了,键入NETLOAD,找到你保存这个项目中的bin/debug目录,那个叫***.dll的文件就是了。加载之后,键入 WPFfrm 这个我们定义的命令。看看,就是我们如下的界面了。
|
|