title | author | date | CreateTime | categories |
WPF 禁用實時觸摸 | lindexi | 2018-11-13 10:45:37 +0800 | 2018-5-4 21:0:38 +0800 | WPF 觸摸 |
微軟想把 WPF 作為 win7 的觸摸好用的架構,是以微軟做了很多特殊的相容。為了獲得真實的觸摸消息,微軟提供了 OnStylusDown, OnStylusUp, 和 OnStylusMove 事件。 本文告訴大家如何使用代碼禁用 WPF 的觸摸消息,解決一些問題。
在 win7 還提供了多點觸摸 windows 消息 WM_TOUCH ,通過這兩個 API 一個是 OnStylusDown 這些事件,另一個就是 WM_TOUCH ,使用者可以拿到觸摸消息。
這兩個 API 是互相獨立,依靠相同的 HWND 。
那麼為什麼需要禁用 WPF 的 RealTimeStylus ,因為在 WPF 觸摸平台會禁用 WM_TOUCH 消息。如果想要使用 WM_TOUCH ,在 WPF 需要禁用 WPF 的觸摸事件。
如果沒有禁用,就無法拿到 WM_TOUCH 消息,這個方法可以讓自己定義自己的觸摸。
禁用的方法使用下面代碼
public static void DisableWPFTabletSupport()
{
// Get a collection of the tablet devices for this window.
TabletDeviceCollection devices = System.Windows.Input.Tablet.TabletDevices;
if (devices.Count > 0)
{
// Get the Type of InputManager.
Type inputManagerType = typeof(System.Windows.Input.InputManager);
// Call the StylusLogic method on the InputManager.Current instance.
object stylusLogic = inputManagerType.InvokeMember("StylusLogic",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, InputManager.Current, null);
if (stylusLogic != null)
{
// Get the type of the stylusLogic returned from the call to StylusLogic.
Type stylusLogicType = stylusLogic.GetType();
// Loop until there are no more devices to remove.
while (devices.Count > 0)
{
// Remove the first tablet device in the devices collection.
stylusLogicType.InvokeMember("OnTabletRemoved",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
null, stylusLogic, new object[] { (uint)0 });
}
}
}
}
代碼直接可以直接放在項目,代碼是在微軟文檔複制。
雖然禁用微軟提供的觸摸事件,可以修複很多坑,但是禁用了也是有很多新的坑,不過我就不在這裡告訴大家。自己嘗試運作下面代碼,然後試試程式。
為什麼這樣就可以禁用觸摸,請看WPF 觸摸到事件