VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.Net远程控制编程实例[鼠标钩子、 键盘钩子]

功能描述:模拟鼠标事件的函数模型  设置光标到指定位置的函数模型 模拟键盘事件的函数模型

命名空间:Lob.API

转化后代码如下:

    Imports System
    
Imports System.Runtime.InteropServices
    
    Namespace Lob.API


    Public Class Api
        
        
''' <summary>
        ''' 模拟鼠标事件的函数模型
        ''' </summary>
        ''' <param name="flags"></param>
        ''' <param name="dx"></param>
        ''' <param name="dy"></param>
        ''' <param name="dwData"></param>
        ''' <param name="dwExtraInfo"></param>
        
Public Declare Sub mouse_event Lib "user32.dll"  _(ByVal flags As IntegerByVal dx As IntegerByVal dy As IntegerByVal dwData As IntegerByVal dwExtraInfo As Integer)
        
        
''' <summary>
        ''' 设置光标到指定位置的函数模型
        ''' </summary>
        ''' <param name="X"></param>
        ''' <param name="Y"></param>
        ''' <returns></returns>
        
Public Declare Function SetCursorPos Lib "user32.dll"  _

(ByVal As IntegerByVal As IntegerAs Boolean
        
        
''' <summary>
        ''' 模拟键盘事件的函数模型
        ''' </summary>
        ''' <param name="bVk"></param>
        ''' <param name="bScan"></param>
        ''' <param name="dwFlags"></param>
        ''' <param name="dwExtraInfo"></param>
        
Public Declare Sub keybd_event Lib "user32.dll" Alias "keybd_event" _

 (ByVal bVk As ByteByVal bScan As ByteByVal dwFlags As IntegerByVal dwExtraInfo As Integer)
    
End Class
End Namespace

    

 

 

功能描述::鼠标控制(Hook鼠标)  鼠标钩子

 

相关事件 :鼠标Hook类 委托-鼠标按键触发 委托-鼠标移动触发 模拟鼠标移动的事件 模拟鼠标按下的事件

创建鼠标钩子的实例  控制鼠标执行相应操作

命名空间:Lob.Hooks

Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Imports Lob.API
Imports Lob.Codes
Namespace Lob.Hooks

    Public Class MouseHook

        Public Sub New()
            MyBase.New()
            MouseButton = (MouseButton + New DoMouseButtons(Api.mouse_event))
            MouseMove = (MouseMove + New DoMouseMove(Api.SetCursorPos))
        End Sub

        Private Event MouseButton As DoMouseButtons

        Private Event MouseMove As DoMouseMove

        Public Sub MouseWork(ByVal MEvent As MouseEvent)
            Select Case (MEvent.Type)
                Case MouseEventType.MouseMove
                    MouseMove(MEvent.X, MEvent.Y)
                Case MouseEventType.MouseLeftDown
                    MouseMove(MEvent.X, MEvent.Y)
                    MouseButton(CType(MouseEventFlag.LeftDown, Integer), MEvent.X, MEvent.Y, 0, 0)
                Case MouseEventType.MouseLeftUp
                    MouseMove(MEvent.X, MEvent.Y)
                    MouseButton(CType(MouseEventFlag.LeftUp, Integer), MEvent.X, MEvent.Y, 0, 0)
                Case MouseEventType.MouseRightDown
                    MouseButton(CType(MouseEventFlag.RightDown, Integer), MEvent.X, MEvent.Y, 0, 0)
                Case MouseEventType.MouseRightUp
                    MouseButton(CType(MouseEventFlag.RightUp, Integer), MEvent.X, MEvent.Y, 0, 0)
                Case MouseEventType.MouseClick
                    MouseMove(MEvent.X, MEvent.Y)
                    MouseButton(CType(MouseEventFlag.LeftDown, Integer), MEvent.X, MEvent.Y, 0, 0)
                    MouseButton(CType(MouseEventFlag.LeftUp, Integer), MEvent.X, MEvent.Y, 0, 0)
                Case MouseEventType.MouseDoubleClick
                    MouseMove(MEvent.X, MEvent.Y)
                    MouseButton(CType(MouseEventFlag.LeftDown, Integer), MEvent.X, MEvent.Y, 0, 0)
                    MouseButton(CType(MouseEventFlag.LeftDown, Integer), MEvent.X, MEvent.Y, 0, 0)
                    MouseButton(CType(MouseEventFlag.LeftUp, Integer), MEvent.X, MEvent.Y, 0, 0)
                    MouseButton(CType(MouseEventFlag.LeftUp, Integer), MEvent.X, MEvent.Y, 0, 0)
            End Select
        End Sub

        Public Enum MouseEventFlag

            Move = 1

            LeftDown = 2

            LeftUp = 4

            RightDown = 8

            RightUp = 16

            MiddleDown = 32

            MiddleUp = 64

            XDown = 128

            XUp = 256

            Wheel = 2048

            VirtualDesk = 16384

            Absolute = 32768
        End Enum

        Public Delegate Sub DoMouseButtons(ByVal flags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer)

        Public Delegate Function DoMouseMove(ByVal X As Integer, ByVal Y As Integer) As Boolean
    End Class
End Namespace

功能描述:键盘控制(Hook键盘)  键盘钩子

相关事件 :按下按键  释放按键

命名空间:Lob.Hooks

Imports System
Imports System.Runtime.InteropServices
Imports Lob.API

Namespace Lob.Hooks

    Public Class KeyBoardHook

        Private Const KEYEVENTF_KEYDOWN As Integer = 1

        Private Const KEYEVENTF_KEYUP As Integer = 2

        Public Shared Sub KeyDown(ByVal keyCode As System.Windows.Forms.Keys)
            Api.keybd_event(CType(keyCode, Byte), 0, KEYEVENTF_KEYDOWN, 0)
        End Sub

        Public Shared Sub KeyUp(ByVal keyCode As System.Windows.Forms.Keys)
            Api.keybd_event(CType(keyCode, Byte), 0, KEYEVENTF_KEYUP, 0)
        End Sub
    End Class

End Namespace


相关教程