在 WPF 触摸应用中,插入触摸设备,即可在应用里面使用上插入的触摸设备。在 WPF 使用触摸设备的触摸时,需要获取到触摸设备的信息,才能实现触摸

获取触摸设备插入

在 WPF 中,通过 Windows 消息获取触摸设备插入事件,在 src\Microsoft.DotNet.Wpf\src\PresentationCore\System\Windows\Input\Stylus\Wisp\WispLogic.cs 的 HandleMessage 将获取 Windows 消息,代码如下

        internal override void HandleMessage(WindowMessage msg, IntPtr wParam, IntPtr lParam)
{
switch (msg)
{
// 忽略代码
case WindowMessage.WM_TABLET_ADDED:
OnTabletAdded((uint)NativeMethods.IntPtrToInt32(wParam));
break; case WindowMessage.WM_TABLET_DELETED:
OnTabletRemovedImpl((uint)NativeMethods.IntPtrToInt32(wParam), isInternalCall: true);
break;
}
}

在 WPF 框架,使用 WM_TABLET_ADDEDWM_TABLET_DELETED 消息获取设备的插入和删除事件

如上面代码,在设备插入时,将会调用 OnTabletAdded 方法。如 WM_TABLET_ADDED 官方文档描述,以上代码获取的参数是 Wisptis 的 Index 序号。这是因为用户可以插入多个触摸设备,通过传入序号可以拿到插入的设备

在 WPF 中,每次插入触摸设备,都会重新更新所有的触摸设备的信息,而不是只更新插入的设备。在 OnTabletAdded 方法里面,将会调用 GetDeviceCount 方法,在 GetDeviceCount 方法里面将通过 PenThread 的 WorkerGetTabletsInfo 更新所有触摸设备的信息,代码如下

        private void OnTabletAdded(uint wisptisIndex)
{
lock (__penContextsLock)
{
WispTabletDeviceCollection tabletDeviceCollection = WispTabletDevices;
// 忽略代码 // Update the last known device count.
_lastKnownDeviceCount = GetDeviceCount(); uint tabletIndex = UInt32.MaxValue;
// HandleTabletAdded returns true if we need to update contexts due to a change in tablet devices.
if (tabletDeviceCollection.HandleTabletAdded(wisptisIndex, ref tabletIndex))
{
// Update all contexts with this new tablet device.
foreach (PenContexts contexts in __penContextsMap.Values)
{
contexts.AddContext(tabletIndex);
}
}
}
} private int GetDeviceCount()
{
PenThread penThread = null; // Get a PenThread by mimicking a subset of the code in TabletDeviceCollection.UpdateTablets().
TabletDeviceCollection tabletDeviceCollection = TabletDevices;
if (tabletDeviceCollection != null && tabletDeviceCollection.Count > 0)
{
penThread = tabletDeviceCollection[0].As<WispTabletDevice>().PenThread;
} if (penThread != null)
{
// Use the PenThread to get the full, unfiltered tablets info to see how many there are.
TabletDeviceInfo[] tabletdevices = penThread.WorkerGetTabletsInfo();
return tabletdevices.Length;
}
else
{
// if there's no PenThread yet, return "unknown"
return -1;
}
} // WPF 代码格式化就是这样

以上代码调用 WorkerGetTabletsInfo 方法实际的获取触摸信息逻辑是放在触摸线程,上面代码需要先获取触摸线程 PenThread 然后调用触摸线程类的 WorkerGetTabletsInfo 方法,在这个方法里面执行逻辑

触摸线程

WPF 触摸到事件 博客里面告诉大家,在 WPF 框架,为了让触摸的性能足够强,将触摸的获取放在独立的进程里面

在获取触摸信息时,也需要调度到触摸线程执行。在 WPF 中,通过 PenThread 类的相关方法可以调度到触摸线程

在调用 WorkerGetTabletsInfo 方法时,进入 WorkerGetTabletsInfo 方法依然是主线程,里面代码如下

        internal TabletDeviceInfo[] WorkerGetTabletsInfo()
{
// Set data up for this call
WorkerOperationGetTabletsInfo getTablets = new WorkerOperationGetTabletsInfo(); lock(_workerOperationLock)
{
_workerOperation.Add(getTablets);
} // Kick thread to do this work.
MS.Win32.Penimc.UnsafeNativeMethods.RaiseResetEvent(_pimcResetHandle.Value); // Wait for this work to be completed.
getTablets.DoneEvent.WaitOne();
getTablets.DoneEvent.Close(); return getTablets.TabletDevicesInfo;
}

实际上以上代码是放在 PenThreadWorker.cs 文件中,在 WPF 的触摸线程设计上,触摸线程是一个循环,将会等待 PenImc 层发送触摸消息,或者等待 _pimcResetHandle 锁被释放。如上面代码,先插入 WorkerOperationGetTabletsInfo 到 _workerOperation 列表中,然后调用 RaiseResetEvent 方法释放 _pimcResetHandle 对象。触摸线程将会因为 _pimcResetHandle 被释放而跳出循环,然后获取 _workerOperation 列表里面的项,进行执行逻辑

主线程将会在 getTablets.DoneEvent.WaitOne 方法里面进入锁,等待触摸线程执行 WorkerOperationGetTabletsInfo 完成之后释放这个锁,才能让主线程继续执行

触摸线程的循环逻辑代码大概如下

        internal void ThreadProc()
{
Thread.CurrentThread.Name = "Stylus Input";
while (!__disposed)
{
// 忽略代码
WorkerOperation[] workerOps = null; lock(_workerOperationLock)
{
if (_workerOperation.Count > 0)
{
workerOps = _workerOperation.ToArray();
_workerOperation.Clear();
}
} if (workerOps != null)
{
for (int j=0; j<workerOps.Length; j++)
{
workerOps[j].DoWork();
}
workerOps = null;
} // 这是第二层循环
while (true)
{
// 忽略代码 if (!MS.Win32.Penimc.UnsafeNativeMethods.GetPenEvent(
_handles[0], _pimcResetHandle.Value,
out evt, out stylusPointerId,
out cPackets, out cbPacket, out pPackets))
{
break;
}
}
}

默认 WPF 的触摸线程都会在第二层循环,在 GetPenEvent 方法里面等待 PenImc 发送触摸消息或等待 _pimcResetHandle 释放。在跳出第二层循环,将会去获取 _workerOperation 的项,然后执行

                    WorkerOperation[] workerOps = null;

                    lock(_workerOperationLock)
{
if (_workerOperation.Count > 0)
{
workerOps = _workerOperation.ToArray();
_workerOperation.Clear();
}
} if (workerOps != null)
{
for (int j=0; j<workerOps.Length; j++)
{
workerOps[j].DoWork();
}
workerOps = null;
}

获取触摸信息

在调用 WorkerOperationGetTabletsInfo 的 DoWork 方法时,将会在触摸线程获取触摸设备信息

        private class WorkerOperationGetTabletsInfo : WorkerOperation
{
internal TabletDeviceInfo[] TabletDevicesInfo
{
get { return _tabletDevicesInfo;}
} /////////////////////////////////////////////////////////////////////////
/// <summary>
/// Returns the list of TabletDeviceInfo structs that contain information
/// about all of the TabletDevices on the system.
/// </summary>
protected override void OnDoWork()
{
try
{
// create new collection of tablets
MS.Win32.Penimc.IPimcManager3 pimcManager = MS.Win32.Penimc.UnsafeNativeMethods.PimcManager;
uint cTablets;
pimcManager.GetTabletCount(out cTablets); TabletDeviceInfo[] tablets = new TabletDeviceInfo[cTablets]; for ( uint iTablet = 0; iTablet < cTablets; iTablet++ )
{
MS.Win32.Penimc.IPimcTablet3 pimcTablet;
pimcManager.GetTablet(iTablet, out pimcTablet); tablets[iTablet] = PenThreadWorker.GetTabletInfoHelper(pimcTablet);
} // Set result data and signal we are done.
_tabletDevicesInfo = tablets;
}
catch (Exception e) when (PenThreadWorker.IsKnownException(e))
{
Debug.WriteLine("WorkerOperationGetTabletsInfo.OnDoWork failed due to: {0}{1}", Environment.NewLine, e.ToString());
}
} TabletDeviceInfo[] _tabletDevicesInfo = Array.Empty<TabletDeviceInfo>();
}

上面代码的 IPimcManager3 接口是一个 COM 接口,实际逻辑是在 PenImc 层进行定义,在 PenImcRcw.cs 引用,代码如下

    [
ComImport,
Guid(PimcConstants.IPimcManager3IID),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
interface IPimcManager3
{
void GetTabletCount(out UInt32 count);
void GetTablet(UInt32 tablet, out IPimcTablet3 IPimcTablet);
}

在 PenImc 层的 PenImc.idl 文件里面,定义了公开的接口

[
object,
uuid(BD2C38C2-E064-41D0-A999-940F526219C2),
nonextensible,
helpstring("IPimcManager3 Interface"),
pointer_default(unique)
]
interface IPimcManager3 : IUnknown {
[helpstring("method GetTabletCount")] HRESULT GetTabletCount([out] ULONG* pcTablets);
[helpstring("method GetTablet") ] HRESULT GetTablet([in] ULONG iTablet, [out] IPimcTablet3** ppTablet);
};

在 WPF 中,在 C# 代码使用的不是最底层的方法,也就是 BD2C38C2-E064-41D0-A999-940F526219C2 组件只是 WPF 用的,而不是系统等给的接口

实际调用底层的代码是在 PenImc 层的 C++ 代码,但 PenImc 层的 C++ 代码只是一层转发调用而已,换句话说,如果使用 C# 调用底层的系统的组件也是完全可以的

如上面代码通过 GetTabletCount 方法获取当前的触摸设备,此方法是通过 COM 调用到在 PenImc.idl 文件定义的 GetTabletCount 获取的,实际定义的代码是 PimcManager.cpp 文件的 GetTabletCount 方法

STDMETHODIMP CPimcManager::GetTabletCount(__out ULONG* pcTablets)
{
DHR; ULONG cTablets = 0; LoadWisptis(); // Try to load wisptis via the surrogate object. // we will return 0 in the case that there is no stylus since mouse is not considered a stylus anymore
if (m_fLoadedWisptis)
{
CHR(m_pMgrS->GetTabletCount(&cTablets));
} *pcTablets = cTablets; CLEANUP:
RHR;
}

以上代码里面用到了一些宏,如 DHR 的含义是定义 HRESULT 变量,代码如下

#define DHR                                         \
HRESULT hr = S_OK;

CHR 表示的是判断 HRESULT 的值,如果失败了,将会调用 CLEANUP 标签的内容。在 CHR 里面用到 goto 的方法

#define CHR(hr_op)                                  \
{ \
hr = hr_op; \
if (FAILED(hr)) \
goto CLEANUP; \
}

上面代码的 RHR 表示的是返回 HRESULT 变量

#define RHR                                         \
return hr;

因此以上代码实际就是如下代码

STDMETHODIMP CPimcManager::GetTabletCount(__out ULONG* pcTablets)
{
HRESULT hr = S_OK; ULONG cTablets = 0; LoadWisptis(); // Try to load wisptis via the surrogate object. // we will return 0 in the case that there is no stylus since mouse is not considered a stylus anymore
if (m_fLoadedWisptis)
{
hr = m_pMgrS->GetTabletCount(&cTablets);
if (FAILED(hr))
{
goto CLEANUP;
}
} *pcTablets = cTablets; CLEANUP:
return hr;
}

通过上面代码可以看到,实际调用的是 m_pMgrS 的 GetTabletCount 方法,也就是如下代码定义的方法

    MIDL_INTERFACE("764DE8AA-1867-47C1-8F6A-122445ABD89A")
ITabletManager : public IUnknown
{
public:
virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE GetDefaultTablet(
/* [out] */ __RPC__deref_out_opt ITablet **ppTablet) = 0; virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE GetTabletCount(
/* [out] */ __RPC__out ULONG *pcTablets) = 0; virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE GetTablet(
/* [in] */ ULONG iTablet,
/* [out] */ __RPC__deref_out_opt ITablet **ppTablet) = 0; virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE GetTabletContextById(
/* [in] */ TABLET_CONTEXT_ID tcid,
/* [out] */ __RPC__deref_out_opt ITabletContext **ppContext) = 0; virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE GetCursorById(
/* [in] */ CURSOR_ID cid,
/* [out] */ __RPC__deref_out_opt ITabletCursor **ppCursor) = 0; };

可以看到这是一个 COM 接口调用,实际使用的就是系统提供的 ITabletManager 组件

在底层系统组件,先调用 ITabletManager 的 GetTabletCount 方法 获取触摸设备数量,然后遍历触摸设备序号拿到 ITablet 对象

在 C# 代码里面的逻辑如下

                    pimcManager.GetTabletCount(out cTablets);

                    TabletDeviceInfo[] tablets = new TabletDeviceInfo[cTablets];

                    for ( uint iTablet = 0; iTablet < cTablets; iTablet++ )
{
MS.Win32.Penimc.IPimcTablet3 pimcTablet;
pimcManager.GetTablet(iTablet, out pimcTablet); tablets[iTablet] = PenThreadWorker.GetTabletInfoHelper(pimcTablet);
}

这里的 pimcManager.GetTablet 方法将会调用到 PimcManager.cpp 的 GetTablet 方法

STDMETHODIMP CPimcManager::GetTablet(ULONG iTablet, __deref_out IPimcTablet3** ppTablet)
{
DHR; switch (iTablet)
{
case RELEASE_MANAGER_EXT:
{
CHR(m_managerLock.Unlock());
}
break;
default:
{
CHR(GetTabletImpl(iTablet, ppTablet));
}
} CLEANUP:
RHR;
} STDMETHODIMP CPimcManager::GetTabletImpl(ULONG iTablet, __deref_out IPimcTablet3** ppTablet)
{
DHR;
LoadWisptis(); // Make sure wisptis has been loaded! (Can happen when handling OnTabletAdded message) CComPtr<ITablet> pTabS;
CComObject<CPimcTablet> * pTabC; // Can only call if we have real tablet hardware which means wisptis must be loaded!
CHR(m_fLoadedWisptis ? S_OK : E_UNEXPECTED);
CHR(CComObject<CPimcTablet>::CreateInstance(&pTabC));
CHR(pTabC->QueryInterface(IID_IPimcTablet3, (void**)ppTablet));
CHR(m_pMgrS->GetTablet(iTablet, &pTabS));
CHR(pTabC->Init(m_fLoadedWisptis?pTabS:NULL, this)); CLEANUP:
RHR;
}

本质调用的是 m_pMgrS 的 GetTablet 方法,也就是系统提供的 ITabletManager 的 GetTablet 方法 获取 ITablet 接口。只是在 C++ 代码里面,将 ITablet 接口再做一层封装,返回给 C# 的是 IPimcTablet3 接口

接下来就是通过 PenThreadWorker 的 GetTabletInfoHelper 方法获取触摸信息

        private static TabletDeviceInfo GetTabletInfoHelper(IPimcTablet3 pimcTablet)
{
TabletDeviceInfo tabletInfo = new TabletDeviceInfo(); tabletInfo.PimcTablet = new SecurityCriticalDataClass<IPimcTablet3>(pimcTablet);
pimcTablet.GetKey(out tabletInfo.Id);
pimcTablet.GetName(out tabletInfo.Name);
pimcTablet.GetPlugAndPlayId(out tabletInfo.PlugAndPlayId);
int iTabletWidth, iTabletHeight, iDisplayWidth, iDisplayHeight;
pimcTablet.GetTabletAndDisplaySize(out iTabletWidth, out iTabletHeight, out iDisplayWidth, out iDisplayHeight);
tabletInfo.SizeInfo = new TabletDeviceSizeInfo(new Size(iTabletWidth, iTabletHeight),
new Size(iDisplayWidth, iDisplayHeight));
int caps;
pimcTablet.GetHardwareCaps(out caps);
tabletInfo.HardwareCapabilities = (TabletHardwareCapabilities)caps;
int deviceType;
pimcTablet.GetDeviceType(out deviceType);
tabletInfo.DeviceType = (TabletDeviceType)(deviceType -1); //
// REENTRANCY NOTE: Let a PenThread do this work to avoid reentrancy!
// The IPimcTablet3 object is created in the pen thread. If we access it from the UI thread,
// COM will set up message pumping which will cause reentrancy here.
InitializeSupportedStylusPointProperties(pimcTablet, tabletInfo);
tabletInfo.StylusDevicesInfo = GetStylusDevicesInfo(pimcTablet); // Obtain the WispTabletKey for future use in locking the WISP tablet.
tabletInfo.WispTabletKey = MS.Win32.Penimc.UnsafeNativeMethods.QueryWispTabletKey(pimcTablet); // If the manager has not already been created and locked, we will lock it here. This is the first opportunity
// we will have to lock the manager as it will have been created on the thread to instantiate the first tablet.
MS.Win32.Penimc.UnsafeNativeMethods.SetWispManagerKey(pimcTablet); MS.Win32.Penimc.UnsafeNativeMethods.LockWispManager(); return tabletInfo;
}

实际调用的就是 ITablet 接口 的方法

以上代码的 pimcTablet.GetKey 方法是在 C++ 层封装的,而不是系统提供的

STDMETHODIMP CPimcTablet::GetKey(__out INT * pKey)
{
DHR;
CHR(pKey ? S_OK : E_INVALIDARG);
*pKey = (INT)PtrToInt(m_pTabS.p);
CLEANUP:
RHR;
} CComPtr<ITablet> m_pTabS;

在 WPF 框架,获取的方法本质就是通过 Tablet PC 系统组件获取

更多触摸请看 WPF 触摸相关

dotnet 读 WPF 源代码笔记 插入触摸设备的初始化获取设备信息的更多相关文章

  1. dotnet 读 WPF 源代码笔记 布局时 Arrange 如何影响元素渲染坐标

    大家是否好奇,在 WPF 里面,对 UIElement 重写 OnRender 方法进行渲染的内容,是如何受到上层容器控件的布局而进行坐标偏移.如有两个放入到 StackPanel 的自定义 UIEl ...

  2. dotnet 读 WPF 源代码笔记 渲染收集是如何触发

    在 WPF 里面,渲染可以从架构上划分为两层.上层是 WPF 框架的 OnRender 之类的函数,作用是收集应用程序渲染的命令.上层将收集到的应用程序绘制渲染的命令传给下层,下层是 WPF 的 GF ...

  3. Python实用笔记 (21)面向对象编程——获取对象信息

    当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type()判断: >>> ...

  4. iOS获取设备型号和App版本号等信息(OC+Swift)

    iOS获取设备型号和App版本号等信息(OC+Swift) 字数1687 阅读382 评论3 喜欢10 好久没有写过博客了,因为中间工作比较忙,然后有些个人事情所以耽误了.但是之前写的博客还一直有人来 ...

  5. opencl(2)平台、设备、上下文的获取与信息获取

    1:平台 1)获取平台id cl_int clGetPlatformIDs( cl_uint num_entries,      //想要获取的平台数 cl_platform_id * flatfor ...

  6. 通过 AppSwitch 禁用 WPF 内置的触摸让 WPF 程序可以处理 Windows 触摸消息

    原文:通过 AppSwitch 禁用 WPF 内置的触摸让 WPF 程序可以处理 Windows 触摸消息 WPF 框架自己实现了一套触摸机制,但同一窗口只能支持一套触摸机制,于是这会禁用系统的触摸消 ...

  7. 读Flask源代码学习Python--config原理

    读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因   莫名其妙在第一份工作中使用了从来没有接 ...

  8. 《深入浅出WPF》笔记——绘画与动画

    <深入浅出WPF>笔记——绘画与动画   本篇将记录一下如何在WPF中绘画和设计动画,这方面一直都不是VS的强项,然而它有一套利器Blend:这方面也不是我的优势,幸好我有博客园,能记录一 ...

  9. 《深入浅出WPF》笔记——资源篇

    原文:<深入浅出WPF>笔记--资源篇 前面的记录有的地方已经用到了资源,本文就来详细的记录一下WPF中的资源.我们平时的“资源”一词是指“资财之源”,是创造人类社会财富的源泉.在计算机程 ...

  10. 在Linux上编译dotnet cli的源代码生成.NET Core SDK的安装包

    .NET 的开源,有了更多的DIY乐趣.这篇博文记录一下在新安装的 Linux Ubuntu 14.04 上通过自己动手编译 dotnet cli 的源代码生成 .net core sdk 的 deb ...

随机推荐

  1. 崩溃bug日志总结1

    目录介绍 1.1 java.lang.UnsatisfiedLinkError找不到so库异常 1.2 java.lang.IllegalStateException非法状态异常 1.3 androi ...

  2. 如何理解UDP 和 TCP? 区别? 应用场景?

    一.UDP UDP(User Datagram Protocol),用户数据包协议,是一个简单的面向数据报的通信协议,即对应用层交下来的报文,不合并,不拆分,只是在其上面加上首部后就交给了下面的网络层 ...

  3. Advanced .Net Debugging 5:基本调试任务(线程的操作、代码审查、CLR内部的命令、诊断命令和崩溃转储文件)

    一.介绍 这是我的<Advanced .Net Debugging>这个系列的第五篇文章.今天这篇文章的标题虽然叫做"基本调试任务",但是这章的内容还是挺多的.上一篇我 ...

  4. 解决maven编译错误:程序包com.sun.xml.internal.ws.spi不存在

    转自https://blog.csdn.net/mn960mn/article/details/51253038 解决方法如下: 添加maven-compiler-plugin插件,并且配置compi ...

  5. SQLSERVER 的表分区(水平) 操作记录1

    --创建表格 (注意) 是唯一(NONCLUSTERED)表示 非聚集索引 CREATE TABLE [dbo].[UserInfo]( [Id] [int] IDENTITY(1,1) NOT NU ...

  6. 7 HTML锚点应用

    7 锚点应用 锚点( anchor )是超链接的一种应用,也叫命名锚记,锚点可以像一个定位器一样,可以实现页面内的链接跳转,运用相当普遍.例如,我们有一个网页,由于内容太多,导致页面很长,而且里面的内 ...

  7. Codeforces Round #670 (Div. 2)

    CF1406A Subset Mex 洛谷传送门 CF1406A 分析 从小到大考虑每一个数的出现次数,最小未出现的数就是A的mex值, 然后将A选完的数删掉一个接着以同样的方式找B的mex值,这显然 ...

  8. 【开源三方库】Fuse.js:强大、轻巧、零依赖的模糊搜索库

      开源项目 OpenHarmony 是每个人的 OpenHarmony 曹天恒 公司:中国科学院软件研究所 小组:知识体系工作组 1.简介 Fuse.js是一款功能强大且轻量级的JavaScript ...

  9. OpenHarmony源码解析之电话子系统——通话流程

    (以下内容来自开发者分享,不代表 OpenHarmony 项目群工作委员会观点) 王大鹏 深圳开鸿数字产业发展有限公司 一.简介 OpenAtom OpenHarmony(以下简称"Open ...

  10. Java ArrayList 与 LinkedList 的灵活选择

    Java ArrayList Java ArrayList 类是一个可变大小的数组,位于 java.util 包中. 创建 ArrayList import java.util.ArrayList; ...