首页 > 解决方案 > 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 以便上面的代码可以工作吗?

标签: c#

解决方案


你能把苹果变成橘子吗?不。好吧,你不能只MouseEventArgs从一个EventArgs实例中创建一个。

在这种情况下,您的代码没有意义。您正在尝试获取悬停事件的按钮。无需单击任何按钮即可完成悬停。如果您想知道悬停时按下的按钮,则需要先缓存MouseDownMouseUp事件以注册单击了哪个按钮。


推荐阅读