明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2046|回复: 2

[求助] WithEvents 事件

[复制链接]
发表于 2009-10-11 10:55:00 | 显示全部楼层 |阅读模式

我在AutoCAD vba 模块中编写如下代码

Public WithEvents PLine As AcadLWPolyline       ' Use with Modified Event Example
Sub Example_Modified()
     ' This example creates a light weight polyline in model space and
     ' references the new PolyLine using the public variable (PLine) which
     ' is setup to intercept Modified events.
     '
     ' This example then modifies the new object, triggering the code
     ' in the Modified event.
   
    Dim points(0 To 9) As Double
   
    ' Define the 2D polyline points
    points(0) = 1: points(1) = 1
    points(2) = 1: points(3) = 2
    points(4) = 2: points(5) = 2
    points(6) = 3: points(7) = 2
    points(8) = 4: points(9) = 4
       
    ' Create a light weight Polyline object in model space
    '
    ' * Note: We are returning the new PolyLine object into a Module
    ' level variable.  This allows us to intercept events associated
    ' with that particular object.
    Set PLine = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)
   
    ThisDrawing.Application.ZoomAll
   
    ' Modify object to trigger event.
    '
    ' * Note: The event code for the PolyLine modification will be triggered
    ' before we move forward and refresh the view, so the line will not
    ' appear red when the event message box is displayed
    PLine.Color = acRed

    ThisDrawing.Regen acAllViewports
   
End Sub

Private Sub PLine_Modified(ByVal pObject As AutoCAD.IAcadObject)
    ' This example intercepts an object's Modified event.
    '
    ' This event is triggered when an object supporting this event is modified
    '
    ' To trigger this code: Modifiy an object connected to this event
    ' * Note: By connected, we mean the object setup to intercept events using
    ' the VBA WithEvents statement

    ' Use the "pObject" variable to determine which object was modified
    MsgBox "You just modified an object with an ID of: " & pObject.ObjectID
   
End Sub


调试时提示 错误,怎末回事呀?

谢谢赐教!

 楼主| 发表于 2009-10-12 07:44:00 | 显示全部楼层

本人已经解决了:

1、在类中增加一个“eventclassmoduel”类,

2、把Public WithEvents PLine As AcadLWPolyline       ' Use with Modified Event Example

放入“eventclassmoduel”中

3、在标准模块中定义引用即可:dim x as eventclassmoduel

4、sub xxx()

................

’根据需要添加代码

end sub


发表于 2009-10-12 23:18:00 | 显示全部楼层
把整个代码放在thisdarwing里不就可以了,不用搞那么麻烦!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 00:31 , Processed in 0.178702 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表