共用方式為


Ink 物件模型:Windows Form 和 COM 與 WPF 的比較

更新:2007 年 11 月

目前,有三種平台支援數位筆墨,包括:Tablet PC Windows Forms 平台、Tablet PC COM 平台和 Windows Presentation Foundation (WPF) 平台。Windows Forms 和 COM 平台會共用類似的物件模型,但 WPF 平台則有極大差異。本主題會討論這些高階功能的差異,好讓操作任一物件模型的開發人員可以深入了解其他的物件模型。

啟用應用程式中的筆墨

這三個平台都已隨附物件和控制項,讓應用程式可以接收來自平板電腦手寫筆的輸入。Windows Forms 和 COM 平台隨附 InkPicture, InkEdit, InkOverlayInkCollector 類別。您可以把 InkPictureInkEdit 這兩個控制項加到應用程式中,進行筆墨收集。InkOverlayInkCollector 可附加至現有的視窗、啟用筆墨的視窗和自訂控制項。

WPF 平台包含 InkCanvas 控制項。您可以將 InkCanvas 加入至應用程式並立即開始收集筆墨。使用者可以透過 InkCanvas,複製、選取並調整筆墨大小。您可以將其他控制項加入 InkCanvas,而使用者也透過手寫覆寫那些控制項。您可以將 InkPresenter 加入至已啟用筆墨功能的自訂控制項並收集其手寫筆的點,來建立自訂控制項。

請參閱下表,進一步了解在應用程式中啟用筆墨的資訊:

如要完成這項工作…

在 WPF 平台上…

在 Windows Forms/COM 平台上…

將已啟用筆墨功能的控制項加入應用程式

請參閱筆墨入門

請參閱Auto Claims Form Sample

啟用自訂控制項上的比跡

請參閱建立筆墨輸入控制項

請參閱Ink Clipboard Sample

筆墨資料

在 Windows Forms 和 COM 平台上,InkCollectorInkOverlayInkEditInkPicture,任一個都會公開 Microsoft.Ink.Ink 物件。Ink 物件包含一或多個 Microsoft.Ink.Stroke 物件的資料,並會公開常見的方法和屬性,以管理和操作那些筆劃。Ink 物件會管理其所包含之筆劃的存留期,Ink 物件會建立和刪除其所有的筆劃。每個 Stroke 都有其父 Ink 物件中唯一的識別項。

在 WPF 平台上,System.Windows.Ink.Stroke 類別會擁有並管理其所有的存留期。可將 Stroke 物件群組收集在 StrokeCollection 中,可提供常見筆墨資料管理作業的方法,例如點擊測試、清除、轉換和序列化筆墨。在任何指定的時間中,Stroke可以屬於零個、一個或多個 StrokeCollection 物件。InkCanvasInkPresenter 會包含 System.Windows.Ink.StrokeCollection,而不需要 Microsoft.Ink.Ink 物件。

下列圖例配對會比較筆墨資料物件模型。在 Windows Forms 和 COM 平台上,Microsoft.Ink.Ink 物件會限制 Microsoft.Ink.Stroke 物件的存留期,且手寫筆封包會屬於個別筆畫。兩或多個筆畫可參考相同 Microsoft.Ink.DrawingAttributes 的物件,如下圖所示。

COM/Winforms 之 Ink 物件模型的圖表。

在 WPF 上,只要某項目包含 System.Windows.Ink.Stroke 的參考,則其都會是 Common Language Runtime 物件。每個 Stroke 都會參考 StylusPointCollectionSystem.Windows.Ink.DrawingAttributes 物件,這些也都是 Common Language Runtime 物件。

WPF 之 Ink 物件模型的圖表。

下表會比較如何在 WPF 平台和 Windows Forms 和 COM 平台上完成某些共同工作的作法。

工作

Windows Presentation Foundation

Windows Forms 和 COM

儲存筆墨

Save

Save

載入筆墨

使用 StrokeCollection.StrokeCollection(Stream) 建構函式建立 StrokeCollection

Load

點擊測試

HitTest

HitTest

複製筆墨

CopySelection

ClipboardCopy

貼上筆墨

Paste

ClipboardPaste

存取筆劃集合上的自訂屬性

AddPropertyData (內部存取的屬性且透過 AddPropertyData, RemovePropertyDataContainsPropertyData 存取)

使用 ExtendedProperties

在平台之間共用筆墨

雖然平台具有筆墨資料的不同物件模型,但是在平台之間共用筆墨非常容易。下列範例會從 Windows Forms 應用程式儲存筆墨,並將筆墨載入 Windows Presentation Foundation 應用程式。

Imports Microsoft.Ink
Imports System.Drawing


...


'/ <summary>
'/ Saves the digital ink from a Windows Forms application.
'/ </summary>
'/ <param name="inkToSave">An Ink object that contains the 
'/ digital ink.</param>
'/ <returns>A MemoryStream containing the digital ink.</returns>
Function SaveInkInWinforms(ByVal inkToSave As Ink) As MemoryStream 
    Dim savedInk As Byte() = inkToSave.Save()

    Return New MemoryStream(savedInk)

End Function 'SaveInkInWinforms
using Microsoft.Ink;
using System.Drawing;


...


/// <summary>
/// Saves the digital ink from a Windows Forms application.
/// </summary>
/// <param name="inkToSave">An Ink object that contains the 
/// digital ink.</param>
/// <returns>A MemoryStream containing the digital ink.</returns>
MemoryStream SaveInkInWinforms(Ink inkToSave)
{
    byte[] savedInk = inkToSave.Save();

    return (new MemoryStream(savedInk));

}
Imports System.Windows.Ink



...


'/ <summary>
'/ Loads digital ink into a StrokeCollection, which can be 
'/ used by a WPF application.
'/ </summary>
'/ <param name="savedInk">A MemoryStream containing the digital ink.</param>
Public Sub LoadInkInWPF(ByVal inkStream As MemoryStream) 
    strokes = New StrokeCollection(inkStream)

End Sub 'LoadInkInWPF

using System.Windows.Ink;


...


/// <summary>
/// Loads digital ink into a StrokeCollection, which can be 
/// used by a WPF application.
/// </summary>
/// <param name="savedInk">A MemoryStream containing the digital ink.</param>
public void LoadInkInWPF(MemoryStream inkStream)
{
    strokes = new StrokeCollection(inkStream);
}

下列範例會從 Windows Presentation Foundation 應用程式儲存筆墨,並將筆墨載入 Windows Forms 應用程式。

Imports System.Windows.Ink



...


'/ <summary>
'/ Saves the digital ink from a WPF application.
'/ </summary>
'/ <param name="inkToSave">A StrokeCollection that contains the 
'/ digital ink.</param>
'/ <returns>A MemoryStream containing the digital ink.</returns>
Function SaveInkInWPF(ByVal strokesToSave As StrokeCollection) As MemoryStream 
    Dim savedInk As New MemoryStream()

    strokesToSave.Save(savedInk)

    Return savedInk

End Function 'SaveInkInWPF

using System.Windows.Ink;


...


/// <summary>
/// Saves the digital ink from a WPF application.
/// </summary>
/// <param name="inkToSave">A StrokeCollection that contains the 
/// digital ink.</param>
/// <returns>A MemoryStream containing the digital ink.</returns>
MemoryStream SaveInkInWPF(StrokeCollection strokesToSave)
{
    MemoryStream savedInk = new MemoryStream();

    strokesToSave.Save(savedInk);

    return savedInk;
}
Imports Microsoft.Ink
Imports System.Drawing


...


'/ <summary>
'/ Loads digital ink into a Windows Forms application.
'/ </summary>
'/ <param name="savedInk">A MemoryStream containing the digital ink.</param>
Public Sub LoadInkInWinforms(ByVal savedInk As MemoryStream) 
    theInk = New Ink()
    theInk.Load(savedInk.ToArray())

End Sub 'LoadInkInWinforms
using Microsoft.Ink;
using System.Drawing;


...


/// <summary>
/// Loads digital ink into a Windows Forms application.
/// </summary>
/// <param name="savedInk">A MemoryStream containing the digital ink.</param>
public void LoadInkInWinforms(MemoryStream savedInk)
{
    theInk = new Ink();
    theInk.Load(savedInk.ToArray());
}

Tablet 畫筆中的事件

當使用者輸入畫筆資訊時,Windows Forms 和 COM 平台上的 InkOverlayInkCollectorInkPicture 便會接收事件。InkOverlayInkCollector 會附加至視窗或控制項,而且可以訂閱由 Tablet 輸入資料引發的事件。事件發生的執行緒取決於事件是否由畫筆、滑鼠或以程式設計方式引發。如需關於這些事件之執行緒的詳細資訊,請參閱General Threading ConsiderationsThreads on Which an Event Can Fire

在 Windows Presentation Foundation 平台上,UIElement 類別具有畫筆輸入的事件。這代表每個控制項會公開一組完整的手寫筆事件。手寫筆事件具有通道版本和反昇版本事件配對,而且一定會在應用程式執行緒上進行。如需詳細資訊,請參閱路由事件概觀

下列圖表會顯示引發手寫筆事件之類別的物件模型比較。Windows Presentation Foundation 物件模型只會顯示反昇事件,而不會顯示通道事件對應項目。

WPF 和 Winforms 中 Stylus 事件的比較圖表

畫筆資料

這三個平台提供攔截和管理來自於 Tablet 畫筆之資料的方法。在 Windows Forms 和 COM 平台上,可建立 RealTimeStylus、將視窗或控制項複製到其中,以及建立實作 IStylusSyncPluginIStylusAsyncPlugin 介面的類別來達到這個作業。然後,則會將自訂外掛程式加入至 RealTimeStylus 的外掛程式集合。如需此物件模型的詳細資訊,請參閱Architecture of the StylusInput APIs

在 WPF 平台上,UIElement 類別會公開外掛程式的集合,此集合的設計與 RealTimeStylus 十分類似。若要攔截畫筆資料,請建立從 StylusPlugIn 繼承的類別,並將物件加入至 UIElementStylusPlugIns 集合。如需有關這個互動的詳細資訊,請參閱攔截手寫筆的輸入

在所有平台上,執行緒集區會透過手寫筆事件接收筆墨資料,並將之傳送到應用程式執行緒。如需有關 COM 和 Windows 平台上執行緒的詳細資訊,請參閱Threading Considerations for the StylusInput APIs。如需有關 Windows Presentation Software 上執行緒的詳細資訊,請參閱筆墨執行緒模型

下列圖示會比較類別的物件模型,這些類別會接收畫筆執行緒集區的畫筆資料。

WPF 和 Winforms 中 StylusPlugin 模型的比較圖表。