- 잉크 스트로크를 표현하는 사각형 화면 그리기
- 실버라이트 영역에 마우스로 그리기 기능 제공






[MainPage.xaml]


<UserControl x:Class="RiaInkPresenter.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">

 

    <Grid x:Name="LayoutRoot" Background="White">

        <StackPanel>

            <Button x:Name="btnClear" Content="전체 지우기"></Button>

            <InkPresenter x:Name="ipPen"

                Background="Silver"

                Width="400"

                Height="300"

            />

        </StackPanel>

    </Grid>

</UserControl>

 

 






[MainPage.xaml.cs]


using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Ink;

 

namespace RiaInkPresenter

{

    public partial class MainPage : UserControl

    {

        // (스트로크) 생성

        private Stroke stroke; // 스트로크 개체 : 펜의 모양

        private Brush brush = new SolidColorBrush(Colors.Red); //[A] 펜의 색상 저장

 

        public MainPage()

        {

            InitializeComponent();

 

            // 4가지 마우스 관련 이벤트 등록

            this.ipPen.MouseLeftButtonDown += new MouseButtonEventHandler(ipPen_MouseLeftButtonDown);

            this.ipPen.MouseMove += new MouseEventHandler(ipPen_MouseMove);

            this.ipPen.MouseLeftButtonUp += new MouseButtonEventHandler(ipPen_MouseLeftButtonUp);

            this.ipPen.MouseLeave += new MouseEventHandler(ipPen_MouseLeave);

 

            this.ipPen.LostMouseCapture += new MouseEventHandler(ipPen_LostMouseCapture);

 

            this.btnClear.Click += new System.Windows.RoutedEventHandler(btnClear_Click);

 

            this.ipPen.KeyDown += new KeyEventHandler(ipPen_KeyDown);

 

            this.LayoutRoot.KeyDown += new KeyEventHandler(ipPen_KeyDown); //[D] 펜의 색상 변경

        }

        //[7] 키보드의 키가 눌렸을 때

        void ipPen_KeyDown(object sender, KeyEventArgs e)

        {

            if (e.Key == Key.R)

            {

                brush = new SolidColorBrush(Colors.Red); //[B] 현재 펜의 색상을 Red

            }

            else if (e.Key == Key.G)

            {

                brush = new SolidColorBrush(Colors.Green);

            }

            else if (e.Key == Key.B)

            {

                brush = new SolidColorBrush(Colors.Blue);

            }

            else

            {

                brush = new SolidColorBrush(Colors.Black);

            }

        }

        //[6] 전체 지우기 버튼 클릭시

        void btnClear_Click(object sender, System.Windows.RoutedEventArgs e)

        {

            ipPen.Strokes.Clear(); // 포함된 모든 Stroke를 클리어

        }

        //[5] 마우스 캡쳐를 잃었을 때

        void ipPen_LostMouseCapture(object sender, MouseEventArgs e)

        {

            stroke = null;

        }

 

        //[0] 펜의 모양 초기화

        private void initializePen()

        {

            this.stroke.DrawingAttributes.Color = ((SolidColorBrush)brush).Color; //[c] 펜의 색상 적용

            this.stroke.DrawingAttributes.OutlineColor = Colors.Blue; // 테두리 색상은 Blue

        }

        //[4] 영역을 벗어날 때

        void ipPen_MouseLeave(object sender, MouseEventArgs e)

        {

            ipPen_MouseLeftButtonUp(null, null); //[3]번 메서드 호출

        }

 

 

        //[3] 마우스 왼쪽버튼 놓기

        void ipPen_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

        {

            ipPen.ReleaseMouseCapture(); // 마우스 캡쳐 종료

            stroke = null; // 스트로크 지우기

        }

 

        //[2] 마우스 이동

        void ipPen_MouseMove(object sender, MouseEventArgs e)

        {

            // 현재 설정된 스트로크가 있다면...

            if (stroke != null)

            {               

                // 이동할 때마다 좌표값을 얻어서 해당 스트로크를 출력 : 스트로크 색상과 위치 추가

                this.stroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(ipPen));

            }

        }

 

        //[1] 왼쪽버튼 눌렀을 때

        void ipPen_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

            // 현재 잉크 영역에서 마우스를 캡쳐

            ipPen.CaptureMouse(); // 마우스 위치 캡쳐

            // 현재 잉크의 위치를 기준으로 마우스(스타일러스) 정보를 스트로크에 담음

            this.stroke = new Stroke(e.StylusDevice.GetStylusPoints(ipPen));

            // 펜 색상 초기화

            initializePen();

            // 모양 출력

            ipPen.Strokes.Add(stroke);

        }

    }

}

 











--> '전체 지우기'버튼을 누른 후의 화면.






Posted by holland14
: