首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

“AddressOf”报错,请大家帮小弟我修正

2012-02-08 
“AddressOf”报错,请大家帮我修正提示:“AddressOf”表达式不能转换为“Long”,因为“Long”不是委托类型。(改为 I

“AddressOf”报错,请大家帮我修正
提示:“AddressOf”表达式不能转换为“Long”,因为“Long”不是委托类型。(改为 Integer 亦无用处)

本人使用2005不久,请大家帮助我修改它,使其可以在VB2005中正常编译。

VB code
Public Function SetWndProc(ByVal hwnd As Long, ByVal NewWndProc As Boolean) As Long        If NewWndProc Then            prevWndProc = GetWindowLong(hwnd, GWL_WNDPROC)            SetWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndProc)        Else            SetWndProc = SetWindowLong(hwnd, GWL_WNDPROC, prevWndProc)        End If    End Function    Private Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long        If Msg = WM_HOTKEY Then            Select Case wParam                Case 2222                    SetWindowPos(GetForegroundWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)                Case 2223                    SetWindowPos(GetForegroundWindow, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)                Case 2224                    Form1.Close()            End Select            Exit Function        End If        WndProc = CallWindowProc(prevWndProc, hwnd, Msg, wParam, lParam)    End Function



[解决办法]
回调函数WndProc不能是类的成员,必须单独定义或为static
[解决办法]
你参考一下,和VB6不同,Vs2005后的版本要先声明委托类型
VB.NET code
Public Class Form1    '这里的Integer和VB6的Long相同    Delegate Function SubClassProc(ByVal hwnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer '先声明委托SubClassProc类型    Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Integer, ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer    Declare Function SetWindowLong Lib "USER32.DLL" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal attr As Integer, ByVal lVal As SubClassProc) As Integer '注意最后一个参数声明成委托SubClassProc    Const GWL_WNDPROC = (-4)    Const WM_RBUTTONDOWN = &H204    Dim PrevProcPtr As Integer    Private Function myWndProc(ByVal hwnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer        If msg = WM_RBUTTONDOWN Then            MsgBox("你点了鼠标右键!")        End If        myWndProc = CallWindowProc(PrevProcPtr, hwnd, msg, wParam, lParam)    End Function    Sub SubClassWindow(ByVal hwnd As Integer)        If PrevProcPtr = 0 Then            PrevProcPtr = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf myWndProc)        End If    End Sub    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        SubClassWindow(Me.Handle)    End SubEnd Class 

热点排行