如何判断哪个物理设备点击了 C# 中的按钮?

发布于 2024-09-16 07:40:12 字数 88 浏览 4 评论 0原文

我有一个带有按钮的表单。该应用程序旨在在触摸屏计算机上运行。单击按钮时,我想知道是通过鼠标单击还是通过触摸屏单击。

这可能吗?如果是这样,怎么办?

I have a Form with buttons. This application is meant to run on a touchscreen computer. When a button is clicked I would like to know whether it was clicked by the mouse or the touchscreen.

Is this possible? If so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2024-09-23 07:40:12
private void button_Click(object sender, EventArgs e)
{
    try
    {
        ((MouseEventArgs)e).Button.ToString();
    }
    catch(Exception)
    {
        //If an exception is catch, it means the mouse was not used.
    }
}

这是一种粗俗的方法,因为只要按钮被鼠标之外的其他东西“单击”,无论是触摸屏还是键盘的返回,它都会捕获异常。但它会完成这项工作:)

private void button_Click(object sender, EventArgs e)
{
    try
    {
        ((MouseEventArgs)e).Button.ToString();
    }
    catch(Exception)
    {
        //If an exception is catch, it means the mouse was not used.
    }
}

This is a gross way to do it, because it will catch an exception anytime button is "clicked" by something else than the mouse, either touchscreen or keyboard's return. But it will do the job :)

梦归所梦 2024-09-23 07:40:12

以这种方式从 Wildhorn 的答案中获取可以避免抛出异常:

MouseEventArgs thisObject = e as MouseEventArgs

if(thisObject != null)
{
//Do Something
}

这没有太大不同,但您没有异常处理的开销。您也可以尝试多次演员表,直到获得正确的演员表。

Taking from Wildhorn's answer this way avoids the exceptions being thrown:

MouseEventArgs thisObject = e as MouseEventArgs

if(thisObject != null)
{
//Do Something
}

It's not much different but you don't have the overhead of the exception handling. Also you could try multiple casts til you got the right one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文