c# - 执行单击图片框
问题描述
到目前为止,我在单击另一个按钮后一直使用单击按钮的方法。(我单击按钮“A”并激活按钮“B”)
buttonDeleteFields.PerformClick();
现在,因为我创新了设计,我已经用图标(图片框)替换了这个按钮,但是带有图片框的 PerformClick 方法不起作用。你能给我一个解决这个问题的方法吗?(我单击按钮“A”并激活图片框“B”)
解决方案
要完全模拟 Click 事件(就像用户点击了图片框一样),
将这些添加到您的表单类中:
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private int WM_LBUTTONDOWN = 0x0201;
private int WM_LBUTTONUP = 0x0202;
并在您的按钮单击事件中这样调用:
private void button1_Click(object sender, EventArgs e)
{
// OLD CODE: Used to perform click on button 2,
// before it was changed to a picture box
// buttonDeleteFields.PerformClick();
// new code, emulating picture box click:
SendMessage(pictureBoxDeleteFields.Handle, WM_LBUTTONDOWN, 0, 1);
SendMessage(pictureBoxDeleteFields.Handle, WM_LBUTTONUP, 0, 0);
}
推荐阅读
- c# - 通过将“[Serializable]”属性添加到 C# 类,消息大小可以增加多少?
- laravel - 在不选择图像的情况下更新会出错(Laravel)
- javascript - 如何从 JSON 对象填充列表并创建树视图 - JavaScript
- html - Flex 项目不在 IE 中居中
- kubernetes - Kubernetes 中 hostPath 的缺点
- docker - Docker撰写部署停机时间与capistrano?
- javascript - 从 InAppBrowser 读取 localStorage
- lua - (LUA) 需要帮助将命令发送到 Web 服务器
- python - 将 python 脚本翻译成 qt 或 c++
- selenium - 按子级文本选择节点