共用方式為


自動宣告表單範例

自動索賠範例解決了保險評估者的假設案例。 評估員的工作要求他或她在家或公司與客戶一起訪問,並將他們的索賠資訊輸入表單。 為了提高評估者的生產力,他的IT部門開發了平板電腦應用程式,可讓他或她透過兩個筆跡控件快速且準確地輸入宣告資訊:InkEditInkPicture 控件。

在此範例中,每個文字輸入字段都會使用 InkEdit 控件。 使用者以手寫筆將保險單和車輛的相關信息輸入到這些欄位中。 InkPicture 控件可用來在汽車影像上新增筆跡,以醒目提示汽車損壞的區域。 自動宣告範例適用於 C# 和 Microsoft Visual Basic .NET。 本主題描述 Visual Basic .NET。

AutoClaims 類別定義為 System.Windows.Forms.Form 的子類別,並定義了一個巢狀類別,用於建立和管理不同類型損毀的墨跡層。 已定義四個事件處理程式來執行下列工作:

  • 初始化格式和筆跡圖層。
  • 重新繪製 InkPicture 控制項
  • 透過列表框選取墨水圖層。
  • 變更筆跡圖層的可見性。

注意

此範例的版本適用於 C# 和 Visual Basic .NET。 本節中討論的版本是 Visual Basic .NET。 這些概念在版本之間是相同的。

 

定義表單和筆跡圖層

您必須匯入 Microsoft.Ink 命名空間:

Imports Microsoft.Ink
// The Ink namespace, which contains the Tablet PC Platform API
using Microsoft.Ink;

接下來,在 AutoClaims 類別中,會定義巢狀 InkLayer 類別,並宣告四個 InkLayer 物件的陣列。 (InkLayer 包含用於儲存筆跡的 Microsoft.Ink.Ink 物件,以及儲存圖層色彩和隱藏狀態的布爾值 System.Drawing.Color 布爾值。當隱藏所有筆跡層時,第五個 Ink 物件會宣告為處理 InkPicture 的筆跡

' Declare the array of ink layers used the vehicle illustration.
Dim inkLayers(3) As InkLayer

' Declare an empty ink object (used when we don't want to draw
' any ink).
Dim emptyInk As Ink

' Declare a value to hold the index of selected ink
Dim selectedIndex As Integer

' Declare a value to hold whether the selected ink is hidden
Dim selectedHidden As Boolean 
// Declare the array of ink layers used the vehicle illustration.
InkLayer[] inkLayers;

// Declare an empty ink object (used when we don't want to draw
// any ink).
Ink emptyInk;

// Declare a value to hold the index of selected ink
int selectedIndex = -1;

// Declare a value to hold whether the selected ink is hidden
bool selectedHidden = false;

每個圖層都有自己的 Ink 物件。 索賠表單中有四個離散區域(車身、視窗、輪胎和頭燈),因此會使用四個 InkLayer 物件。 用戶可以一次檢視圖層的任何組合。

初始化表單和筆跡圖層

Load 事件處理程式會初始化 Ink 物件和四個 InkLayer 物件。

' Initialize the empty ink
emptyInk = New Ink()

' Initialize the four different layers of ink on the vehicle diagram:  
' vehicle body, windows, tires, and headlights.
inkLayers(0) = New InkLayer(New Ink(), Color.Red, False)
inkLayers(1) = New InkLayer(New Ink(), Color.Violet, False)
inkLayers(2) = New InkLayer(New Ink(), Color.LightGreen, False)
inkLayers(3) = New InkLayer(New Ink(), Color.Aqua, False)
// Initialize the empty ink
emptyInk = new Ink();

// Initialize the four different layers of ink on the vehicle diagram:  
// vehicle body, windows, tires, and headlights.
inkLayers = new InkLayer[4];
inkLayers[0] = new InkLayer(new Ink(), Color.Red, false);
inkLayers[1] = new InkLayer(new Ink(), Color.Violet, false);
inkLayers[2] = new InkLayer(new Ink(), Color.LightGreen, false);
inkLayers[3] = new InkLayer(new Ink(), Color.Aqua, false);

然後,選取清單框中的第一個專案(Body)。

' By default, select the first ink layer
lstAnnotationLayer.SelectedIndex = 0
// By default, select the first ink layer
lstAnnotationLayer.SelectedIndex = 0;

最後,將 InkPicture 控件的墨水顏色設定為目前所選的列表框項目。

inkPictVehicle.DefaultDrawingAttributes.Color =
      inkLayers(lstAnnotationLayer.SelectedIndex).ActiveColor
inkPictVehicle.DefaultDrawingAttributes.Color = inkLayers[lstAnnotationLayer.SelectedIndex].ActiveColor;

重新繪製 InkPicture 控制件

InkPicture 控件所繼承的 Paint 事件處理程式中,會檢查筆跡圖層,以判斷哪些是隱藏的。 如果未隱藏圖層,事件過程會使用 Renderer 屬性的 Draw 方法來顯示它。 如果您在對象瀏覽器中查看,您會看到 Microsoft.Ink.InkPicture.Renderer 屬性定義為 Microsoft.Ink.Renderer 物件:

Private Sub inkPictVehicle_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles inkPictVehicle.Paint
    Dim layer As InkLayer

    ' Cycle through each ink layer.  If it is visible, paint it.
    ' Note that it is necessary to customize the paint
    ' behavior, since we want to hide/show different ink layers.
    For Each layer In inkLayers
        If (Not layer.Hidden) Then
            inkPictVehicle.Renderer.Draw(e.Graphics, layer.ActiveInk.Strokes)
        End If
    Next
End Sub
private void inkPictVehicle_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    // Cycle through each ink layer.  If it is visible, paint it.
    // Note that it is necessary to customize the paint
    // behavior, since we want to hide/show different ink layers.
    foreach (InkLayer layer in inkLayers)
    {
        if (!layer.Hidden)
        {
             inkPictVehicle.Renderer.Draw(e.Graphics,layer.ActiveInk.Strokes);
        }
    }          
}

透過清單框選取墨水圖層

當使用者選取清單框中的專案時,SelectedIndexChanged 事件處理程式會先檢查選取範圍是否已變更,並且確認 InkPicture 控件目前未在收集筆跡。 然後,它會將 InkPicture 控制件的筆墨色彩設定為所選筆跡圖層的適當色彩。 此外,它會更新 [隱藏層] 複選框,以反映選取的筆跡圖層隱藏狀態。 最後,InkPicture 控件的繼承 Refresh 方法只會用來顯示控件內所需的層次。

Private Sub chHideLayer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chHideLayer.CheckedChanged

    ' Provided that the new checked hidden value is different than
    ' the previous value...
    If (Not (chHideLayer.Checked = selectedHidden)) Then
        If (Not (inkPictVehicle.CollectingInk)) Then

            ' Update the array indicating the visibility of each ink layer
            inkLayers(lstAnnotationLayer.SelectedIndex).Hidden = chHideLayer.Checked

            ' Set the active ink object to the selected ink
            ' Note that if the current layer is not visible, empty
            ' ink is used to prevent flicker.
            inkPictVehicle.InkEnabled = False
            If (chHideLayer.Checked) Then
                inkPictVehicle.Ink = emptyInk
            Else
                inkPictVehicle.Ink = inkLayers(lstAnnotationLayer.SelectedIndex).ActiveInk
            End If

            ' Update the previous checkbox value to the current
            selectedHidden = chHideLayer.Checked

            ' If the layer is marked hidden, disable ink collection
            inkPictVehicle.InkEnabled = Not chHideLayer.Checked

            Me.Refresh()
       Else
            ' If ink collection is enabled, the active ink cannot be changed
            ' and it is necessary to restore the checkbox to its previous value.
            chHideLayer.Checked = selectedHidden
            MessageBox.Show("Cannot change visibility while collecting ink.")
       End If
   End If
End Sub
private void lstAnnotationLayer_SelectedIndexChanged(object sender, System.EventArgs e)
{
    // Provided that the new selected index value is different than
    // the previous value...
    if (lstAnnotationLayer.SelectedIndex != selectedIndex) 
    {
        if (!inkPictVehicle.CollectingInk)
        {
            // Set the ink and visiblity of the current ink layer
            inkPictVehicle.DefaultDrawingAttributes.Color = inkLayers[lstAnnotationLayer.SelectedIndex].ActiveColor;
            chHideLayer.Checked = inkLayers[lstAnnotationLayer.SelectedIndex].Hidden;

            // Set the active ink object to the selected ink
            // Note that if the current layer is not visible, empty
            // ink is used to prevent flicker.
            inkPictVehicle.InkEnabled = false;
            inkPictVehicle.Ink = chHideLayer.Checked?emptyInk:inkLayers[lstAnnotationLayer.SelectedIndex].ActiveInk;
            inkPictVehicle.InkEnabled = !chHideLayer.Checked;
    
            selectedIndex = lstAnnotationLayer.SelectedIndex;

            this.Refresh();
        }
        else 
        {
            // If ink collection is enabled, the active ink cannot be changed
            // and it is necessary to restore the selection to its previous value.
            lstAnnotationLayer.SelectedIndex = selectedIndex;
            MessageBox.Show("Cannot change active ink while collecting ink.");
        }
    }
}

變更筆跡圖層的可見性

CheckedChanged 事件處理程式首先檢查選取範圍是否已更改,且確認 InkPicture 控件目前未在收集筆跡。 然後,它會更新選取的筆跡圖層隱藏狀態,將 InkPicture 控制件的 InkEnabled 設定為 FALSE、 。

接下來,將 InkPicture 控件的 InkEnabled 屬性設置為 FALSE,然後再更新其 Ink 屬性。

最後,InkPicture 控件會根據選取 [隱藏層] 複選框,針對特定車輛元件啟用或停用,而 InkPicture 控件的 [Refresh 方法只會用來顯示控件內所需的圖層。

Private Sub chHideLayer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chHideLayer.CheckedChanged

    ' Provided that the new checked hidden value is different than
    ' the previous value...
    If (Not (chHideLayer.Checked = selectedHidden)) Then
        If (Not (inkPictVehicle.CollectingInk)) Then

            ' Update the array indicating the visibility of each ink layer
            inkLayers(lstAnnotationLayer.SelectedIndex).Hidden = chHideLayer.Checked

            ' Set the active ink object to the selected ink
            ' Note that if the current layer is not visible, empty
            ' ink is used to prevent flicker.
            inkPictVehicle.InkEnabled = False
            If (chHideLayer.Checked) Then
                inkPictVehicle.Ink = emptyInk
            Else
                inkPictVehicle.Ink = inkLayers(lstAnnotationLayer.SelectedIndex).ActiveInk
            End If

            ' Update the previous checkbox value to the current
            selectedHidden = chHideLayer.Checked

            ' If the layer is marked hidden, disable ink collection
            inkPictVehicle.InkEnabled = Not chHideLayer.Checked

            Me.Refresh()
        Else
            ' If ink collection is enabled, the active ink cannot be changed
            ' and it is necessary to restore the checkbox to its previous value.
            chHideLayer.Checked = selectedHidden
            MessageBox.Show("Cannot change visiblity while collecting ink.")
        End If
    End If
End Sub
private void chHideLayer_CheckedChanged(object sender, System.EventArgs e)
{
    // Provided that the new checked hidden value is different than
    // the previous value...
    if (chHideLayer.Checked != selectedHidden) 
    {
        if (!inkPictVehicle.CollectingInk)
        {
            // Update the array indicating the visibility of each ink layer
            inkLayers[lstAnnotationLayer.SelectedIndex].Hidden = chHideLayer.Checked;

            // Set the active ink object to the selected ink
            // Note that if the current layer is not visible, empty
            // ink is used to prevent flicker.
            inkPictVehicle.InkEnabled = false;
            inkPictVehicle.Ink = chHideLayer.Checked?emptyInk:inkLayers[lstAnnotationLayer.SelectedIndex].ActiveInk;
 
            // If the layer is marked hidden, disable ink collections
            inkPictVehicle.InkEnabled = !chHideLayer.Checked;

            // Update the previous checkbox value to reflect the current
            selectedHidden = chHideLayer.Checked;

            this.Refresh();
        }
        else 
        {
            // If ink collection is enabled, the active ink cannot be changed
            // and it is necessary to restore the checkbox to its previous value.
            chHideLayer.Checked = selectedHidden;
            MessageBox.Show("Cannot change visiblity while collecting ink.");
        }
    }
}

關閉表單

在 Windows 窗體設計工具產生的程式代碼中,當表單初始化時,InkEditInkPicture 控件會新增至表單的元件清單。 當表單關閉時,表單的 Dispose 方法會處置 InkEdit 和 InkPicture 控制項,以及表單上的其他元件。 表單的 Dispose 方法也會將為該表單建立的 Ink 物件處置掉。

Microsoft.Ink.Ink

InkPicture 控制件

InkEdit 控制項