如何告诉普通触摸屏?

时间:2015-03-23 16:10:09

标签: c# wpf screen touchscreen

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

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

那么有没有办法以某种方式将ScreenTabletDevice匹配,还是我可以使用另一种解决方法?

2 个答案:

答案 0 :(得分:7)

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

ITablet2 :: GetMatchingScreenRect()函数正在寻找一些时髦的东西,你正在寻找它,缺少它的文档。本身可能是WPF未公开此信息的原因。因此需要谨慎,您需要在要使用它的硬件上进行彻底测试。我没有任何要验证的内容。

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

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; }

使用它的示例程序:

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或更高版本是必需的。触摸式无线笔记本电脑的输出:

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}

答案 1 :(得分:1)

你试过吗。

 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链接

或试试这个

var isTouchDevice = Tablet.TabletDevices.Cast<TabletDevice>().Any(dev => dev.Type == TabletDeviceType.Touch);

this可能也有帮助