模拟鼠标点击问题 C#
我想做个 C#程序
程序运行时
自动 点击某网页的 固定坐标
50分求详细代码
[解决办法]
mouse_event
更方法的是使用WebBrowser控件
[解决办法]
up
[解决办法]
使用WebBrowser控件,在里面容易搞点
[解决办法]
[DllImport("user32.dll")] private static extern void mouse_event ( UInt32 dwFlags, // motion and click options UInt32 dx, // horizontal position or change UInt32 dy, // vertical position or change UInt32 dwData, // wheel movement IntPtr dwExtraInfo // application-defined information );private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002; private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004; public void Click() { m_Browser.Visible = true; Point pp = new Point(CurrentTask.X, CurrentTask.Y); Point screenPoint = m_MainForm.PointToScreen(pp); screenPoint.X += m_Browser.Left; screenPoint.Y += m_Browser.Top; Cursor.Position = screenPoint; mouse_event(MOUSEEVENTF_LEFTDOWN, (UInt32)pp.X, (UInt32)pp.Y, 0, new IntPtr()); for (int i = 0; i < 19; i++) { Thread.Sleep(20); Application.DoEvents(); } mouse_event(MOUSEEVENTF_LEFTUP, (UInt32)pp.X, (UInt32)pp.Y, 0, new IntPtr()); m_Browser.Visible = false; m_State = ClickerState.Clicked; m_CurrentTask = null; Thread.Sleep(100); //The element the browser clicked is not a link if(m_State != ClickerState.Navigating) { m_State = ClickerState.WaitingTask; Navigate(m_Manager.GetTask()); } }
[解决办法]
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace 点击{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("User32")] public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo); [DllImport("User32")] public extern static void SetCursorPos(int x, int y); [DllImport("User32")] public extern static bool GetCursorPos(out POINT p); [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; } public enum MouseEventFlags { Move = 0x0001, LeftDown = 0x0002, LeftUp = 0x0004, RightDown = 0x0008, RightUp = 0x0010, MiddleDown = 0x0020, MiddleUp = 0x0040, Wheel = 0x0800, Absolute = 0x8000 } private void AutoClick(int x, int y) { POINT p = new POINT(); GetCursorPos(out p); try { SetCursorPos(x, y); mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero); mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero); } finally { SetCursorPos(p.X, p.Y); } } private void button1_Click(object sender, EventArgs e) { AutoClick(20, 40); } }}
[解决办法]
自动化测试的应该很好做。我这由一个范例,如果想要你可以照着改改