Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何区分触摸屏和普通触摸屏?_C#_Wpf_Screen_Touchscreen - Fatal编程技术网

C# 如何区分触摸屏和普通触摸屏?

C# 如何区分触摸屏和普通触摸屏?,c#,wpf,screen,touchscreen,C#,Wpf,Screen,Touchscreen,如果多个屏幕连接到一台计算机,我想在触摸屏上显示一个应用程序。通过迭代System.Windows.Forms.Screen.AllScreens我可以获得工作区域以移动窗口。但是,Screen不提供IsTouchscreen方法 另一方面,通过迭代所有System.Windows.Input.Tablet.TabletDevices,我无法找到相应的Screen,因为Screen.DeviceName与TabletDevice.Name不匹配 因此,是否有办法将屏幕与表格设备相匹配,或者是否有

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

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


因此,是否有办法将
屏幕
表格设备
相匹配,或者是否有其他解决方法可以使用?

此信息可用,WPF使用的低级COM接口在中有记录。无论免责声明多么恰当,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; }
你试过了吗

 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;
    }
尝试链接

或者试试这个

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

它也可能有帮助

可能是复制品,或者我认为它不是复制品,@Pedro不只是想知道是否有可用的触摸屏。他想知道连接的屏幕是否是触摸屏。
 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;
    }
var isTouchDevice = Tablet.TabletDevices.Cast<TabletDevice>().Any(dev => dev.Type == TabletDeviceType.Touch);