使用回调函数需要借助API函数.(SetWindowLong) 回调有很多种,看你需要什么样的回调函数了 一个简单的消息回调 OldWindowProc = SetWindowLong(Frm_Show.hwnd, GWL_WNDPROC, AddressOf WindowProc)Public Function WindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '系统运用函数判断鼠标动作,进行处理 If Msg = TRAY_CALLBACK Then If lParam = WM_LBUTTONUP Then If Frm_Show.WindowState = vbMinimized Then _ Frm_Show.WindowState = LastState Frm_Show.Show Exit Function End If If lParam = WM_RBUTTONUP Then Frm_Show.PopupMenu Frm_Menu Exit Function End If End If ' window proc WindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam) '恢复窗口原来的句柄设置 End Function
CallByName Function Executes a method of an object, or sets or returns a property of an object.SyntaxCallByName(object, procname, calltype,[args()])The CallByName function syntax has these named arguments:Part Description object Required; Variant (Object). The name of the object on which the function will be executed. procname Required; Variant (String). A string expression containing the name of a property or method of the object. calltype Required; Constant. A constant of type vbCallType representing the type of procedure being called. args() Optional: Variant (Array). ResThe CallByName function is used to get or set a property, or invoke a method at run time using a string name.In the following example, the first line uses CallByName to set the MousePointer property of a text box, the second line gets the value of the MousePointer property, and the third line invokes the Move method to move the text box:CallByName Text1, "MousePointer", vbLet, vbCrosshair Result = CallByName (Text1, "MousePointer", vbGet) CallByName Text1, "Move", vbMethod, 100, 100
先谢谢各 位啦 不好意思,我没说清楚。我想在DLL中定义一公共变量,然后在程序的 某一函数调用该变量。在外部程序中(EXE或VBS)中把DLL类中定义的公共 变量指向一自定义函数。当外部程序执行DLL函数时,自动回调自定义函数。 该方法在VBS中是可行的,但在EXE中不行,我找过MSDN,里面好象说要引用 两个类,但我找不到那些类。请帮忙!! 我的程序如下:dll: '---------------------------------------------------- CBTest.cls '---------------------------------------------------- Public CBFunction As Variant Public Function Test() As Boolean Dim bolRet As Boolean bolRet = Module1.FCallback Test = bolRet End Function'----------------------------------------------------------- module1.bas '----------------------------------------------------------- Private CBT As New CBTest Public Function FCallback() As Boolean Dim vRet As Variant
vRet = CBT.CBFunction FCallback = True End Function VBS dim objTCB ,bolRet set objTCB=createobject("PCallbackT.CBTest") objTCB.CBFunction=getref("testcb") bolRet=objTCB.Test function testcb() msgbox "already calback" end function
回调有很多种,看你需要什么样的回调函数了
一个简单的消息回调
OldWindowProc = SetWindowLong(Frm_Show.hwnd, GWL_WNDPROC, AddressOf WindowProc)Public Function WindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'系统运用函数判断鼠标动作,进行处理
If Msg = TRAY_CALLBACK Then
If lParam = WM_LBUTTONUP Then
If Frm_Show.WindowState = vbMinimized Then _
Frm_Show.WindowState = LastState
Frm_Show.Show
Exit Function
End If
If lParam = WM_RBUTTONUP Then
Frm_Show.PopupMenu Frm_Menu
Exit Function
End If
End If ' window proc
WindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)
'恢复窗口原来的句柄设置
End Function
Executes a method of an object, or sets or returns a property of an object.SyntaxCallByName(object, procname, calltype,[args()])The CallByName function syntax has these named arguments:Part Description
object Required; Variant (Object). The name of the object on which the function will be executed.
procname Required; Variant (String). A string expression containing the name of a property or method of the object.
calltype Required; Constant. A constant of type vbCallType representing the type of procedure being called.
args() Optional: Variant (Array).
ResThe CallByName function is used to get or set a property, or invoke a method at run time using a string name.In the following example, the first line uses CallByName to set the MousePointer property of a text box, the second line gets the value of the MousePointer property, and the third line invokes the Move method to move the text box:CallByName Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName (Text1, "MousePointer", vbGet)
CallByName Text1, "Move", vbMethod, 100, 100
位啦
不好意思,我没说清楚。我想在DLL中定义一公共变量,然后在程序的
某一函数调用该变量。在外部程序中(EXE或VBS)中把DLL类中定义的公共
变量指向一自定义函数。当外部程序执行DLL函数时,自动回调自定义函数。
该方法在VBS中是可行的,但在EXE中不行,我找过MSDN,里面好象说要引用
两个类,但我找不到那些类。请帮忙!!
我的程序如下:dll:
'----------------------------------------------------
CBTest.cls
'----------------------------------------------------
Public CBFunction As Variant
Public Function Test() As Boolean
Dim bolRet As Boolean
bolRet = Module1.FCallback
Test = bolRet
End Function'-----------------------------------------------------------
module1.bas
'-----------------------------------------------------------
Private CBT As New CBTest
Public Function FCallback() As Boolean
Dim vRet As Variant
vRet = CBT.CBFunction
FCallback = True
End Function
VBS
dim objTCB ,bolRet
set objTCB=createobject("PCallbackT.CBTest")
objTCB.CBFunction=getref("testcb")
bolRet=objTCB.Test
function testcb()
msgbox "already calback"
end function
谢谢:
好象文中的情况和我这里的有所不同,文中是调用了API 函数的,我这里
是在DLL的类中加入一变量,然后通过变量引入回调函数的。