silverlight3 学习 3,绘图--鼠标轨迹画线

新建一个Silverlight Navigation Application

修改Home.xaml

<navigation:Page x:Class="SilverlightApplication2.Home"
    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"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
    Title="Home"
    Style="{StaticResource PageStyle}">

 

    <Grid x:Name="LayoutRoot" Background="AliceBlue" >
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}"  Grid.Row="0">
            <StackPanel x:Name="ContentStackPanel" Orientation="Horizontal" Height="20">
                <TextBox x:Name="txtPoint" Width="100"></TextBox>
            </StackPanel>
        </ScrollViewer>
        <InkPresenter x:Name="inkP" Grid.Row="1" Background="Black" MouseLeftButtonDown="inkP_MouseLeftButtonDown" MouseMove="inkP_MouseMove" LostMouseCapture="inkP_LostMouseCapture"></InkPresenter>
       
    </Grid>

</navigation:Page>

修改Home.xaml.cs

public partial class Home : Page
    {
        private Stroke MyStroke;
        public Home()
        {
            InitializeComponent();
           
        }
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        private void inkP_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            inkP.CaptureMouse();
            StylusPointCollection MySpc = new StylusPointCollection();
            MySpc.Add(e.StylusDevice.GetStylusPoints(inkP));
            MyStroke = new Stroke(MySpc);
            MyStroke.DrawingAttributes = new DrawingAttributes();
            MyStroke.DrawingAttributes.Color = Colors.White;
            inkP.Strokes.Add(MyStroke);
        }

        private void inkP_MouseMove(object sender, MouseEventArgs e)
        {
            if (MyStroke != null)
            {
                MyStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkP));
                txtPoint.Text = "" + e.StylusDevice.GetStylusPoints(inkP)[0].X+","+ e.StylusDevice.GetStylusPoints(inkP)[0].Y;
            }

        }

        private void inkP_LostMouseCapture(object sender, MouseEventArgs e)
        {
            MyStroke = null;
        }       
    }
说明:

InkPresenter 继承自 Canvas

posted @ 2010-10-20 16:53  base  阅读(1144)  评论(0编辑  收藏  举报