首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何区分触摸屏和普通触摸屏?

如何区分触摸屏和普通触摸屏?
EN

Stack Overflow用户
提问于 2015-03-24 00:10:09
回答 2查看 983关注 0票数 2

如果多个屏幕连接到一台计算机,我想在触摸屏上显示一个应用程序。通过对System.Windows.Forms.Screen.AllScreens进行迭代,我可以获得用于移动窗口的WorkingArea。但是,Screen不提供IsTouchscreen方法。

另一方面,通过迭代所有System.Windows.Input.Tablet.TabletDevices,我无法找到相应的Screen,因为Screen.DeviceNameTabletDevice.Name不匹配。

那么,有没有办法以某种方式将ScreenTabletDevice匹配,或者有没有其他我可以使用的变通办法?

EN

回答 2

Stack Overflow用户

发布于 2015-03-27 01:27:56

这些信息是可用的,WPF使用的低级COM接口记录在this MSDN article中。然而,免责声明是适当的,微软不喜欢你使用它们。接口文档警告“开发人员不应该使用这个接口”,否则没有任何明显的理由为什么这是好的建议。如果微软真的想阻止我们使用它,那么只需不对其进行文档记录将会简单得多。

ITablet2::GetMatchingScreenRect()函数有一些奇怪的地方,您正在寻找的那个函数,缺少它的文档。这本身就是WPF不公开此信息的可能原因。所以谨慎是必要的,你需要在你想要使用它的硬件上进行彻底的测试。我没有任何证据可以证实。

我写了一些使用这些接口的代码。向您的项目添加一个新类,并粘贴如下所示的代码。您需要添加对System.Drawing的引用。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Text;

public enum TouchDeviceKind { Mouse, Pen, Touch }

public class TouchTabletCollection {
    public TouchTabletCollection() {
        Guid CLSID_TabletManager = new Guid("A5B020FD-E04B-4e67-B65A-E7DEED25B2CF");
        var manager = (ITabletManager)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_TabletManager));
        int count = 0;
        manager.GetTabletCount(out count);
        Count = count;
        tablets = new List<TouchTablet>(count);
        for (int index = 0; index < count; index++) {
            tablets.Add(new TouchTablet(manager, index));
        }
    }
    public int Count { get; private set; }
    public TouchTablet this[int index] {
        get { return tablets[index]; }
    }
    private List<TouchTablet> tablets;
}

public class TouchTablet {
    internal TouchTablet(ITabletManager mgr, int index) {
        ITablet itf;
        mgr.GetTablet(index, out itf);
        device1 = itf;
        device2 = (ITablet2)itf;
        device3 = (ITablet3)itf;
    }
    public bool IsMultiTouch {
        get {
            bool multi;
            device3.IsMultiTouch(out multi);
            return multi;
        }
    }
    public TouchDeviceKind Kind {
        get {
            TouchDeviceKind kind;
            device2.GetDeviceKind(out kind);
            return kind;
        }
    }
    public string Name {
        get {
            IntPtr pname;
            device1.GetName(out pname);
            return Marshal.PtrToStringUni(pname);
        }
    }
    public Rectangle InputRectangle {
        get {
            RECT rc;
            device1.GetMaxInputRect(out rc);
            return Rectangle.FromLTRB(rc.Left, rc.Top, rc.Right, rc.Bottom);
        }
    }
    public Rectangle ScreenRectangle {
        get {
            RECT rc;
            device2.GetMatchingScreenRect(out rc);
            return Rectangle.FromLTRB(rc.Left, rc.Top, rc.Right, rc.Bottom);
        }
    }
    private ITablet device1;
    private ITablet2 device2;
    private ITablet3 device3;
}

// COM declarations
[ComImport, Guid("764DE8AA-1867-47C1-8F6A-122445ABD89A")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ITabletManager {
    void GetDefaultTablet(out ITablet table);
    void GetTabletCount(out int count);
    void GetTablet(int index, out ITablet tablet);
    // rest omitted...
}
[ComImport, Guid("1CB2EFC3-ABC7-4172-8FCB-3BC9CB93E29F")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ITablet {
    void Dummy1();
    void Dummy2();
    void GetName(out IntPtr pname);
    void GetMaxInputRect(out RECT inputRect);
    void GetHardwareCaps(out uint caps);
    // rest omitted
}
[ComImport, Guid("C247F616-BBEB-406A-AED3-F75E656599AE")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ITablet2 {
    void GetDeviceKind(out TouchDeviceKind kind);
    void GetMatchingScreenRect(out RECT rect);
}
[ComImport, Guid("AC0E3951-0A2F-448E-88D0-49DA0C453460")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ITablet3 {
    void IsMultiTouch(out bool multi);
    void GetMaximumCursors(out int cursors);
}

internal struct RECT { public int Left, Top, Right, Bottom; }

一个使用它的示例程序:

代码语言:javascript
复制
using System;

class Program {
    static void Main(string[] args) {
        var tablets = new TouchTabletCollection();
        for (int ix = 0; ix < tablets.Count; ++ix) {
            Console.WriteLine("Found tablet {0} named {1}", ix + 1, tablets[ix].Name);
            Console.WriteLine("  Type = {0}, Multi-touch supported = {1}", tablets[ix].Kind, tablets[ix].IsMultiTouch);
            Console.WriteLine("  Input rectangle  = {0}", tablets[ix].InputRectangle);
            Console.WriteLine("  Screen rectangle = {0}", tablets[ix].ScreenRectangle);
        }
        Console.ReadLine();
    }
}

请注意,需要Windows 7或更高版本。我的不懂触摸的笔记本电脑上的输出:

代码语言:javascript
复制
Found tablet 1 named \\.\DISPLAY1
  Type = Mouse, Multi-touch supported = False
  Input rectangle  = {X=0,Y=0,Width=1440,Height=900}
  Screen rectangle = {X=0,Y=0,Width=1440,Height=900}
票数 7
EN

Stack Overflow用户

发布于 2015-04-01 17:39:07

你试过了吗。

代码语言:javascript
复制
 public bool HasTouchInput()
    {
        foreach (TabletDevice tabletDevice in Tablet.TabletDevices)
        {
            //Only detect if it is a touch Screen 
            if(tabletDevice.Type == TabletDeviceType.Touch)
                return true;
        }

        return false;
    }

尝试this链接

或者试试这个

代码语言:javascript
复制
var isTouchDevice = Tablet.TabletDevices.Cast<TabletDevice>().Any(dev => dev.Type == TabletDeviceType.Touch);

this is也可能会有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29215016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档