winfrom实现模拟点击下拉框
- 创建下拉框控件
ComboBox comboBox1 = new ComboBox();
comboBox1.Items.Add("选项1");
comboBox1.Items.Add("选项2");
comboBox1.Items.Add("选项3");
comboBox1.Location = new Point(10, 10);
this.Controls.Add(comboBox1);
- 模拟鼠标点击下拉箭头
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
private const uint MOUSEEVENTF_LEFTUP = 0x04;
private void button1_Click(object sender, EventArgs e)
{
// 获取下拉箭头位置
Point point = new Point(comboBox1.Right - 15, comboBox1.Bottom - 15);
// 模拟鼠标点击下拉箭头
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)point.X, (uint)point.Y, 0, 0);
}
- 模拟鼠标点击下拉项
private const uint MOUSEEVENTF_MOVE = 0x0001;
private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
private const uint MOUSEEVENTF_LEFTUP = 0x0004;
private void button2_Click(object sender, EventArgs e)
{
// 获取下拉项位置
Point point = comboBox1.PointToScreen(new Point(5, 5));
// 模拟鼠标点击下拉项
mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, (uint)(65535 * point.X / Screen.PrimaryScreen.Bounds.Width), (uint)(65535 * point.Y / Screen.PrimaryScreen.Bounds.Height), 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
注意:在模拟鼠标点击下拉项时,需要将下拉框控件的位置转换为屏幕坐标。
原文地址: http://www.cveoy.top/t/topic/p6n 著作权归作者所有。请勿转载和采集!