Blog Content

    티스토리 뷰

    50.SilverLight3 - InkPresenter

    반응형


    MainPage.xaml

    [그림 50-1]


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

            <StackPanel>

                <Button x:Name="btnClear" Content="u¨ù o¯i¾a"></Button>

                <InkPresenter x:Name="ipPen" Background="Silver" Width="400" Height="300"/>

            </StackPanel>

    </Grid>


    MainPage.xaml.cs

    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]

        }

        //[0]

        private void InitializePen()

        {

            //[C]

            this.stroke.DrawingAttributes.Color = ((SolidColorBrush)brush).Color; 

            this.stroke.DrawingAttributes.OutlineColor = Colors.Blue; // Blue

        }

     

        //[1]

        void ipPen_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

            //

            ipPen.CaptureMouse(); //

     

            // ()

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


            //

            InitializePen();


            //

            ipPen.Strokes.Add(stroke);

        }

     

        //[2]

        void ipPen_MouseMove(object sender, MouseEventArgs e)

        {

            // ...

            if (stroke != null)

            {

                // :

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

            }

        }

     

        //[3]

        void ipPen_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

        {

            ipPen.ReleaseMouseCapture(); //

            stroke = null; //

        }

     

        //[4]

        void ipPen_MouseLeave(object sender, MouseEventArgs e)

        {

            ipPen_MouseLeftButtonUp(null, null); //[3]

        }

     

        //[5]

        void ipPen_LostMouseCapture(object sender, MouseEventArgs e)

        {

            stroke = null;

        }

     

        //[6]

        void btnClear_Click(object sender, RoutedEventArgs e)

        {

            ipPen.Strokes.Clear(); // Stroke

        }

     

        //[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);

            }

        }

    }


    결과화면

    [그림 50-2]



    반응형

    'Silverlight' 카테고리의 다른 글

    52.SilverLight3 - AttachedProperty  (0) 2009.12.02
    51.SilverLight3 - RoutedEventArgs  (0) 2009.12.01
    49.SilverLight3 - DragAndDrop  (0) 2009.12.01
    48.SilverLight3 - TextBlock  (0) 2009.12.01
    47.SilverLight3 - PasswordBox  (0) 2009.12.01

    Comments