求助,用vba代码控制鼠标,用到mouseeventf_absolute在win10里不好用了是为什么呢

a1247618246 2019-10-30 10:29:15
求助,用vba代码控制鼠标,用到mouseeventf_absolute在win10里不好用了是为什么呢,怎么解决
...全文
517 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
a1247618246 2019-10-31
  • 打赏
  • 举报
回复
引用 3 楼 脆皮大雪糕的回复:
mouse_event 函数的dwFlags 参数传入的为Long 长度4个字节 ,但API声明时时 MOUSEEVENTF_ABSOLUTE = &H8000 系统默认为2字节整形,MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE 得到的是一个负数,然后再以byval的方式传递参数过去,API收到的并不是 &H00008001 而是&H80000001 所以出乱子了。
太感谢了,问题终于解决了
脆皮大雪糕 2019-10-30
  • 打赏
  • 举报
回复
mouse_event 函数的dwFlags 参数传入的为Long 长度4个字节 ,但API声明时时 MOUSEEVENTF_ABSOLUTE = &H8000 系统默认为2字节整形,MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE 得到的是一个负数,然后再以byval的方式传递参数过去,API收到的并不是 &H00008001 而是&H80000001 所以出乱子了。
脆皮大雪糕 2019-10-30
  • 打赏
  • 举报
回复
模拟键盘鼠标操作飞信界面发送短信 private void SendSMS() { #if DEBUG string[] addr = new string[] { "闻佃来"}; #else string[] addr = new string[] { "张三", "李四"}; #endif string message = " "; if (message == "") return; Rectangle rect = new Rectangle(); Windows wins = new Windows(); IntPtr hwin = IntPtr.Zero; foreach (Window w in wins) { if (w.Title.IndexOf("发短信") >= 0) { hwin = w.hWnd; break; } } //未找到发送窗口,点击主窗口下的短信发送按钮 if (hwin == IntPtr.Zero) { wins = new Windows(); foreach (Window w in wins) { if (w.Title.IndexOf("飞信2013") >= 0) { //唤醒屏保 Win32.SetCursorPos(10,10); Win32.mouse_event(Win32.MOUSEEVENTF_MOVE, 50, 50, 0, 0); Thread.Sleep(1500); Win32.ShowWindow(w.hWnd, Win32.SW_SHOWNORMAL); Win32.SetForegroundWindow(hwin); Win32.GetWindowRect(w.hWnd, ref rect); IntPtr hDesk = Win32.GetDesktopWindow(); Rectangle rdesk = new Rectangle(); Win32.GetWindowRect(hDesk,ref rdesk); if (rect.Y < 0) { //上 Win32.SetCursorPos(rect.X +(rect.Width-rect.X)/2,0); Win32.mouse_event(Win32.MOUSEEVENTF_MOVE, 0, 1, 0, 0); Thread.Sleep(1500); Win32.GetWindowRect(w.hWnd, ref rect); } else if (rect.Y >= rdesk.Height - 3) { //下 Win32.SetCursorPos(rect.X + (rect.Width - rect.X) / 2,rdesk.Height); Win32.mouse_event(Win32.MOUSEEVENTF_MOVE, 0, -1, 0, 0); Thread.Sleep(1500); Win32.GetWindowRect(w.hWnd, ref rect); } else if (rect.X <= 0) { //左边 Win32.SetCursorPos(0, rect.Y + (rect.Height - rect.Y) / 2); Thread.Sleep(2000); Win32.mouse_event(Win32.MOUSEEVENTF_MOVE, 1, 0, 0, 0); Thread.Sleep(1500); Win32.GetWindowRect(w.hWnd, ref rect); } else if (rect.X >= rdesk.Width - 3) { //右边 Win32.SetCursorPos(rdesk.Width, rect.Y + (rect.Height - rect.Y) / 2); Thread.Sleep(2000); Win32.mouse_event(Win32.MOUSEEVENTF_MOVE, -1, 0, 0, 0); Thread.Sleep(1500); Win32.GetWindowRect(w.hWnd, ref rect); }else { Win32.MoveWindow(w.hWnd, rect.Left, 0, rect.Width - rect.Left, rect.Height - rect.Top, true); Win32.GetWindowRect(w.hWnd, ref rect); Thread.Sleep(300); } Win32.SetCursorPos(rect.X+70,rect.Height-50); Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN | Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); Thread.Sleep(2000); break; } } //再次查找发送窗口 wins = new Windows(); foreach (Window w in wins) { if (w.Title.IndexOf("发短信") >= 0) { hwin = w.hWnd; break; } } } //查询发送窗口成功 if (hwin != IntPtr.Zero) { Win32.ShowWindow(hwin, Win32.SW_SHOWNORMAL); Win32.SetForegroundWindow(hwin); Win32.GetWindowRect(hwin, ref rect); IntPtr hwndtel = Win32.WindowFromPoint(rect.Left + 120, rect.Top + 45); IntPtr hwndsend = Win32.WindowFromPoint(rect.Left + 50, rect.Height - 60); //单击地址栏 Win32.SetCursorPos(rect.Left + 120, rect.Top + 45); Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN | Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); Thread.Sleep(1000); //加入地址 for (int i = 0; i < addr.Length; i++) { Clipboard.SetText(addr[i]); Thread.Sleep(200); Win32.SendMessage(hwndtel, Win32.WM_PASTE, 0, 0); Thread.Sleep(300); Win32.SendMessage(hwndtel, Win32.WM_KEYDOWN, 0X0D, 0);//发 Win32.SendMessage(hwndtel, Win32.WM_KEYUP, 0X0D, 0); //送 Win32.SendMessage(hwndtel, Win32.WM_CHAR, 0X0D, 0); //回车 Thread.Sleep(300); } //加入消息文本 Clipboard.SetText(message); Thread.Sleep(200); Win32.SendMessage(hwndsend, Win32.WM_PASTE, 0, 0); Thread.Sleep(200); #if DEBUG #else if (!_TestMode) { Win32.SendMessage(hwndsend, Win32.WM_KEYDOWN, 0X0D, 0); Win32.SendMessage(hwndsend, Win32.WM_KEYUP, 0X0D, 0); Win32.SendMessage(hwndsend, Win32.WM_CHAR, 0X0D, 0); } #endif textBox1.AppendText("---------------------------------------- \n"); textBox1.AppendText(DateTime.Now + ":"+ message + "\n"); } else { textBox1.AppendText("无法打开飞信短信发送窗口\n"); } _LastSend = DateTime.Now; LastSendTime = _LastSend; }
鼠标模拟键盘.frm Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long '这个是设置鼠标的位置! Private Declare Function CreateDCA& Lib "gdi32" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Long) 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 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 GetCursorPos Lib "user32" (lpPoint As pointapi) As Long 'Private Declare Function CreateDCA& Lib "gdi32" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Long) Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Private Type pointapi x As Long y As Long End Type Dim mx, my Private Sub Command1_Click() x = Int(Rnd(1) * 500) y = Int(Rnd(1) * 500) Call SetCursorPos(x, y) '让鼠标移动到(10,20) 'mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0& '模拟鼠标点击 mouse_event LEFTDOWN_RIGHTDOWN, 0, 0, 0, 0 '//模拟按下鼠标右键。 End Sub Private Sub Command2_Click() Timer2.Interval = 0 mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '模拟鼠标的左键单击! End Sub Private Sub Command3_Click() End End Sub Private Sub Form_Load() '定义鼠标事件 '上面的是声明部分.只有声明了,才可以使用.. '代码部分 Call SetCursorPos(580, 20) '让鼠标移动到(10,20) mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '模拟鼠标的左键单击! End Sub Private Sub Timer1_Timer() x = Int(Rnd(1) * 500) y = Int(Rnd(1) * 500) Call SetCursorPos(x, y) '让鼠标移动到(10,20) mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 '模拟鼠标的左键单击! mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '模拟鼠标的左键单击! End Sub ''为 了 指 定 那 些 与 SHIFT、 CTRL 及 ALT 等 按 键 结 合 的 组 合 键 , 可 在 这 些 按 键 码 的 前 面 放 置 一 个 或 多 个 代 码 , 这 些 代 码 列 举 如 下 : '按 键 代 码 'SHIFT + 'CTRL ^ 'ALT % '{PRTSC} ' 为 了 说 明 在 按 下 其 它 按 键 时 应 同 时 按 下 SHIFT、 CTRL、 及 ALT 的 任 意 组 合 键 , 请 把 那 些 按 键 的 码 放 在 括 号 当 中 。 例 如 , 为 了 说 明 按 下 E 与 C 的 时 候 同 时 按 下 SHIFT 键 , 请 使 用 "+(EC)"。 为 了 说 明 在 按 下 E 的 时 候 同 时 按 下 SHIFT 键 , 但 接 着 按 C 而 不 按 SHIFT, 则 使 用 "+EC"。 '对 SendKeys 来 说 , 加 号 (+)、 插 入 符 (^)、 百 分 比 符 号 (%)、 上 划 线 (~) 及 圆 括 号 ( ) 都 具 有 特 殊 意 义 。 为 了 指 定 上 述 任 何 一 个 字 符 , 要 将 它 放 在 大 括 号 ({}) 当 中 。 例 如 , 要 指 定 正 号 , 可 用 {+} 表 示 。 方 括 号 ([ ]) 对 SendKeys 来 说 并 不 具 有 特 殊 意 义 , 但 必 须 将 它 们 放 在 大 括 号 中 。 在 其 它 应 用 程 序 中 , 方 括 号 有 特 殊 意 义 , 在 出 现 动 态 数 据 交 换 (DDE) 的 时 候 , 它 可 能 具 有 重 要 意 义 。 为 了 指 定 大 括 号 字 符 , 请 使 用 {{} 及 {}}。 '另 外 , 参 考 Sendkeys的 帮 助 , 可 以 找 到 其 他 一 些 特 殊 键 的 传 递 方 法 。 'SendKeys "^B" 'SendKeys ("{PRTSC}") Private Sub Timer2_Timer() Dim a As Long Dim p As pointapi a = CreateDCA("DISPLAY", vbNullString, vbNullString, 0) GetCursorPos p 'Command1.Caption = GetPixel(a, p.x, p.y) 'h获取颜色值 If p.x = mx Or p.y = my Then GoTo 50 If p.x > mx Then GoTo 10 If p.x < mx Then GoTo 20 GoTo 50 10 SendKeys "右" GoTo 50 20 SendKeys "左" GoTo 50 30 SendKeys "下" GoTo 60 40 SendKeys "上" GoTo 60 SendKeys "B" SendKeys p.x '坐标 SendKeys "a" SendKeys p.y 50 If p.y > my Then GoTo 30 If p.y < my Then GoTo 40 60 mx = p.x my = p.y End Sub

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧