自动声明窗体示例

自动索赔示例解决了保险评估员的假设方案。 评估员的工作要求他或她与客户在家中或企业进行访问,并将他们的索赔信息输入到表单中。 为了提高评估者的工作效率,其 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);

然后,在列表框中选择“正文) (第一个条目。

' 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,即 。

接下来,在更新 Ink 属性之前,InkPicture 控件的 InkEnabled 属性设置为 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 窗体Designer生成的代码中,初始化窗体时,InkEditInkPicture 控件将添加到窗体的组件列表中。 当窗体关闭时,InkEdit 和 InkPicture 控件以及窗体的其他组件通过窗体的 Dispose 方法释放 。 窗体的 Dispose 方法还释放为窗体创建的 Ink 对象。

Microsoft.Ink.Ink

InkPicture 控件

InkEdit 控件