由于WinForm无法直接操作别的软件的下拉框,需要使用Windows API来模拟点击操作。下面是一个示例代码,演示如何在C# WinForm程序中模拟点击别的软件下拉框并选择子项。

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinFormDropdownSimulator
{
    public partial class Form1 : Form
    {
        // 定义Windows API函数
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

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

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

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

        [DllImport("user32.dll")]
        private static extern int GetComboBoxInfo(IntPtr hwndCombo, ref COMBOBOXINFO pcbi);

        // ComboBox信息结构体
        [StructLayout(LayoutKind.Sequential)]
        private struct COMBOBOXINFO
        {
            public int cbSize;
            public RECT rcItem;
            public RECT rcButton;
            public IntPtr stateButton;
            public IntPtr hwndCombo;
            public IntPtr hwndEdit;
            public IntPtr hwndList;
        }

        // 矩形结构体
        [StructLayout(LayoutKind.Sequential)]
        private struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;

            public int Width { get { return right - left; } }
            public int Height { get { return bottom - top; } }
        }

        // Windows消息常量
        private const uint WM_COMMAND = 0x0111;
        private const uint CB_SHOWDROPDOWN = 0x014F;
        private const uint CB_GETCOUNT = 0x0146;
        private const uint CB_GETLBTEXTLEN = 0x0149;
        private const uint CB_GETLBTEXT = 0x0148;
        private const uint CB_SETCURSEL = 0x014E;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSimulate_Click(object sender, EventArgs e)
        {
            // 获取目标窗口句柄
            IntPtr hwndParent = FindWindow(null, "目标窗口标题");
            if (hwndParent == IntPtr.Zero)
            {
                MessageBox.Show("找不到目标窗口!");
                return;
            }

            // 获取ComboBox句柄
            IntPtr hwndCombo = FindWindowEx(hwndParent, IntPtr.Zero, "ComboBox", "下拉框标题");
            if (hwndCombo == IntPtr.Zero)
            {
                MessageBox.Show("找不到ComboBox!");
                return;
            }

            // 显示下拉框
            SendMessage(hwndCombo, CB_SHOWDROPDOWN, IntPtr.One, IntPtr.Zero);

            // 获取ComboBox信息
            COMBOBOXINFO info = new COMBOBOXINFO();
            info.cbSize = Marshal.SizeOf(info);
            GetComboBoxInfo(hwndCombo, ref info);

            // 获取子项数量
            int itemCount = (int)SendMessage(info.hwndList, CB_GETCOUNT, IntPtr.Zero, IntPtr.Zero);
            if (itemCount == 0)
            {
                MessageBox.Show("下拉框没有子项!");
                return;
            }

            // 获取子项文本
            for (int i = 0; i < itemCount; i++)
            {
                int textLength = (int)SendMessage(info.hwndList, CB_GETLBTEXTLEN, (IntPtr)i, IntPtr.Zero);
                if (textLength > 0)
                {
                    IntPtr buffer = Marshal.AllocHGlobal((textLength + 1) * 2);
                    SendMessage(info.hwndList, CB_GETLBTEXT, (IntPtr)i, buffer);

                    string itemText = Marshal.PtrToStringUni(buffer);
                    if (itemText == "目标子项文本")
                    {
                        // 选择目标子项
                        SendMessage(hwndCombo, CB_SETCURSEL, (IntPtr)i, IntPtr.Zero);
                        break;
                    }

                    Marshal.FreeHGlobal(buffer);
                }
            }

            // 隐藏下拉框
            PostMessage(hwndCombo, WM_COMMAND, IntPtr.Zero, info.hwndList);
        }
    }
}

在代码中,我们使用了以下Windows API函数:

  • FindWindow:查找指定窗口句柄。
  • FindWindowEx:查找指定窗口的子窗口句柄。
  • PostMessage:向指定窗口发送消息。
  • SendMessage:向指定窗口发送消息,并等待窗口处理完毕。
  • GetComboBoxInfo:获取ComboBox的信息。

首先,我们使用FindWindow函数查找目标窗口句柄。然后,使用FindWindowEx函数查找ComboBox句柄。接着,我们使用SendMessage函数发送CB_SHOWDROPDOWN消息,显示下拉框。然后,使用GetComboBoxInfo函数获取ComboBox的信息,包括子窗口句柄。接下来,我们使用SendMessage函数发送CB_GETCOUNT消息,获取子项数量。然后,使用SendMessage函数发送CB_GETLBTEXTLENCB_GETLBTEXT消息,获取每个子项的文本。如果找到目标子项文本,我们使用SendMessage函数发送CB_SETCURSEL消息,选择目标子项。最后,我们使用PostMessage函数发送WM_COMMAND消息,隐藏下拉框。

注意,根据实际情况,需要修改代码中的目标窗口标题、下拉框标题和目标子项文本。此外,需要将代码放在WinForm应用程序的事件处理程序中,例如按钮的Click事件处理程序。

C# WinForm 模拟点击下拉框选择子项代码示例

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

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