robbin840311 发表于 2008-11-7 11:30:00

【原创】VB封装含窗体的DLL,并在CASS中调用它

本帖最后由 作者 于 2008-11-8 8:52:11 编辑 <br /><br /> <p>以改变选择字的颜色这个简单的功能为例进行说明。这样,以后可以在VB中设置窗体,然后再VBA中调用它。因为VB中设置窗体和书写代码比在VBA中的窗体方便的多咯。一些控件在VBA中不能用,控件也不能编组。所以有此想法,将窗体和一些代码在VB环境下设好,然后打包为DLL,再在VBA中调用它。</p><p><strong>1、在VB环境下:</strong></p><p>创建一个ActiveX DLL工程,然后添加一个窗体和模块,这里模块的作用是用于定义全局变量。再在窗体上添加一个按钮与文本框。</p><p><font color="#ff0000">在窗体(Form1)中输入如下代码:</font></p><p>Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long<br/>Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long<br/>Private m_oApp As Object</p><p>&nbsp;<wbr></wbr><wbr></wbr><br/>Public Property Set Application(ByVal vNewValue As Object)<br/>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr> Set m_oApp = vNewValue<br/>End Property</p><p>Private Sub Command1_Click()</p><p>&nbsp;<wbr></wbr><wbr></wbr><br/>&nbsp;<wbr></wbr><wbr></wbr> acadDoc.Utility.GetEntity pTxtObj, pBasePt, vbNewLine &amp; "请选择文字:"<br/>&nbsp;<wbr></wbr><wbr></wbr><br/>&nbsp;<wbr></wbr><wbr></wbr> Text1.Text = pTxtObj.TextString<br/>&nbsp;<wbr></wbr><wbr></wbr> '修改颜色<br/>&nbsp;<wbr></wbr><wbr></wbr> With pTxtObj<br/>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr> .TrueColor = pColor(0, 255, 0) '修改为绿色<br/>&nbsp;<wbr></wbr><wbr></wbr> End With<br/>&nbsp;<wbr></wbr><wbr></wbr><br/>&nbsp;<wbr></wbr><wbr></wbr> '刷新<br/>&nbsp;<wbr></wbr><wbr></wbr> acadDoc.Application.Update<br/>End Sub</p><p>Private Sub Form_Load()<br/>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr> SetParent Me.hwnd, GetParent(GetParent(m_oApp.ActiveDocument.hwnd))<br/>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr><br/>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr> '获得当前活动文档<br/>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr> Set acadDoc = m_oApp.ActiveDocument<br/>End Sub</p><p>'********************************<br/>'名称:pColor函数<br/>'作者:罗简单<br/>'日期:2008-7-3<br/>'功能:用户自定义颜色,颜色模式为RGB<br/>'********************************<br/>'定义绿色<br/>Public Function pColor(ByRef pRed As Integer, ByRef pGreen As Integer, ByRef pBlue As Integer) As AcadAcCmColor<br/>&nbsp;<wbr></wbr><wbr></wbr> Dim icorgreen As AcadAcCmColor<br/>&nbsp;<wbr></wbr><wbr></wbr> Set icorgreen = m_oApp.GetInterfaceObject("AutoCAD.AcCmColor.16")<br/>&nbsp;<wbr></wbr><wbr></wbr> Call icorgreen.SetRGB(pRed, pGreen, pBlue) '用户自定义颜色<br/>&nbsp;<wbr></wbr><wbr></wbr><br/>&nbsp;<wbr></wbr><wbr></wbr> Set pColor = icorgreen<br/>End Function<wbr></wbr><wbr></wbr></p><p>'文本框内容修改后,双击则将刚才文字的内容修改为文本框中的内容</p><p>Private Sub Text1_DblClick()<br/>&nbsp;<wbr></wbr><wbr></wbr> pTxtObj.TextString = Text1.Text<br/>&nbsp;<wbr></wbr><wbr></wbr> pTxtObj.Update<br/>End Sub<br/><wbr></wbr><wbr></wbr></p><p><font color="#ff0000">在模块中输入如下代码:</font></p><p>Public acadDoc As AcadDocument&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr> ' 当前活动文档对象<br/>Public pTxtObj As AcadText<br/>Public pBasePt As Variant<wbr></wbr><wbr></wbr></p><p><font color="#ff0000">在类模块(ChangeTextColor)中输入如下代码:</font></p><p>Private m_oApp As Object</p><p>Public Property Set Application(ByVal vNewValue As Object)<br/>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr> Set m_oApp = vNewValue<br/>End Property</p><p>Public Sub ShowForm()<br/>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr> Set Form1.Application = m_oApp<br/>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr>&nbsp;<wbr></wbr><wbr></wbr> Form1.Show<br/>End Sub<wbr></wbr><wbr></wbr></p><p>最后进行打包,生成ChangeTextColor.dll<wbr></wbr><wbr></wbr></p><p><strong>2、在CASS中的VBA环境下</strong></p><p>引用刚才生成的DLL文件,然后新建过程,输入如下代码:</p><p>Sub tt()<br/>&nbsp;<wbr></wbr><wbr></wbr> Dim myForm As New ChangeTextColor<br/>&nbsp;<wbr></wbr><wbr></wbr> Set myForm.Application = Application<br/>&nbsp;<wbr></wbr><wbr></wbr> myForm.ShowForm<br/>End Sub</p><p>这个过程只是在CASS环境中把窗体调用出来了,其他操作的代码在窗体中,这样就可以将VB中的窗体和代码在VBA的环境下应用了。</p><p><strong>3、程序的运行步骤如下:</strong></p><p>运行VBA中的tt过程,调出了VB窗体,然后单击按钮,选择文字,文件颜色将变为绿色,并且文字内容出现在文本框内,修改文本框内的内容,然后双击文本框,这样CAD中的文字内容被修改为文本框中的内容。</p>

jxlsp 发表于 2008-11-7 18:31:00

很好的想法.

jxphklibin 发表于 2008-11-8 16:29:00

很好,支持了

yshf 发表于 2008-11-9 22:54:00

如果是用Lisp调用,该如何做?

robbin840311 发表于 2008-11-10 08:07:00

能够把DVB做成Lisp命令的,然后通过命令调用。

robbin840311 发表于 2008-11-19 21:15:00

非恶意灌水

nonsmall 发表于 2009-2-10 16:05:00

<p>恩 不错</p><p>楼主是否能写一个通用DLL</p><p>让VBA可以调用任意VB窗体?</p>

yu-gn 发表于 2009-2-12 13:57:00

<p>好想法</p><p>严重支持</p>

robbin840311 发表于 2009-2-12 16:45:00

<p>7楼的朋友,你的想法非常好!</p><p>大家也可以修改一下代码看可否实现。</p><p>这段时间手头有项目忙,没时间去想。</p>

yxr_MJTD 发表于 2009-3-5 10:35:00

这个方法我也有,只是现在唯一让我想知道的就是怎么让VB突破CAD的版本问题.
页: [1] 2
查看完整版本: 【原创】VB封装含窗体的DLL,并在CASS中调用它