VB 6.0 中的AddressOf使用问题,请教各位大侠。
做activex客户端控件时, 在共用方法中调用Addressof提示 调用无效, google了一番, 都说addressof要在类模块中当参数使用。 由于工作需要刚接触vb, 请教各位大侠, 如何在类模块中使用addressof, 具体步骤是怎么的啊?
- VB code
Dim strtest As StringPublic Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim slength As Long, wintext As String ' title bar text length and buffer Dim retval As Long ' return value Dim Buffer As String Buffer = Space(slength) ' make room in the buffer retval = GetWindowText(hwnd, Buffer, slength) ' get title bar text strtest = strtest & "---" & retval EnumChildProc = 1 ' return value of 1 means continue enumerationEnd Function调用方—im lHandle As LonglHandle = FindWindow(vbNullString, "软件窗体名称")Dim retval As Long ' return valueretval = EnumChildWindows(lHandle, AddressOf EnumChildProc, 0)
[解决办法]
AddressOf只能在标准模块中使用
参阅:
使用 AddressOf 关键字
如果代码要调用 Visual Basic 5.0 的函数指针,则必须将该代码放到标准的 .BAS 模块中,不可以将其放到类模块中,也不能将其附加到窗体上。在使用 AddressOf 关键字声明函数时,必须注意下列事项:
AddressOf 只能紧接在是参数列表中的参数前;该参数可以是自定义的过程、函数或者属性的名字。
写在 AddressOf 后面的过程、函数、属性必须与有关的声明和过程在同一个工程中。
AddressOf 只能用于自定义的过程、函数和属性,不能将其用于 Declare 语句声明的外部函数,也不能将其用于类型库中的函数。
在声明的 Sub、Function 或自定义的类型定义中,可以将函数指针传递到 As Any 或 As Long 类型的参数。
注意 可创建用 Visual C++ (或类似的工具)编译的 DLL 中的回调函数原型。要使用 AddressOf 时,原型必须使用 __stdcall 调用约定。不能将缺省调用约定与 AddressOf 并用
[解决办法]
- VB code
'Example Name:EnumWindows and EnumChildWindows Callbacks '------------------------------------------'' BAS Moduel Code''------------------------------------------Option Explicit Private Const LVIF_INDENT As Long = &H10Private Const LVIF_TEXT As Long = &H1Private Const LVM_FIRST As Long = &H1000Private Const LVM_SETITEM As Long = (LVM_FIRST + 6)Private Type LVITEM mask As Long iItem As Long iSubItem As Long state As Long stateMask As Long pszText As String cchTextMax As Long iImage As Long lParam As Long iIndent As LongEnd TypePublic Declare Function EnumWindows Lib "user32" _ (ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As Long Public Declare Function EnumChildWindows Lib "user32" _ (ByVal hWndParent As Long, _ ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As LongPrivate Declare Function GetWindowTextLength Lib "user32" _ Alias "GetWindowTextLengthA" _ (ByVal hwnd As Long) As Long Private Declare Function GetWindowText Lib "user32" _ Alias "GetWindowTextA" _ (ByVal hwnd As Long, _ ByVal lpString As String, _ ByVal cch As Long) As Long Private Declare Function GetClassName Lib "user32" _ Alias "GetClassNameA" _ (ByVal hwnd As Long, _ ByVal lpClassName As String, _ ByVal nMaxCount As Long) As LongPrivate Declare Function IsWindowVisible Lib "user32" _ (ByVal hwnd As Long) As Long Private Declare Function GetParent Lib "user32" _ (ByVal hwnd As Long) As LongPrivate Declare Function SendMessage Lib "user32" _ Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As LongPublic Function EnumWindowProc(ByVal hwnd As Long, _ ByVal lParam As Long) As Long 'working vars Dim nSize As Long Dim sTitle As String Dim sClass As String Dim sIDType As String Dim itmX As ListItem Dim nodX As Node 'eliminate windows that are not top-level. If GetParent(hwnd) = 0& And _ IsWindowVisible(hwnd) Then 'get the window title / class name sTitle = GetWindowIdentification(hwnd, sIDType, sClass) 'add to the listview Set itmX = Form1.ListView1.ListItems.Add(Text:=sTitle, Key:=CStr(hwnd) & "h") itmX.SmallIcon = Form1.ImageList1.ListImages("parent").Key itmX.SubItems(1) = CStr(hwnd) itmX.SubItems(2) = sIDType itmX.SubItems(3) = sClass End If 'To continue enumeration, return True 'To stop enumeration return False (0). 'When 1 is returned, enumeration continues 'until there are no more windows left. EnumWindowProc = 1 End FunctionPrivate Function GetWindowIdentification(ByVal hwnd As Long, _ sIDType As String, _ sClass As String) As String Dim nSize As Long Dim sTitle As String 'get the size of the string required 'to hold the window title nSize = GetWindowTextLength(hwnd) 'if the return is 0, there is no title If nSize > 0 Then sTitle = Space$(nSize + 1) Call GetWindowText(hwnd, sTitle, nSize + 1) sIDType = "title" sClass = Space$(64) Call GetClassName(hwnd, sClass, 64) Else 'no title, so get the class name instead sTitle = Space$(64) Call GetClassName(hwnd, sTitle, 64) sClass = sTitle sIDType = "class" End If GetWindowIdentification = TrimNull(sTitle)End FunctionPublic Function EnumChildProc(ByVal hwnd As Long, _ ByVal lParam As Long) As Long 'working vars Dim sTitle As String Dim sClass As String Dim sIDType As String Dim itmX As ListItem 'get the window title / class name sTitle = GetWindowIdentification(hwnd, sIDType, sClass) 'add to the listview Set itmX = Form2.ListView1.ListItems.Add(Text:=sTitle) itmX.SmallIcon = Form2.ImageList1.ListImages("child").Key itmX.SubItems(1) = CStr(hwnd) itmX.SubItems(2) = sIDType itmX.SubItems(3) = sClass Listview_IndentItem Form2.ListView1.hwnd, CLng(itmX.Index), 1 EnumChildProc = 1 End FunctionPrivate Function TrimNull(startstr As String) As String Dim pos As Integer pos = InStr(startstr, Chr$(0)) If pos Then TrimNull = Left$(startstr, pos - 1) Exit Function End If 'if this far, there was 'no Chr$(0), so return the string TrimNull = startstr End FunctionPrivate Sub Listview_IndentItem(hwnd As Long, _ nItem As Long, _ nIndent As Long) Dim LV As LVITEM 'if nIndent indicates that indentation 'is requested nItem is the item to indent If nIndent > 0 Then With LV .mask = LVIF_INDENT .iItem = nItem - 1 'have to subtract 1 .iIndent = nIndent End With Call SendMessage(hwnd, LVM_SETITEM, 0&, LV) End If End Sub'--end block--''------------------------------------------'' Form Code''------------------------------------------Option ExplicitPrivate Sub Command1_Click() ListView1.ListItems.Clear Call EnumWindows(AddressOf EnumWindowProc, &H0)End SubPrivate Sub Form_Load() Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2End SubPrivate Sub ListView1_DblClick() Dim hwndSelected As Long hwndSelected = Val(ListView1.SelectedItem.Key) Load Form2 Call Form2.EnumSelectedWindow(ListView1.SelectedItem.Text, hwndSelected) End Sub'--end block--' Form2 Code Add the following code to Form2: -------------------------------------------- Option ExplicitPublic Sub EnumSelectedWindow(sItem As String, hwnd As Long) ListView1.ListItems.Clear ListView1.ListItems.Add Text:=sItem, SmallIcon:="parent" Call EnumChildWindows(hwnd, AddressOf EnumChildProc, &H0) Me.Show vbModal End SubPrivate Sub Form_Load() Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2 End Sub