林烈涛 2021-06-07 02:34 采纳率: 50%
浏览 89

C#模拟点击拼多多后台无反应,如何解决

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);//左键-按下

        }
    }
}

这个代码没有模拟鼠标点击,怎么修改都没有反应,这个如何处理

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-11-05 05:59
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM