c# - c# 我可以将 EventArgs 转换为 MouseEventArgs 吗?
问题描述
我认为这与铸造有关:
首先,我为我的图片框声明一个事件处理程序:
pictureBox1.MouseHover += new EventHandler(this.pictureBox1_MouseHover);
接下来我试图检查鼠标左键是否被按下:
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
if (e.Button == MouseButtons.Left)
{
/*some gibberish for when the left mouse button is held*/
}
}
但这不起作用,因为EventArgs
不是MouseEventArgs
我可以Cast
或以某种方式转换EventArgs e
为被视为 MouseEventArgs 以便上面的代码可以工作吗?
解决方案
你能把苹果变成橘子吗?不。好吧,你不能只MouseEventArgs
从一个EventArgs
实例中创建一个。
在这种情况下,您的代码没有意义。您正在尝试获取悬停事件的按钮。无需单击任何按钮即可完成悬停。如果您想知道悬停时按下的按钮,则需要先缓存MouseDown
和MouseUp
事件以注册单击了哪个按钮。
推荐阅读
- ios - 在 iPad Pro 9.7" 上使用前置摄像头以 60 FPS 的速度拍摄视频
- deepsecurity - 搜索字段 lastIPUsed 不可搜索
- datetime - 在 Mule 4 中转换日期(Salesforce 到 Oracle)
- javascript - 防止默认();对 drop 事件没有任何影响
- php - 在上传之前发布输入文件并重命名
- javafx - 您如何运行 jlink 为您的 JavaFX 应用程序创建运行时?
- javascript - Vue Js 在 vue-js-modal 中打开画板
- html - 保持物体居中而不移动
- html - 单击播放后,音频被阻止“自动播放”?
- reactjs - 根据其他两个字段设置 redux-form 字段值