[英]How do I specify a COM event callback in FoxPro?
It is many years since I did anything with FoxPro, but I have a legacy system that needs work. 我使用FoxPro做任何事情已经有很多年了,但是我有一个需要工作的旧系统。 Okay, I can call a COM-based application such as MapPoint or Excel from FoxPro. 好的,我可以从FoxPro调用基于COM的应用程序,例如MapPoint或Excel。 I've done that before. 我以前做过。 However, how do I pass a function or object method as an event callback? 但是,如何传递函数或对象方法作为事件回调? Is it even possible? 可能吗 (I can't find anything online or the FoxPro books I've managed to track down) (我找不到任何在线内容或找不到的FoxPro书籍)
The following is a VB6 example of what I mean, taken from the MapPoint documentation. 以下是我的意思的VB6示例,摘自MapPoint文档。 As it happens OnConnection()
is itself a callback; 碰巧OnConnection()
本身就是一个回调; but the call to moaApp.AddCommand()
passes a reference to a callback function ( SayHello()
) to MapPoint ( moApp
), as a menu callback. 但对moaApp.AddCommand()
的调用会将对回调函数( SayHello()
)的引用传递给MapPoint( moApp
),作为菜单回调。 Not that it matters for the question, but I'm probably going to need to trap the Save, Quit, and Menu callback events. 并不是这个问题很重要,但是我可能需要捕获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
Thanks to the lead from @Alan B, I've managed to get it working... 感谢@Alan B的带领,我设法使它工作起来...
Events are caught by creating a COM class that implements the required event interface. 通过创建实现所需事件接口的COM类来捕获事件。 All events in the interface have to be implemented, although they can be empty implementations. 接口中的所有事件都必须实现,尽管它们可以是空的实现。
Eg 例如
&& 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
Methods can also be passed (eg. for menu items), but these cannot be to the same class. 也可以传递方法(例如,用于菜单项),但是这些方法不能属于同一类。 You need to implement one class for each event handler interface you wish to implement, and a separate class to handle the menu callbacks. 您需要为要实现的每个事件处理程序接口实现一个类,并为处理菜单回调提供一个单独的类。
Here is an example with a menu item: 这是带有菜单项的示例:
&& 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.