自動宣告表單範例
自動宣告範例解決了保險評估者的假設案例。 評估者的工作需要他或她造訪其家或企業中的用戶端,並將其宣告資訊輸入表單中。 為了提高評估者的生產力,他的 IT 部門會開發平板電腦應用程式,讓其能夠透過兩個筆跡控制項快速且精確地輸入宣告資訊: InkEdit 和 InkPicture 控制項。
在此範例中, 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);
然後,選取清單方塊中的第一個專案 (本文) 。
' 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 visiblity 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 屬性會在更新 Ink 屬性之前設定為 FALSE 。
最後,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 Form Designer產生的程式碼中,當表單初始化時,InkEdit和InkPicture控制項會新增至表單的元件清單。 當表單關閉時,會由表單的 Dispose 方法處置 InkEdit 和 InkPicture 控制項,以及表單的其他元件。 表單的 Dispose 方法也會處置為表單建立的 Ink 物件。
相關主題