由于 WinForm 是一个桌面应用程序开发平台,而模拟点击其他软件的下拉框需要使用一些 API 函数来实现,因此在 WinForm 中实现模拟点击其他软件的下拉框需要通过调用 Windows API 函数来实现。具体实现方式如下:

  1. 获取目标窗口的句柄。

可以使用 FindWindow 或 FindWindowEx 函数来获取目标窗口的句柄。如果目标窗口是一个下拉框,可以使用 GetComboBoxInfo 函数来获取下拉框的句柄。

  1. 发送消息模拟鼠标点击。

使用 SendMessage 或 PostMessage 函数来向目标窗口发送鼠标点击消息。具体需要发送哪种消息,可以根据目标窗口的类型和功能来判断。

例如,如果目标窗口是一个下拉框,可以使用 CB_SHOWDROPDOWN 消息来打开下拉框,使用 CB_SELECTSTRING 或 CB_SETCURSEL 消息来选择下拉框中的项。

  1. 处理下拉框中的项。

如果需要选择下拉框中的某一项,可以使用 SendMessage 或 PostMessage 函数向下拉框窗口发送 CB_GETLBTEXT 消息或 CB_GETLBTEXTLEN 消息来获取下拉框中的所有项。然后使用 CB_SELECTSTRING 或 CB_SETCURSEL 消息来选择所需的项。

代码示例:

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, string lParam);

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

[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetComboBoxInfo(IntPtr hwndCombo, ref COMBOBOXINFO pcbi);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, StringBuilder lParam);

private const uint CB_SHOWDROPDOWN = 0x014F;
private const uint CB_SELECTSTRING = 0x014D;
private const uint CB_SETCURSEL = 0x014E;
private const int CB_GETLBTEXT = 0x0148;
private const int CB_GETLBTEXTLEN = 0x0149;

public void SimulateComboBoxClick(string windowTitle, string comboBoxName, string itemText)
{
    // 获取目标窗口的句柄
    IntPtr hWndParent = FindWindow(null, windowTitle);
    IntPtr hWndChild = FindWindowEx(hWndParent, IntPtr.Zero, "ComboBox", comboBoxName);

    if (hWndChild != IntPtr.Zero)
    {
        // 发送消息模拟鼠标点击
        SendMessage(hWndChild, CB_SHOWDROPDOWN, IntPtr.Zero, IntPtr.Zero);

        // 处理下拉框中的项
        COMBOBOXINFO info = new COMBOBOXINFO();
        info.cbSize = Marshal.SizeOf(info);
        GetComboBoxInfo(hWndChild, ref info);

        int count = SendMessage(info.hwndList, CB_GETCOUNT, IntPtr.Zero, IntPtr.Zero).ToInt32();
        for (int i = 0; i < count; i++)
        {
            StringBuilder sb = new StringBuilder(256);
            SendMessage(info.hwndList, CB_GETLBTEXT, i, sb);
            if (sb.ToString() == itemText)
            {
                SendMessage(hWndChild, CB_SETCURSEL, i, IntPtr.Zero);
                break;
            }
        }

        // 关闭下拉框
        SendMessage(hWndChild, CB_SHOWDROPDOWN, IntPtr.Zero, IntPtr.Zero);
    }
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}

[StructLayout(LayoutKind.Sequential)]
public struct COMBOBOXINFO
{
    public int cbSize;
    public RECT rcItem;
    public RECT rcButton;
    public int stateButton;
    public IntPtr hwndCombo;
    public IntPtr hwndEdit;
    public IntPtr hwndList;
}
WinForm 模拟点击其他软件下拉框代码详解

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

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