gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:39:09 27 4
gpt4 key购买 nike

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

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

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

最佳答案

此信息可用,WPF 使用的低级 COM 接口(interface)记录在 this MSDN article 中.然而,免责声明是适当的,Microsoft 不喜欢您使用它们。接口(interface)文档警告“开发人员不应使用此接口(interface)”,否则没有任何明显的理由说明这是个好建议。如果 Microsoft 真的想阻止我们使用它,那么不记录它们会简单得多。

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

我写了一些使用这些接口(interface)的代码。向您的项目添加一个新类并粘贴如下所示的代码。您需要添加对 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}

关于c# - 如何区分触摸屏和普通触摸屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29215016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com