0

自从我使用 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
4

1 回答 1

0

感谢@Alan B 的领导,我已经设法让它工作了......

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

例如

&& 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  
于 2012-08-22T17:07:57.933 回答