本帖最后由 qjchen 于 2010-12-28 22:13 编辑
(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
那么,修改其代码如下
- Imports System
- Imports System.Windows
- Imports System.Windows.Controls
- Namespace ToDazeReality
- Public Class frmWPF
- Inherits Window
- Public Shared Sub Main()
- Dim app As Application = New Application()
- app.Run(New frmWPF())
- End Sub
- Private txtbox1 As TextBox
- Public Sub New()
- Title = "WPF acad sample"
- Width = 400.0
- Height = 80.0
- Dim stack As StackPanel = New StackPanel()
- Content = stack
- Dim grid1 As Grid = New Grid()
- grid1.Margin = New Thickness(3)
- stack.Children.Add(grid1)
- Dim rowdef As RowDefinition = New RowDefinition()
- rowdef.Height = GridLength.Auto
- grid1.RowDefinitions.Add(rowdef)
- Dim lbl As Label = New Label()
- lbl.Margin = New Thickness(5)
- lbl.Content = "Enter something: "
- lbl.VerticalContentAlignment = VerticalAlignment.Center
- lbl.HorizontalAlignment = HorizontalAlignment.Left
- lbl.Width = 150.0
- grid1.Children.Add(lbl)
- txtbox1 = New TextBox()
- txtbox1.Margin = New Thickness(5)
- txtbox1.Width = 150.0
- txtbox1.VerticalContentAlignment = Windows.VerticalAlignment.Center
- txtbox1.HorizontalAlignment = HorizontalAlignment.Center
- txtbox1.Name = "TextBox1"
- grid1.Children.Add(txtbox1)
- Dim btn As Button = New Button()
- btn.Margin = New Thickness(5)
- btn.Content = "OK"
- btn.HorizontalAlignment = HorizontalAlignment.Right
- btn.Width = 75.0
- btn.IsDefault = True
- AddHandler btn.Click, New RoutedEventHandler(AddressOf OK_ButtonClick)
- grid1.Children.Add(btn)
- End Sub
- Private Sub OK_ButtonClick(ByVal sender As Object, ByVal args As RoutedEventArgs)
- MsgBox(txtbox1.Text)
- Close()
- End Sub
- End Class
- End Namespace
可以看出,在这个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 这个我们定义的命令。看看,就是我们如下的界面了。
此贴仅为 学习mikko代码的学习记录过程,希望能对看本帖的您也有点用处~
|