赞
踩
static void Drag(int startX, int startY, int endX, int endY)
{
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
var sim = new InputSimulator();
sim.Mouse.MoveMouseToPositionOnVirtualDesktop(Convert.ToDouble(startX * 65535 / screenWidth), Convert.ToDouble(startY * 65535 / screenHeight));
sim.Mouse.LeftButtonDown();
Thread.Sleep(1000);
sim.Mouse.MoveMouseTo(Convert.ToDouble(endX * 65535 / screenWidth), Convert.ToDouble(endY * 65535 / screenHeight));
Thread.Sleep(1000);
sim.Mouse.LeftButtonUp();
}
using System; using System.Runtime.InteropServices; using System.Text; namespace 鼠标操作 { class Program { static void Main(string[] args) { //硬件鼠标操作 //MouseHelp.POINT pOINT = new MouseHelp.POINT(); //MouseHelp.GetCursorPos(out pOINT); //MouseHelp.mouse_event(MouseHelp.MOUSEEVENTF_LEFTDOWN, pOINT.X, pOINT.Y, 0, 0); // MouseHelp.mouse_event(MouseHelp.MOUSEEVENTF_LEFTUP, pOINT.X, pOINT.Y, 0, 0); //鼠标软模拟操作 https://stackoverflow.com/questions/3443325/sendmessage-wm-lbuttondown-up-works-on-buttons-but-not-window //https://blog.csdn.net/Ink_cherry/article/details/70210746 // MouseHelp.SendMessage(Control.hWnd, MouseHelp.WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero); Console.ReadKey(); } } public class MouseHelp { /** {鼠标软模拟:好处就是不会真的移动鼠标 开始按钮 坐标 x=386y=387 } sendmessage(hookHwnd,messages.WM_LBUTTONDOWN ,0,$0180017A); {按下鼠标左键} sendmessage(hookHwnd,messages.WM_LBUTTONUP ,0, $0180017A); {抬起鼠标左键} {硬件模拟:会真的移动鼠标} mouse_event(MOUSEEVENTF_LEFTDOWN,X ,Y ,0,0); mouse_event(MOUSEEVENTF_LEFTUP,X ,Y ,0,0); * **/ #region 硬件模拟鼠标键值 https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mouse_event /// <summary> /// dx和dy参数包含标准化的绝对坐标。 如果未设置,则这些参数包含相对数据:自上次报告的位置以来的位置变化。 无论将哪种类型的鼠标或类似鼠标的设备连接到系统,都可以设置或不设置此标志。 有关鼠标相对运动的更多信息,请参见下面的“备注”部分。 /// </summary> public const int MOUSEEVENTF_ABSOLUTE = 0x8000; /// <summary> /// 左按钮按下。 /// </summary> public const int MOUSEEVENTF_LEFTDOWN = 0x0002; /// <summary> /// 左按钮向上。 /// </summary> public const int MOUSEEVENTF_LEFTUP = 0x0004; /// <summary> /// 中间按钮按下。 /// </summary> public const int MOUSEEVENTF_MIDDLEDOWN = 0x20; /// <summary> /// 中间按钮向上。 /// </summary> public const int MOUSEEVENTF_MIDDLEUP = 0x40; /// <summary> /// 鼠标移动 /// </summary> public const int MOUSEEVENTF_MOVE = 0x1; /// <summary> /// 右按钮按下。 /// </summary> public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; /// <summary> /// 右侧按钮向上。 /// </summary> public const int MOUSEEVENTF_RIGHTUP = 0x0010; /// <summary> /// 如果鼠标带有滚轮,则滚轮已移动。 移动量在dwData中指定 //滚轮按钮被旋转。 /// </summary> public const int MOUSEEVENTF_WHEEL = 0x800; /// <summary> /// 按下了X按钮。 /// </summary> public const int MOUSEEVENTF_XDOWN = 0x0080; /// <summary> /// X按钮被释放。 /// </summary> public const int MOUSEEVENTF_XUP = 0x0100; /// <summary> /// 滚轮按钮倾斜。 /// </summary> public const int MOUSEEVENTF_HWHEEL = 0x01000; #endregion 硬件模拟鼠标键值 #region 软鼠标键值 https://docs.microsoft.com/zh-CN/windows/win32/inputdev/mouse-input /// <summary> /// 鼠标移动 /// </summary> public const int WM_MOUSEMOVE = 0x0200; /// <summary> /// 双击鼠标左键 /// </summary> public const int WM_LBUTTONDBLCLK = 0x0203; /// <summary> /// 按下鼠标左键 /// </summary> public const int WM_LBUTTONDOWN = 0x0201; /// <summary> /// 释放鼠标左键 /// </summary> public const int WM_LBUTTONUP = 0x0202; /// <summary> /// 双击鼠标中键 /// </summary> public const int WM_MBUTTONDBLCLK = 0x0209; /// <summary> /// 按下鼠标中键 /// </summary> public const int WM_MBUTTONDOWN = 0x0207; /// <summary> /// 释放鼠标中键 /// </summary> public const int WM_MBUTTONUP = 0x0208; /// <summary> /// 双击鼠标右键 /// </summary> public const int WM_RBUTTONDBLCLK = 0x0206; /// <summary> /// 按下鼠标右键 /// </summary> public const int WM_RBUTTONDOWN = 0x0204; /// <summary> /// 释放鼠标右键 /// </summary> public const int WM_RBUTTONUP = 0x0205; /// <summary> /// 当用户在窗口的客户区域中双击第一个或第二个X按钮时,将发布该消息。如果未捕获鼠标,则消息将发布到光标下方的窗口中。否则,该消息将发布到捕获鼠标的窗口中。 /// </summary> public const int WM_XBUTTONDBLCLK = 0x020D; /// <summary> /// 当光标在窗口的工作区中时用户按下第一个或第二个X按钮时发布。如果未捕获鼠标,则消息将发布到光标下方的窗口中。否则,该消息将发布到捕获鼠标的窗口中。 /// </summary> public const int WM_XBUTTONDOWN = 0x020B; /// <summary> /// 当光标在窗口的客户区域中时,用户释放第一个X或第二个X按钮时发布。如果未捕获鼠标,则消息将发布到光标下方的窗口中。否则,该消息将发布到捕获鼠标的窗口中。 /// </summary> public const int WM_XBUTTONUP = 0x020C; /// <summary> /// 旋转鼠标滚轮时发送到焦点窗口。该DefWindowProc函数功能将消息传播到窗口的父。不应在内部转发消息,因为DefWindowProc会将消息沿父链传播,直到找到处理该消息的窗口为止。 /// </summary> public const int WM_MOUSEWHEEL = 0x020A; #endregion 软鼠标键值 #region 其他数据操作键值--剪切板、粘贴板 https://docs.microsoft.com/en-us/windows/win32/dataxchg/wm-paste public const int WM_COPYDATA = 0x004A; public const int WM_PASTE = 0x0302; public const int WM_CHAR = 0x102; public const int WM_SETTEXT = 0x0C; public const int WM_KEYDOWN = 0x0100; public const int WM_KEYUP = 0x0101; public const int WM_CLOSE = 0x0010; public const int VK_END = 0x23; public const int VK_BACK = 0x08; public const int VK_RETURN = 0x0D; public const int VK_Delete = 0x2E; #endregion 其他数据操作键值 public const int SM_CXSCREEN = 0; public const int SM_CYSCREEN = 1; [DllImport("user32.dll", EntryPoint = "SetCursorPos")] public static extern int SetCursorPos(int x, int y); [DllImport("user32.dll", EntryPoint = "mouse_event")] public static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo); [DllImport("user32.dll", EntryPoint = "keybd_event")] public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo); [DllImport("user32.dll", EntryPoint = "SendMessageW")] public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string strclassName, string strWindowText); [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")] public static extern long GetPrivateProfileString(string strSection, string strKey, string strDef, StringBuilder sbBuffer, int iSize, string strFilePath); /// <summary> /// 获取屏幕分辩率 /// </summary> /// <param name="nIndex"></param> /// <returns></returns> [DllImport("user32.dll")] static extern int GetSystemMetrics(int nIndex); /// <summary> /// 获取鼠标当前位置 /// </summary> /// <param name="lpPoint"></param> /// <returns></returns> [DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT lpPoint); [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT(int x, int y) { this.X = x; this.Y = y; } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //int x = this.Location.X+18; //int y = this.Location.Y+18; //int at = MouseHelp.SendMessage(this.Handle, MouseHelp.WM_LBUTTONDOWN, IntPtr.Zero, new IntPtr(MAKELPARAM(x, y))); //for (int i = 0; i < 1000; i++) //{ // x++; y++; // int t = MouseHelp.SendMessage(this.Handle, MouseHelp.WM_MOUSEMOVE, IntPtr.Zero, new IntPtr(MAKELPARAM(x, y))); //} //at = MouseHelp.SendMessage(this.Handle, MouseHelp.WM_LBUTTONUP, IntPtr.Zero, new IntPtr(MAKELPARAM(x, y))); var notepad = Process.GetProcessesByName("Notepad2_x64").FirstOrDefault(); if (notepad != null) { //var edit = FindWindowEx(notepad.MainWindowHandle, IntPtr.Zero, "Edit", null); //PerformRightClick(edit, new Point(20, 20)); PerformRightClick(notepad.MainWindowHandle, new Point(20, 20)); } IntPtr maindHwnd = FindWindow(null, "QQ"); //获得QQ登陆框的句柄 if (maindHwnd != IntPtr.Zero) { IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null, "QQ"); //获得按钮的句柄 PerformRightClick(maindHwnd, new Point(20, 20)); //移动 } else { MessageBox.Show("没有找到窗口"); } } private int MakeLParam(int p, int p_2) { return ((p_2 << 16) | (p & 0xFFFF)); } [DllImport("user32.dll")] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,string lpszClass, string lpszWindow); [DllImport("User32.dll", EntryPoint = "FindWindow")] public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); void PerformRightClick(IntPtr hwnd, Point point) { var pointPtr = MakeLParam(point.X, point.Y); int a = MouseHelp.SendMessage(hwnd, MouseHelp.WM_MOUSEMOVE, IntPtr.Zero, new IntPtr( pointPtr)); a = MouseHelp.SendMessage(hwnd, MouseHelp.WM_RBUTTONDOWN, IntPtr.Zero, new IntPtr(pointPtr)); a = MouseHelp.SendMessage(hwnd, MouseHelp.WM_RBUTTONUP, IntPtr.Zero, new IntPtr(pointPtr)); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。