您现在的位置是: 首页 > 硬件咨询 硬件咨询
_vb 硬件模拟鼠标
tamoadmin 2024-08-25 人已围观
简介1.请教,如何用vb实现模拟鼠标中键滚动!2.如何用vb.net做鼠标输入软件3.VB中模仿鼠标动作4.请问vb如何简单的实现模拟鼠标点击屏幕坐标位置5.(VB!) 如何实现鼠标模拟?Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dX As Long, ByVal dY As Long, ByV
1.请教,如何用vb实现模拟鼠标中键滚动!
2.如何用vb.net做鼠标输入软件
3.VB中模仿鼠标动作
4.请问vb如何简单的实现模拟鼠标点击屏幕坐标位置
5.(VB!) 如何实现鼠标模拟?
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dX As Long, ByVal dY As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_MOVE = &H1
Private Const MOUSEEVENTF_ABSOLUTE = &H8000
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Command1_Click()
destX = 100 '鼠标x坐标
destY = 100 '鼠标y坐标
mouse_event MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE + MOUSEEVENTF_RIGHTDOWN, destX, destY, 0, 0
mouse_event MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE + MOUSEEVENTF_RIGHTUP, destX, destY, 0, 0
End Sub
请教,如何用vb实现模拟鼠标中键滚动!
加个变量定义为布尔型,如 Dim Kkeep as Boolean
在键盘按下的过程(KeyDown)中判断是不是E键,如果是则置Kkeep=true
在键盘弹下的过程(KeyUp)中判断是不是E键,如果是则置Kkeep=False
利用Kkeep的值就要以了
如何用vb.net做鼠标输入软件
呵呵 ,默认VB6.0不支持鼠标滚动,你可以下载vb6idemousewheeladdin.dll文件,安装你操作系统盘符里,运行regsvr32 vb6idemousewheeladdin.dll 注册成功后 去vb打开界面的外接程序管理器里加载mousewheel.fix,就OK了。。。。望纳。
VB中模仿鼠标动作
方法一:
调用api
在Visual Baisc.net中的声明:
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2 '模拟鼠标左键按下
Public Const MOUSEEVENTF_LEFTUP = &H4 ’模拟鼠标左键释放
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 '模拟鼠标中间键按下
Public Const MOUSEEVENTF_MIDDLEUP = &H40 '模拟鼠标中间键释放
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 '模拟鼠标右键按下
Public Const MOUSEEVENTF_RIGHTUP = &H10 '模拟鼠标右键释放
Public Const MOUSEEVENTF_MOVE = &H1 '模拟鼠标指针移动
例:
mouse_event MOUSEEVENTF_LEFTDOWN,10,10,0,0
'在(10,10)模拟鼠标左键按下
方法二:
namespace ClassLibrary.Hardware
{
public class Mouse
{
internal const byte SM_MOUSEPRESENT = 19;
internal const byte SM_CMOUSEBUTTONS = 43;
internal const byte SM_MOUSEWHEELPRESENT = 75;
internal struct POINTAPI
{
internal int x;
internal int y;
}
internal struct RECT
{
internal int left ;
internal int top ;
internal int right ;
internal int bottom ;
}
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SwapMouseButton")]
internal extern static int SwapMouseButton ( int bSwap );
[System.Runtime.InteropServices.DllImport("user32" , EntryPoint="ClipCursor")]
internal extern static int ClipCursor(ref RECT lpRect);
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
internal extern static int GetCursorPos( ref POINTAPI lpPoint );
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="ShowCursor")]
internal extern static bool ShowCursor ( bool bShow ) ;
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint = "EnableWindow" )]
internal extern static int EnableWindow( int hwnd , int fEnable );
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetWindowRect")]
internal extern static int GetWindowRect( int hwnd , ref RECT lpRect ) ;
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetCursorPos")]
internal extern static int SetCursorPos ( int x , int y ) ;
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetSystemMetrics")]
internal extern static int GetSystemMetrics( int nIndex );
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetDoubleClickTime")]
internal extern static int SetDoubleClickTime ( int wCount );
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetDoubleClickTime")]
internal extern static int GetDoubleClickTime() ;
[System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint="Sleep")]
internal extern static void Sleep ( int dwMilliseconds ) ;
//得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系
public static int FullScreenPosition_X
{
get
{
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos ( ref _POINTAPI );
return _POINTAPI.x;
}
}
public static int FullScreenPosition_Y
{
get
{
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos ( ref _POINTAPI );
return _POINTAPI.y;
}
}
// 隐藏 显示 鼠标
public static void Hide()
{
ShowCursor( false ) ;
}
public static void Show()
{
ShowCursor( true ) ;
}
// 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了
public static void Lock( System.Windows.Forms.Form ObjectForm )
{
RECT _FormRect = new RECT ();
GetWindowRect( ObjectForm.Handle.ToInt32() , ref _FormRect );
ClipCursor( ref _FormRect );
}
public static void UnLock()
{
RECT _ScreenRect = new RECT ();
_ScreenRect.top = 0;
_ScreenRect.left = 0;
_ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
_ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
ClipCursor( ref _ScreenRect );
}
// 鼠标失效,不过失效的好像不只是鼠标,小心哦
public static void Disable( System.Windows.Forms.Form ObjectForm )
{
EnableWindow( ObjectForm.Handle.ToInt32() , 0 ) ;
}
public static void Enable( System.Windows.Forms.Form ObjectForm )
{
EnableWindow( ObjectForm.Handle.ToInt32() , 1 ) ;
}
// 鼠标自己移动 很想动画哦 参数是2个控件的handle
// 看这个方法前,先用凉水擦把脸。。。 反正我写的时候 头晕
public static void Move ( int From_Handle_ToInt32 , int To_Handle_ToInt32 )
{
RECT rectFrom = new RECT () ;
RECT rectTo = new RECT () ;
int i ;
GetWindowRect( From_Handle_ToInt32 , ref rectFrom ) ;
GetWindowRect( To_Handle_ToInt32 , ref rectTo ) ;
if ( ( rectFrom.left + rectFrom.right ) / 2 - ( rectTo.left + rectTo.right ) / 2 > 0 )
{
for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i >= ( rectTo.left + rectTo.right ) / 2 ; i-- )
{
SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ;
Sleep ( 1 ) ;
}
}
else
{
for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i <= ( rectTo.left + rectTo.right ) / 2 ; i++ )
{
SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ;
Sleep ( 1 ) ;
}
}
if ( ( rectFrom.top + rectFrom.bottom ) / 2 - ( rectTo.top + rectTo.bottom ) / 2 > 0 )
{
for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i >= ( rectTo.top + rectTo.bottom ) / 2 ; i-- )
{
SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ;
Sleep ( 1 ) ;
}
}
else
{
for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i <= ( rectTo.top + rectTo.bottom ) / 2 ; i++ )
{
SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ;
Sleep ( 1 ) ;
}
}
}
// 得到你的鼠标类型
public static string Type
{
get
{
if ( GetSystemMetrics( SM_MOUSEPRESENT ) == 0 )
{
return "本计算机尚未安装鼠标" ;
}
else
{
if ( GetSystemMetrics( SM_MOUSEWHEELPRESENT ) != 0 )
{
return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键滚轮鼠标" ;
}
else
{
return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键鼠标" ;
}
}
}
}
// 设置鼠标双击时间
public static void DoubleClickTime_Set( int MouseDoubleClickTime )
{
SetDoubleClickTime( MouseDoubleClickTime );
}
public static string DoubleClickTime_Get()
{
return GetDoubleClickTime().ToString() ;
}
// 设置鼠标默认主键 我是没有见过谁左手用鼠标
public static void DefaultRightButton()
{
SwapMouseButton ( 1 ) ;
}
public static void DefaultLeftButton()
{
SwapMouseButton ( 0 ) ;
}
}
}
请问vb如何简单的实现模拟鼠标点击屏幕坐标位置
如果不出意外的话,估计楼主对这个API的使用很难搞懂,一楼说的这些东西到处可以整到,如果要用到实际代码中的话,仅用上面那些是实现不了的。很麻烦,我直接给你写一个练习吧,你就会明白。。。你把这个代码放VB用一下就行了。建一个按钮。。。
Option Explicit
'鼠标操作
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
'将鼠标移动至指定位置
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
'获取鼠标当前位置
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Const MOUSEEVENTF_LEFTUP = &H4 ' 左键弹起
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' 左键按下
Private Type POINTAPI
x As Long
y As Long
End Type
Dim po As POINTAPI '获取鼠标位置
Dim poOld As POINTAPI '存储鼠标移动前的位置
'测试,无意义
Private Sub Command1_Click()
Print "Command1.Click"
End Sub
Private Sub Command2_Click()
GetCursorPos poOld
po.x = Command1.Left + Command1.Width / 2 + Me.Left / Screen.TwipsPerPixelX
po.y = Command1.Top + Command1.Height + Me.Top / Screen.TwipsPerPixelY
SetCursorPos po.x, po.y
mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&
'这里可以放左键按下的任意代码
mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
'SetCursorPos poOld.x, poOld.y '这个你可以不要,如果要了这个,就成虚拟鼠标了,也就是这个鼠标在别的地方悄悄地点了一下,但你不会发现他点了。
End Sub
~~~~~~~~~~~~~`
如果你还不明白,你就加我QQ吧。。。已经写的很详细了。。。
(VB!) 如何实现鼠标模拟?
给你介绍两种方法
1.
mouse_event MOUSEEVENTF_LEFTDOWN
mouse_event MOUSEEVENTF_LEFTUP
2.
SendMessage/PostMessage WM_LBUTTONDOWN
SendMessage/PostMessage WM_LBUTTONUP
自己百度下具体怎么用
'这是一个鼠标模拟点击开始菜单的例子,你可以参考程序中的连续点击的过程。
'在窗体上加入控件timer1(interval=1000),然后在窗体代码区复制下面代码,运行,即可看到效果。
'====窗体代码区====
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As INPUT_TYPE, ByVal cbSize As Long) As Long
Private Enum MouseClick '定义鼠标常数
MOUSEEVENTF_LEFTDOWN = &H2
MOUSEEVENTF_LEFTUP = &H4
MOUSEEVENTF_RIGHTDOWN = &H8
MOUSEEVENTF_RIGHTUP = &H10
MOUSEEVENTF_MIDDLEDOWN = &H20
MOUSEEVENTF_MIDDLEUP = &H40
End Enum
Private Const INPUT_MOUSE = 0
Private Const INPUT_KEYBOARD = 1
Private Const INPUT_HARDWARE = 2
Private Type MOUSEINPUT
dx As Long
dy As Long
mouseData As Long
dwFlags As Long
dwtime As Long
dwExtraInfo As Long
End Type
Private Type INPUT_TYPE
dwType As Long
xi(0 To 23) As Byte
End Type
Dim tp_X As Single, tp_Y As Single
Dim ClkIndex As Integer
Dim OpCtWin As Integer
Private Sub VirtualClickMouse(ButtonPressed As MouseClick, Optional ButtonRelease As MouseClick)
Dim intX As Integer
Dim inputEvents(0 To 1) As INPUT_TYPE ' 锁定信息
Dim mouseEvent As MOUSEINPUT '临时锁定鼠标输入信息
mouseEvent.dx = 0 ' 不水平运动
mouseEvent.dy = 0 ' 不垂直运动
mouseEvent.mouseData = 0
mouseEvent.dwFlags = ButtonPressed ' 按键按下
mouseEvent.dwtime = 0 ' 缺省
mouseEvent.dwExtraInfo = 0 ' 非必须
' 复制结构到输入数组缓冲区
inputEvents(0).dwType = INPUT_MOUSE ' 鼠标输入
CopyMemory inputEvents(0).xi(0), mouseEvent, Len(mouseEvent)
' 相上, 放开鼠标按钮。
mouseEvent.dx = 0
mouseEvent.dy = 0
mouseEvent.mouseData = 0
mouseEvent.dwFlags = ButtonRelease ' 按键抬起
mouseEvent.dwtime = 0
mouseEvent.dwExtraInfo = 0
inputEvents(1).dwType = INPUT_MOUSE
CopyMemory inputEvents(1).xi(0), mouseEvent, Len(mouseEvent)
intX = SendInput(2, inputEvents(0), Len(inputEvents(0))) '
End Sub
Sub MoveMouse(X As Single, Y As Single)
Dim pt As POINTAPI
pt.X = X
pt.Y = Y
ClientToScreen hwnd, pt
SetCursorPos pt.X, pt.Y
End Sub
Private Sub Form_Load()
Me.Left = (Screen.Width - Me.Width) / 2
Me.Top = (Screen.Height - Me.Height) / 2
ClkIndex = 0
OpCtWin = 0
End Sub
Private Sub Timer2_Timer()
Select Case OpCtWin
Case Is = 0
tp_X = (-Me.Left / Screen.TwipsPerPixelX) + 40
tp_Y = (Me.Top + Me.Height) / Screen.TwipsPerPixelY - 20
Case Is = 1
tp_X = (-Me.Left / Screen.TwipsPerPixelX) + 40
tp_Y = (Me.Top + Me.Height) / Screen.TwipsPerPixelY - 50
End Select
MoveMouse tp_X, tp_Y
VirtualClickMouse MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP
OpCtWin = OpCtWin + 1
If OpCtWin > 1 Then
OpCtWin = 0
Timer2.Enabled = False
Unload Me
End If
End Sub