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)
{
Console. ReadKey ( ) ;
}
}
public class MouseHelp
{
#region 硬件模拟鼠标键值 https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mouse_event
public const int MOUSEEVENTF_ABSOLUTE = 0x8000 ;
public const int MOUSEEVENTF_LEFTDOWN = 0x0002 ;
public const int MOUSEEVENTF_LEFTUP = 0x0004 ;
public const int MOUSEEVENTF_MIDDLEDOWN = 0x20 ;
public const int MOUSEEVENTF_MIDDLEUP = 0x40 ;
public const int MOUSEEVENTF_MOVE = 0x1 ;
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008 ;
public const int MOUSEEVENTF_RIGHTUP = 0x0010 ;
public const int MOUSEEVENTF_WHEEL = 0x800 ;
public const int MOUSEEVENTF_XDOWN = 0x0080 ;
public const int MOUSEEVENTF_XUP = 0x0100 ;
public const int MOUSEEVENTF_HWHEEL = 0x01000 ;
#endregion 硬件模拟鼠标键值
#region 软鼠标键值 https://docs.microsoft.com/zh-CN/windows/win32/inputdev/mouse-input
public const int WM_MOUSEMOVE = 0x0200 ;
public const int WM_LBUTTONDBLCLK = 0x0203 ;
public const int WM_LBUTTONDOWN = 0x0201 ;
public const int WM_LBUTTONUP = 0x0202 ;
public const int WM_MBUTTONDBLCLK = 0x0209 ;
public const int WM_MBUTTONDOWN = 0x0207 ;
public const int WM_MBUTTONUP = 0x0208 ;
public const int WM_RBUTTONDBLCLK = 0x0206 ;
public const int WM_RBUTTONDOWN = 0x0204 ;
public const int WM_RBUTTONUP = 0x0205 ;
public const int WM_XBUTTONDBLCLK = 0x020D ;
public const int WM_XBUTTONDOWN = 0x020B ;
public const int WM_XBUTTONUP = 0x020C ;
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) ;
[ DllImport ( "user32.dll" ) ]
static extern int GetSystemMetrics ( int nIndex) ;
[ 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)
{
var notepad = Process. GetProcessesByName ( "Notepad2_x64" ) . FirstOrDefault ( ) ;
if ( notepad != null )
{
PerformRightClick ( notepad. MainWindowHandle, new Point ( 20 , 20 ) ) ;
}
IntPtr maindHwnd = FindWindow ( null , "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) ) ;
}
}
}