天天看點

穩紮穩打Silverlight(14) - 2.0互動之InkPresenter(塗鴉闆)

<a href="http://webabcd.blog.51cto.com/1787395/342790" target="_blank">[索引頁]</a>

<a href="http://down.51cto.com/data/100302" target="_blank">[源碼下載下傳]</a>

穩紮穩打Silverlight(14) - 2.0互動之InkPresenter(塗鴉闆)

介紹

Silverlight 2.0 人機互動:InkPresenter(塗鴉闆)

    InkPresenter - 塗鴉闆,也就是在面闆上呈現墨迹。InkPresenter 可以包含子控件

    Cursor - 滑鼠移動到 InkPresenter 上面時,滑鼠指針的樣式

    Background - 塗鴉闆背景

    Opacity - 面闆上墨迹的不透明度

    Clip - InkPresenter 的剪輯區域

    Stroke.DrawingAttributes - Stroke(筆劃)的外觀屬性

    UIElement.CaptureMouse() - 為 UIElement 對象啟用滑鼠捕捉

    UIElement.CaptureMouse() - 為 UIElement 對象釋放滑鼠捕捉

線上DEMO

<a href="http://webabcd.blog.51cto.com/1787395/342779">http://webabcd.blog.51cto.com/1787395/342779</a>

示例

InkPresenter.xaml

&lt;UserControl x:Class="Silverlight20.Interactive.InkPresenter" 

        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"&gt; 

        &lt;Canvas&gt; 

                &lt;!--InkPresenter 的外圍的帶邊框的背景圖--&gt; 

                &lt;Rectangle Width="420" Height="350" Stroke="Black" StrokeThickness="1"&gt; 

                        &lt;Rectangle.Fill&gt; 

                                &lt;ImageBrush ImageSource="/Silverlight20;component/Images/Background.jpg" Stretch="Fill" /&gt; 

                        &lt;/Rectangle.Fill&gt; 

                &lt;/Rectangle&gt; 

                &lt;!--用于描繪 InkPresenter 的工作區--&gt; 

                &lt;Rectangle Canvas.Top="10" Canvas.Left="10" Width="400" Height="300" RadiusX="25" RadiusY="25" Fill="Black" Opacity="0.2" /&gt; 

                &lt;!-- 

                InkPresenter - 塗鴉闆,也就是在面闆上呈現墨迹 

                        Cursor - 滑鼠移動到 InkPresenter 上面時,滑鼠指針的樣式 

                                Arrow - 箭頭 

                                Hand - 手形    

                                Wait - 沙漏 

                                IBeam - “I”字形    

                                Stylus - 點 

                                Eraser - 橡皮 

                                None - 無 

                        Background - 塗鴉闆背景。建議設定其為“Transparent”,需要的話可以使用其他控件來描繪背景 

                        Opacity - 面闆上墨迹的不透明度 

                        Clip - InkPresenter 的剪輯區域。本例給 InkPresenter 做了一個圓角效果,其Clip值由 Blend 生成 

                --&gt; 

                &lt;InkPresenter x:Name="inkPresenter" Cursor="Stylus" Canvas.Top="10" Canvas.Left="10" Width="400" Height="300" Background="Transparent" 

                        MouseLeftButtonDown="inkPresenter_MouseLeftButtonDown"    

                        MouseLeftButtonUp="inkPresenter_MouseLeftButtonUp"    

                        MouseMove="inkPresenter_MouseMove"    

                        Clip="M0.5,25.5 C0.5,11.692882 11.692882,0.5 25.5,0.5 L374.5,0.5 C388.30713,0.5 399.5,11.692882 399.5,25.5 L399.5,274.5 C399.5,288.30713 388.30713,299.5 374.5,299.5 L25.5,299.5 C11.692882,299.5 0.5,288.30713 0.5,274.5 z"&gt; 

                        &lt;!-- 

                        InkPresenter 可以包含子控件。本例為在 InkPresenter 的底部循環播放視訊 

                        --&gt; 

                        &lt;MediaElement x:Name="mediaElement" Source="/Silverlight20;component/Video/Demo.wmv" Width="400" Height="100" Canvas.Top="200" Stretch="UniformToFill" MediaEnded="mediaElement_MediaEnded" /&gt; 

                &lt;/InkPresenter&gt; 

                &lt;!--紅色取色點,點此後可畫紅色的線--&gt; 

                &lt;Ellipse x:Name="ellipseRed" Canvas.Top="320" Canvas.Left="20" Cursor="Hand" Fill="Red" Width="20" Height="20" MouseLeftButtonDown="ellipseRed_MouseLeftButtonDown" /&gt; 

                &lt;!--黑色取色點,點此後可畫黑色的線--&gt; 

                &lt;Ellipse x:Name="ellipseBlack" Canvas.Top="320" Canvas.Left="50" Cursor="Hand" Fill="Black" Width="20" Height="20" MouseLeftButtonDown="ellipseBlack_MouseLeftButtonDown" /&gt; 

                &lt;!--橡皮擦,點此後可擦除之前畫的線--&gt; 

                &lt;Button x:Name="btnEraser" Canvas.Top="320" Canvas.Left="80" Content="橡皮擦" Click="btnEraser_Click" /&gt; 

                &lt;!--用于清除 InkPresenter 上的墨迹的按鈕--&gt; 

                &lt;Button x:Name="btnClear" Canvas.Top="320" Canvas.Left="130" Content="清除" Click="btnClear_Click" /&gt; 

                &lt;!--用于顯示目前 Stroke(筆劃) 所在的 矩形範圍 的位置資訊--&gt; 

                &lt;TextBox x:Name="txtMsg" Canvas.Top="320" Canvas.Left="180" Width="220" /&gt; 

        &lt;/Canvas&gt; 

&lt;/UserControl&gt;

InkPresenter.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; 

using System.Xml.Linq; 

using System.ServiceModel; 

using System.ServiceModel.Channels; 

namespace Silverlight20.Interactive 

        public partial class InkPresenter : UserControl 

        { 

                // 在塗鴉闆上描繪的筆劃 

                private System.Windows.Ink.Stroke _newStroke; 

                // 在塗鴉闆上描繪的筆劃的顔色 

                private System.Windows.Media.Color _currentColor = Colors.Red; 

                // 是否是擦除操作 

                private bool _isEraser = false; 

                // 目前是否正在 InkPresenter 上捕獲滑鼠 

                private bool _isCapture = false; 

                public InkPresenter() 

                { 

                        InitializeComponent(); 

                } 

                void inkPresenter_MouseLeftButtonDown(object sender, MouseEventArgs e) 

                        // UIElement.CaptureMouse() - 為 UIElement 對象啟用滑鼠捕捉 

                        // 為 InkPresenter 啟用滑鼠捕捉 

                        inkPresenter.CaptureMouse(); 

                        _isCapture = true; 

                        if (_isEraser) 

                        { 

                                // 擦除滑鼠目前位置所屬的 Stroke(筆劃) 

                                RemoveStroke(e); 

                        } 

                        else 

                                // System.Windows.Input.MouseEventArgs.StylusDevice.Inverted - 是否正在使用手寫筆(tablet pen)的輔助筆尖 

                                // System.Windows.Ink.Stroke.DrawingAttributes - Stroke(筆劃)的外觀屬性 

                                // System.Windows.Ink.Stroke.DrawingAttributes.Width - 筆劃的寬 

                                // System.Windows.Ink.Stroke.DrawingAttributes.Height - 筆劃的高 

                                // System.Windows.Ink.Stroke.DrawingAttributes.Color - 筆劃的顔色 

                                // System.Windows.Ink.Stroke.DrawingAttributes.OutlineColor - 筆劃的外框的顔色 

                                _newStroke = new System.Windows.Ink.Stroke(); 

                                _newStroke.DrawingAttributes.Width = 3d; 

                                _newStroke.DrawingAttributes.Height = 3d; 

                                _newStroke.DrawingAttributes.Color = _currentColor; 

                                _newStroke.DrawingAttributes.OutlineColor = Colors.Yellow; 

                                // 為 Stroke(筆劃) 在目前滑鼠所在位置處增加 StylusPoint(點) 

                                _newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter)); 

                                // 将設定好的 Stroke(筆劃) 添加到 InkPresenter 的 Strokes(筆劃集) 中 

                                inkPresenter.Strokes.Add(_newStroke); 

                                // Stroke.GetBounds() - 擷取目前 Stroke(筆劃) 所在的 矩形範圍 的位置資訊 

                                // Strokes.GetBounds() - 擷取目前 Strokes(筆劃集) 所在的 矩形範圍 的位置資訊 

                                // 顯示該 Stroke(筆劃) 所在的 矩形範圍 的位置資訊 

                                Rect rect = _newStroke.GetBounds(); 

                                txtMsg.Text = string.Format("上:{0}; 下:{1}; 左:{2}; 右:{3}", 

                                     rect.Top, rect.Bottom, rect.Left, rect.Right); 

                void inkPresenter_MouseMove(object sender, MouseEventArgs e) 

                        if (_isCapture) 

                                if (_isEraser) 

                                { 

                                        // 擦除滑鼠目前位置所屬的 Stroke 

                                        RemoveStroke(e); 

                                } 

                                else if (_newStroke != null) 

                                        // 為已經添加到 InkPresenter 的 Strokes 中的 Stroke 增加 StylusPoint 

                                        _newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter)); 

                                        // 顯示該 Stroke 所在的 矩形範圍 的位置資訊 

                                        Rect rect = _newStroke.GetBounds(); 

                                        txtMsg.Text = string.Format("上:{0}; 下:{1}; 左:{2}; 右:{3}", 

                                                     rect.Top, rect.Bottom, rect.Left, rect.Right); 

                void inkPresenter_MouseLeftButtonUp(object sender, MouseEventArgs e) 

                        // UIElement.CaptureMouse() - 為 UIElement 對象釋放滑鼠捕捉 

                        // 為 InkPresenter 釋放滑鼠捕捉 

                        inkPresenter.ReleaseMouseCapture(); 

                        _newStroke = null; 

                        _isCapture = false; 

                void RemoveStroke(MouseEventArgs e) 

                        // Stroke.HitTest(StylusPointCollection) -    Stroke 是否與指定的 StylusPoint 集合相連 

                        // Strokes.HitTest(StylusPointCollection) - 與指定的 StylusPoint 集合相連的 Stroke 集合 

                        // 擷取目前滑鼠所在位置處的 StylusPoint 集合 

                        StylusPointCollection erasePoints = new StylusPointCollection(); 

                        erasePoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter)); 

                        // 與目前滑鼠所在位置處的 StylusPoint 集合相連的 Stroke 集合 

                        StrokeCollection hitStrokes = inkPresenter.Strokes.HitTest(erasePoints); 

                        for (int i = 0; i &lt; hitStrokes.Count; i++) 

                                // 在 InkPresenter 上清除指定的 Stroke 

                                inkPresenter.Strokes.Remove(hitStrokes[i]); 

                private void mediaElement_MediaEnded(object sender, RoutedEventArgs e) 

                        // 視訊播放完後,再重新播放 

                        mediaElement.Position = TimeSpan.FromMilliseconds(0); 

                        mediaElement.Play(); 

                private void ellipseRed_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 

                        // 單擊了 紅色取色點 

                        _currentColor = Colors.Red; 

                        inkPresenter.Cursor = Cursors.Stylus; 

                        _isEraser = false; 

                private void ellipseBlack_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 

                        // 單擊了 黑色取色點 

                        _currentColor = Colors.Black; 

                private void btnClear_Click(object sender, RoutedEventArgs e) 

                        // 單擊了 清除 按鈕 

                        inkPresenter.Strokes.Clear(); 

                private void btnEraser_Click(object sender, RoutedEventArgs e) 

                        // 單擊了 橡皮擦 按鈕 

                        inkPresenter.Cursor = Cursors.Eraser; 

                        _isEraser = true; 

        } 

}

OK

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/343057,如需轉載請自行聯系原作者

繼續閱讀