获取表单处理程序时出错(Vb.net)

时间:2011-04-12 23:26:28

标签: vb.net math sendmessage findwindow

我有当前的代码:

Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As HandleRef, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
End Function

Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

Public Function WindowHandle(ByVal sTitle As String) As Long
    WindowHandle = FindWindow(vbNullString, sTitle)
End Function

Dim CurrentProcess As Process
Dim CurrentHandle As IntPtr

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    SendMessage(CurrentHandle, 256, Keys.A, 0)
    SendMessage(CurrentHandle, 257, Keys.A, 65539)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        CurrentProcess = Process.GetProcessesByName(ListBox1.SelectedItem.ToString.Remove(0, ListBox1.SelectedItem.ToString.IndexOf("{") + 1).Replace("}", ""))(0)
        CurrentHandle = New IntPtr(WindowHandle(CurrentProcess.MainWindowTitle))
        Timer1.Start()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

如果您看一下按钮sub,每次运行时都会出现“算术溢出错误!”错误。

这里有什么问题?这应该有效......对吧?

对不起,这有点模糊,但就像我所知道的那样。

1 个答案:

答案 0 :(得分:5)

您的声明语句是正确的,但您调用它们的方式不正确。 VB.NET允许的松散输入让你陷入困境,你无法诊断。解决这个问题的方法是让编译器告诉你,你做错了。将它放在源代码文件的顶部:

 Option Strict On