如何在FoxPro中指定COM事件回调?

时间:2012-08-05 20:18:48

标签: events com callback foxpro mappoint

我用FoxPro做了很多年,但我有一个需要工作的遗留系统。好的,我可以从FoxPro调用基于COM的应用程序,如MapPoint或Excel。我以前做过那个。但是,如何将函数或对象方法作为事件回调传递? 它甚至可能吗? (我在网上找不到任何东西或我设法追踪的FoxPro书籍)

以下是我所说的VB6示例,取自MapPoint文档。碰巧OnConnection()本身就是一个回调;但是对moaApp.AddCommand()的调用将对回调函数(SayHello())的引用传递给MapPoint(moApp),作为菜单回调。 这并不重要,但我可能需要捕获Save,Quit和Menu回调事件。

Dim moaApp As MapPoint.Application
Public Sub SayHello()
MsgBox "Hello"
End Sub

Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As
AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
On Error GoTo error_handler

Set moaApp = Application

'Add this command to the menu (HOW DO I DO THIS IN FOXPRO?)
moaApp.AddCommand "Saying Hello", "SayHello", Me

Exit Sub

error_handler:

MsgBox Err.Description

End Sub

1 个答案:

答案 0 :(得分:0)

感谢@Alan B的领导,我设法让它运转起来......

通过创建实现所需事件接口的COM类来捕获事件。必须实现界面中的所有事件,尽管它们可以是空实现。

E.g。

&& Create an event handler
oHandler = CREATEOBJECT("MapPointEventHandler") 

&& Connect our _ApplicationEvents implementation
EVENTHANDLER( oMyMapPointApp, oHandler)


&& Here is our event handler

DEFINE CLASS MapPointEventHHandler AS Session OLEPUBLIC 
IMPLEMENTS _ApplicationEvents IN "MapPoint.Application"

 && Call back when MapPoint Quits

   PROCEDURE _ApplicationEvents_Quit()  
      MESSAGEBOX("QuitHandler called")
   ENDPROC

   && Event indicates MapPoint is about to close
   PROCEDURE _ApplicationEvents_BeforeClose( bcancel as logical) AS VOID
      MESSAGEBOX("before close called")
   ENDPROC

   && These events are not used here, but must be defined for COM/class compatibility
   PROCEDURE _ApplicationEvents_BeforeSave( SaveAsUI AS logical @, bcancel as logical) AS VOID
      *? PROGRAM()
   ENDPROC
   PROCEDURE _ApplicationEvents_New() AS VOID
      *? PROGRAM()
   ENDPROC
   PROCEDURE _ApplicationEvents_Open() AS VOID
      *? PROGRAM()
   ENDPROC    

ENDDEFINE

也可以传递方法(例如,用于菜单项),但这些方法不能同一个类。您需要为要实现的每个事件处理程序接口实现一个类,并使用一个单独的类来处理菜单回调。

以下是菜单项的示例:

&& Create a menu handler
oMyMenu = CREATEOBJECT("MapPointMenuHandler") 

&& Add our Tools menu entries and hook them up
oMyMapPointApp.AddCommand("Custom Menu Item", "MyMenuCallBack", oMyMenu)


&& This class implements the Tools menu callbacks
&& *** NOTE: MessageBox will appear UNDER MapPoint

DEFINE CLASS MapPointMenuHandler AS Session OLEPUBLIC 

   PROCEDURE MyMenuCallback()
      MESSAGEBOX("Main Menu callback")
   ENDPROC  

ENDDEFINE