다음을 통해 공유


방법: 잉크 데이터에 사용자 지정 데이터 추가

잉크가 ISF(Serialize된 잉크 형식)로 저장될 때, 저장되는 잉크에 사용자 지정 데이터를 추가할 수 있습니다. 사용자 지정 데이터를 DrawingAttributes, StrokeCollection 또는 Stroke에 저장할 수 있습니다. 세 가지 개체에 사용자 지정 데이터를 저장할 수 있으므로 데이터를 저장할 최적의 위치를 결정할 수 있습니다. 세 가지 클래스는 모두 유사한 메서드를 사용하여 사용자 지정 데이터를 저장하고 데이터에 액세스합니다.

다음 형식만 사용자 지정 데이터로 저장할 수 있습니다.

예시

다음 예제에서는 사용자 지정 데이터를 추가하고 StrokeCollection에서 검색하는 방법을 보여 줍니다.

Guid timestamp = new Guid("12345678-9012-3456-7890-123456789012");

// Add a timestamp to the StrokeCollection.
private void AddTimestamp()
{

    inkCanvas1.Strokes.AddPropertyData(timestamp, DateTime.Now);
}

// Get the timestamp of the StrokeCollection.
private void GetTimestamp()
{

    if (inkCanvas1.Strokes.ContainsPropertyData(timestamp))
    {
        object date = inkCanvas1.Strokes.GetPropertyData(timestamp);

        if (date is DateTime)
        {
            MessageBox.Show("This StrokeCollection's timestamp is " +
                ((DateTime)date).ToString());
        }
    }
    else
    {
        MessageBox.Show(
            "The StrokeCollection does not have a timestamp.");
    }
}

다음 예제에서는 InkCanvas와 단추 두 개를 표시하는 애플리케이션을 만듭니다. switchAuthor 단추는 서로 다른 두 명의 작성자가 두 개의 펜을 사용할 수 있게 합니다. changePenColors 단추는 작성자별로 InkCanvas에서 각 스트로크의 색상을 변경합니다. 애플리케이션은 두 DrawingAttributes 개체를 정의하고, 어떤 작성자가 Stroke를 그렸는지를 나타내는 사용자 지정 속성을 각 개체에 추가합니다. 사용자가 changePenColors를 클릭하면 애플리케이션은 사용자 지정 속성 값을 기준으로 스트로크의 모양을 변경합니다.

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Adding Custom Data to Ink" Height="500" Width="700"
    >
  <DockPanel Name="root">

    <StackPanel  Background="DarkSlateBlue">
      <Button Name="switchAuthor" Click="switchAuthor_click" >
        Switch to student's pen 
      </Button>
      <Button Name="changePenColors" Click="changeColor_click" >
        Change the color of the pen ink
      </Button>
    </StackPanel>
    <InkCanvas Name="inkCanvas1">
    </InkCanvas>
  </DockPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Ink;

/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

public partial class Window1 : Window
{
    Guid authorGuid = new Guid("12345678-9012-3456-7890-123456789012");
    DrawingAttributes teachersDA = new DrawingAttributes();
    DrawingAttributes studentsDA = new DrawingAttributes();
    string teacher = "teacher";
    string student = "student";
    bool useStudentPen = false;

    public Window1()
    {
        InitializeComponent();

        teachersDA.Color = Colors.Red;
        teachersDA.Width = 5;
        teachersDA.Height = 5;
        teachersDA.AddPropertyData(authorGuid, teacher);

        studentsDA.Color = Colors.Blue;
        studentsDA.Width = 5;
        studentsDA.Height = 5;
        studentsDA.AddPropertyData(authorGuid, student);

        inkCanvas1.DefaultDrawingAttributes = teachersDA;
    }

    // Switch between using the 'pen' DrawingAttributes and the
    // 'highlighter' DrawingAttributes.
    void switchAuthor_click(Object sender, RoutedEventArgs e)
    {
        useStudentPen = !useStudentPen;

        if (useStudentPen)
        {
            switchAuthor.Content = "Use teacher's pen";
            inkCanvas1.DefaultDrawingAttributes = studentsDA;
        }
        else
        {
            switchAuthor.Content = "Use student's pen";
            inkCanvas1.DefaultDrawingAttributes = teachersDA;
        }
    }

    // Change the color of the ink that on the InkCanvas that used the pen.
    void changeColor_click(Object sender, RoutedEventArgs e)
    {
        foreach (Stroke s in inkCanvas1.Strokes)
        {
            if (s.DrawingAttributes.ContainsPropertyData(authorGuid))
            {
                object data = s.DrawingAttributes.GetPropertyData(authorGuid);

                if ((data is string) && ((string)data == teacher))
                {
                    s.DrawingAttributes.Color = Colors.Black;
                }
                if ((data is string) && ((string)data == student))
                {
                    s.DrawingAttributes.Color = Colors.Green;
                }
            }
        }
    }
}