要使用 InputSimulator 模拟点击别的软件下拉框,可以先使用 FindWindow 或 FindWindowEx 函数找到该窗口的句柄,然后使用 SendMessage 或 PostMessage 函数发送 WM_LBUTTONDOWN 和 WM_LBUTTONUP 消息模拟鼠标点击该下拉框,最后使用 SendMessage 或 PostMessage 函数发送 WM_KEYDOWN 和 WM_KEYUP 消息模拟按下和释放下箭头键,即可打开下拉菜单。

下面是一个示例代码:

using InputSimulatorStandard.Native;
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace SimulateClickComboBox
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        const uint WM_LBUTTONDOWN = 0x0201;
        const uint WM_LBUTTONUP = 0x0202;
        const uint WM_KEYDOWN = 0x0100;
        const uint WM_KEYUP = 0x0101;
        const int VK_DOWN = 0x28;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr hwndComboBox = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "ComboBox", "ComboBox1");
            if (hwndComboBox != IntPtr.Zero)
            {
                PostMessage(hwndComboBox, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
                PostMessage(hwndComboBox, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
                Thread.Sleep(100);
                PostMessage(hwndComboBox, WM_KEYDOWN, (IntPtr)VK_DOWN, IntPtr.Zero);
                PostMessage(hwndComboBox, WM_KEYUP, (IntPtr)VK_DOWN, IntPtr.Zero);
            }
            else
            {
                MessageBox.Show("找不到ComboBox1窗口");
            }
        }
    }
}

在这个示例中,我们假设要模拟点击一个名为'ComboBox1'的下拉框,在 button1_Click 事件处理程序中使用 FindWindowEx 函数找到该下拉框的句柄,然后发送 WM_LBUTTONDOWN 和 WM_LBUTTONUP 消息模拟鼠标点击该下拉框,接着使用 SendMessage 或 PostMessage 函数发送 WM_KEYDOWN 和 WM_KEYUP 消息模拟按下和释放下箭头键,即可打开下拉菜单。

C# 使用 InputSimulator 模拟点击其他软件下拉框

原文地址: https://www.cveoy.top/t/topic/lEhc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录