using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string strClass, string strWindow);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体为活动窗体
//结构体布局 本机位置
[StructLayout(LayoutKind.Sequential)]
struct NativeRECT
{
public int left;
public int top;
public int right;
public int bottom;
}
//将枚举作为位域处理
[Flags]
enum ME : uint //设置鼠标动作的键值MouseEventFlag
{
Move = 0x0001, //发生移动
LeftDown = 0x0002, //鼠标按下左键
LeftUp = 0x0004, //鼠标松开左键
RightDown = 0x0008, //鼠标按下右键
RightUp = 0x0010, //鼠标松开右键
MiddleDown = 0x0020, //鼠标按下中键
MiddleUp = 0x0040, //鼠标松开中键
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800, //鼠标轮被移动
VirtualDesk = 0x4000, //虚拟桌面
Absolute = 0x8000
}
//设置鼠标按键和动作
[DllImport("user32.dll")]
static extern void mouse_event(ME flags, int dx, int dy, uint data, UIntPtr extraInfo); //UIntPtr指针多句柄类型
//设置鼠标位置
//[DllImport("user32.dll")]
//static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
private static extern int SetCursorPos(int x, int y);
private void button1_Click(object sender, EventArgs e)
{
IntPtr hwnd = FindWindow(null, "XXX店铺-拼多多工作台");
Thread.Sleep(500);
SetCursorPos(1406, 735);
SetForegroundWindow(hwnd);
Thread.Sleep(500);
mouse_event(ME.LeftDown, 0, 0, 0, UIntPtr.Zero);//左键-按下
Thread.Sleep(1000);
mouse_event(ME.LeftUp, 0, 0, 0, UIntPtr.Zero);//左键-按下
}
}
}
这个代码没有模拟鼠标点击,怎么修改都没有反应,这个如何处理