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

关于VB模拟鼠标按键的有关问题

2012-02-03 
关于VB模拟鼠标按键的问题我想对某一程序 模拟鼠标同时按下左键、中键、右键和同时松开这三个键!请问应该怎

关于VB模拟鼠标按键的问题
我想对某一程序 模拟鼠标同时按下左键、中键、右键和同时松开这三个键!请问应该怎么实现?请大虾给个实例!谢谢了!

[解决办法]
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '模拟鼠标的左键单击!

' MOUSEEVENTF_LEFTDOWN 模拟鼠标左键按下
' MOUSEEVENTF_LEFTUP 模拟鼠标左键抬起
' MOUSEEVENTF_RIGHTDOWN 模拟鼠标右键按下
' MOUSEEVENTF_RIGHTUP 模拟鼠标右键抬起
' MOUSEEVENTF_MIDDLEDOWN 模拟鼠标中键按下
' MOUSEEVENTF_MIDDLEUP 模拟鼠标中键抬起
[解决办法]

探讨
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '模拟鼠标的左键单击!

' MOUSEEVENTF_LEFTDOWN 模拟鼠标左键按下
' MOUSEEVENTF_LEFTUP 模拟鼠标左键抬起
' MOUSEEVENTF_RIGHTDOWN 模拟鼠标右键按下
' MOUSEEVENTF_RIGHTU……

[解决办法]
探讨
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '模拟鼠标的左键单击!

' MOUSEEVENTF_LEFTDOWN 模拟鼠标左键按下
' MOUSEEVENTF_LEFTUP 模拟鼠标左键抬起
' MOUSEEVENTF_RIGHTDOWN 模拟鼠标右键按下
' MOUSEEVENTF_RIGHTU……

[解决办法]
VB code
'Example Name:Auto Click Menu'Example by Daniel Kaufmann (daniel@i.com.uy)'Paste this code in a Form'with a Menu named menu1 which has a menuitem named menu2Private Type POINTAPI    x As Long    y As LongEnd TypePrivate Type RECT    Left As Long    Top As Long    Right As Long    Bottom As LongEnd TypePrivate Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As LongPrivate Declare Function GetMenuItemRect Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long, ByVal uItem As Long, lprcItem As RECT) As LongPrivate Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As LongPrivate Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute movePrivate Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button downPrivate Const MOUSEEVENTF_LEFTUP = &H4 ' left button upPrivate Const MOUSEEVENTF_MOVE = &H1 ' mouse movePrivate Const MOUSEEVENTF_MIDDLEDOWN = &H20Private Const MOUSEEVENTF_MIDDLEUP = &H40Private Const MOUSEEVENTF_RIGHTDOWN = &H8Private Const MOUSEEVENTF_RIGHTUP = &H10Private 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 GetMessageExtraInfo Lib "user32" () As LongPrivate Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As LongConst SM_CXSCREEN = 0 'X Size of screenConst SM_CYSCREEN = 1 'Y Size of ScreenPrivate Sub Form_KeyPress(KeyAscii As Integer)    Dim mWnd As Long    mWnd = Me.hwnd        Dim hMenu As Long, hSubMenu As Long    hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)    ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu    hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu    ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu    End SubPrivate Sub ScreenToAbsolute(lpPoint As POINTAPI)lpPoint.x = lpPoint.x * (&HFFFF& / GetSystemMetrics(SM_CXSCREEN))lpPoint.y = lpPoint.y * (&HFFFF& / GetSystemMetrics(SM_CYSCREEN))End SubPrivate Sub Click(p As POINTAPI)'p.X and p.Y in absolute coordinates'Put the mouse on the pointmouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, p.x, p.y, 0, GetMessageExtraInfo()'Mouse Downmouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()'Mouse Upmouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()End SubPrivate Sub ClickMenuItem(ByVal mWnd As Long, ByVal hMenu As Long, ByVal Pos As Long)Dim ret As LongDim r As RECT, p As POINTAPIret = GetMenuItemRect(mWnd, hMenu, Pos, r)If ret = 0 Then Exit Subp.x = (r.Left + r.Right) / 2p.y = (r.Top + r.Bottom) / 2ScreenToAbsolute p'Click on pClick pEnd SubPrivate Sub Form_Load()Dim mWnd As Long, p As POINTAPImWnd = Me.hwndDim hMenu As Long, hSubMenu As LonghMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenuhSubMenu = GetSubMenu(hMenu, 0) 'Get its submenuClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenup.x = &HFFFF& / 2p.y = &HFFFF& / 2Click pMe.AutoRedraw = TrueMe.BackColor = vbWhitePrint "Press any key"End SubPrivate Sub menu2_Click()MsgBox "Click"End Sub 

热点排行