如何模拟可视化单击表单中的按钮(WinForms)?
我不是说:
Button_Press(MyButton, new KeyPressEventArgs());
我希望用户看到(视觉上)被点击的按钮。
我当然不想用
SendKeys.Send("{ENTER}")或其他类似的功能。
发布于 2010-07-01 16:31:31
如果使用RadioButton而不是普通按钮,则可以将其.Appearance属性设置为" Button“,然后从其他地方修改其.Checked属性。
例如:
this.radioButton1.Appearance = Appearance.Button;然后打电话:
this.radioButton1.Checked = true;或
this.radioButton1.Checked = false;它看起来就像一个普通的按钮。
发布于 2011-04-11 10:07:50
Button1.PerformClick很简单的一个班轮。这就是你要的。
发布于 2010-07-01 16:50:22
没有干净的方法可以做到这一点。我知道的唯一方法是使用来自mouse_event的user32.dll函数。这还要求您暂时将光标移动到所需的位置,执行单击,然后将其移回。
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy,
long cButtons, long dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public void ClickMouseLeftButton(Point globalLocation)
{
Point currLocation = Cursor.Position;
Cursor.Position = globalLocation;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,
globalLocation.X, globalLocation.Y, 0, 0);
Cursor.Position = currLocation;
}
public void ClickControl(Control target, Point localLocation)
{
ClickMouseLeftButton(target.PointToScreen(localLocation));
}
public void ClickControl(Control target)
{
ClickControl(target, new Point(target.Width / 2, target.Height / 2));
}或者,您可以将其转换为扩展方法:
public static class ControlExtensions
{
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(long dwFlags, long dx, long dy,
long cButtons, long dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
private static void ClickMouseLeftButton(Point globalLocation)
{
Point currLocation = Cursor.Position;
Cursor.Position = globalLocation;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,
globalLocation.X, globalLocation.Y, 0, 0);
Cursor.Position = currLocation;
}
public static void ClickMouse(this Control target, Point localLocation)
{
ClickMouseLeftButton(target.PointToScreen(localLocation));
}
public static void ClickMouse(this Control target)
{
ClickMouse(target, new Point(target.Width / 2, target.Height / 2));
}
}这将允许您调用controlName.ClickMouse();
https://stackoverflow.com/questions/3159651
复制相似问题