关于vb编程中的AddressOf的问题!高手来帮下。谢谢了!!!!

来源:百度知道 编辑:UC知道 时间:2024/06/23 14:58:51
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 EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String

S = String(80, 0)
Call GetWindowText(hwnd, S, 80)
S = Left(S, InStr(S, Chr(0)) - 1)

If Len(S) > 0 Then Form1.List1.AddItem S

EnumWindowsProc = True
End Function

Private Sub Command1_Click()
List1.Clear
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub

是在AddressOf这出现问题的。但我不知道为什么。大家指点下!!

用 AddressOf 取出的函数地址,这个函数必须在 VB 的标准模块中。

窗体模块或类模块中的函数使用 AddressOf 关键字,则会发生编译错误。

把这些放在模块里

Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String

S = String(80, 0)
Call GetWindowText(hwnd, S, 80)
S = Left(S, InStr(S, Chr(0)) - 1)

If Len(S) > 0 Then Form1.List1.AddItem S

EnumWindowsProc = True
End Function

你应该将EnumWindowsProc()放到模块里.(*.bas文件)