silverlight beta2 刚出来不久,变化很大,sdk里面的例子也不是很丰富
下面简单用托管代码写了一个小demo来实现手写功能,(SDK上面有个用js写的),效果如下:
准备:
安装
Install Visual Studio 2008
Install Silverlight Tools Beta 2 for Visual Studio 2008
新建一个silverlight工程。
在xaml页面加入一个InkPresenter 控件,这个例子主要是对InkPresenter控件进行操作
把下面代码复制到xaml页面
下面就是通过写c#代码来操作了
注意到上面的InkPresenter控件里面有操作鼠标的方法,下面我们一个一个说
第一个方法inkCanvas_MouseLeftButtonDown
当鼠标左键按下去的时候开始取得当前坐标开始划线,这个方法的代码如下:
完整的代码我发到这个帖子里面了
http://bbs.crfly.com/showtopic-6552.aspx
下面简单用托管代码写了一个小demo来实现手写功能,(SDK上面有个用js写的),效果如下:
准备:
安装
Install Visual Studio 2008
Install Silverlight Tools Beta 2 for Visual Studio 2008
新建一个silverlight工程。
在xaml页面加入一个InkPresenter 控件,这个例子主要是对InkPresenter控件进行操作
把下面代码复制到xaml页面
<Grid x:Name="LayoutRoot" Background="BlueViolet">
<StackPanel>
<InkPresenter x:Name="inkCanvas" Width="300" Height="200" Margin="50" MouseLeftButtonDown="inkCanvas_MouseLeftButtonDown" Background="Khaki" MouseMove="inkCanvas_MouseMove" MouseLeftButtonUp="inkCanvas_MouseLeftButtonUp" MouseLeave="inkCanvas_MouseLeave">
</InkPresenter>
<Button x:Name="btnInk" Content="画笔" Width="50" Height="20" Click="btnInk_Click"></Button>
<Button x:Name="btnErase" Content="橡皮" Width="50" Height="20" Click="btnErase_Click"></Button>
</StackPanel>
</Grid>
<StackPanel>
<InkPresenter x:Name="inkCanvas" Width="300" Height="200" Margin="50" MouseLeftButtonDown="inkCanvas_MouseLeftButtonDown" Background="Khaki" MouseMove="inkCanvas_MouseMove" MouseLeftButtonUp="inkCanvas_MouseLeftButtonUp" MouseLeave="inkCanvas_MouseLeave">
</InkPresenter>
<Button x:Name="btnInk" Content="画笔" Width="50" Height="20" Click="btnInk_Click"></Button>
<Button x:Name="btnErase" Content="橡皮" Width="50" Height="20" Click="btnErase_Click"></Button>
</StackPanel>
</Grid>
下面就是通过写c#代码来操作了
注意到上面的InkPresenter控件里面有操作鼠标的方法,下面我们一个一个说
第一个方法inkCanvas_MouseLeftButtonDown
当鼠标左键按下去的时候开始取得当前坐标开始划线,这个方法的代码如下:
private void inkCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
inkCanvas.CaptureMouse();
Point p = e.GetPosition(inkCanvas);
StylusPoint sp = new StylusPoint();
sp.X = p.X;
sp.Y = p.Y;
if (_isErase)//判断是不是橡皮
{
_epoint = new StylusPointCollection();
_epoint.Add(sp);
}
else
{
_stroke = new Stroke();
_stroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkCanvas));
_stroke.DrawingAttributes = new DrawingAttributes();
//属性赋值
_stroke.DrawingAttributes.Color = Colors.Black;
_stroke.DrawingAttributes.Width = 2.00;
_stroke.DrawingAttributes.Height = 2.00;
_stroke.DrawingAttributes.OutlineColor = Colors.Green;
inkCanvas.Strokes.Add(_stroke);
}
}
写完这个方法以后,当鼠标左键点击的时候就会往页面上画点。那么我们怎么移动画线呢,写mousemove事件inkCanvas_MouseMove,代码如下{
inkCanvas.CaptureMouse();
Point p = e.GetPosition(inkCanvas);
StylusPoint sp = new StylusPoint();
sp.X = p.X;
sp.Y = p.Y;
if (_isErase)//判断是不是橡皮
{
_epoint = new StylusPointCollection();
_epoint.Add(sp);
}
else
{
_stroke = new Stroke();
_stroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkCanvas));
_stroke.DrawingAttributes = new DrawingAttributes();
//属性赋值
_stroke.DrawingAttributes.Color = Colors.Black;
_stroke.DrawingAttributes.Width = 2.00;
_stroke.DrawingAttributes.Height = 2.00;
_stroke.DrawingAttributes.OutlineColor = Colors.Green;
inkCanvas.Strokes.Add(_stroke);
}
}
private void inkCanvas_MouseMove(object sender, MouseEventArgs e)
{
Point p = e.GetPosition(inkCanvas);
StylusPoint sp = new StylusPoint();
sp.X = p.X;
sp.Y = p.Y;
//也可以用
// e.StylusDevice.GetStylusPoints(inkCanvas)
//来得到
if (_isErase)
{
if (_epoint != null)
{
_epoint.Add(sp);
StrokeCollection sc = inkCanvas.Strokes.HitTest(_epoint);
for (int i = 0; i < sc.Count; i++)
{
this.inkCanvas.Strokes.Remove(sc[i]);
}
}
}
else
{
if (_stroke != null)
{
_stroke.StylusPoints.Add(sp);
}
}
}
最后一步实现鼠标左键松开的事件inkCanvas_MouseLeftButtonUp{
Point p = e.GetPosition(inkCanvas);
StylusPoint sp = new StylusPoint();
sp.X = p.X;
sp.Y = p.Y;
//也可以用
// e.StylusDevice.GetStylusPoints(inkCanvas)
//来得到
if (_isErase)
{
if (_epoint != null)
{
_epoint.Add(sp);
StrokeCollection sc = inkCanvas.Strokes.HitTest(_epoint);
for (int i = 0; i < sc.Count; i++)
{
this.inkCanvas.Strokes.Remove(sc[i]);
}
}
}
else
{
if (_stroke != null)
{
_stroke.StylusPoints.Add(sp);
}
}
}
private void inkCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_stroke != null)
{
Point pos = e.GetPosition(inkCanvas);
StylusPoint sp = new StylusPoint();
sp.X = pos.X;
sp.Y = pos.Y;
_stroke.StylusPoints.Add(sp);
}
this.inkCanvas.ReleaseMouseCapture();
_stroke = null;
_epoint = null;
}
大概就这样,大家根据这个进行扩展就行了{
if (_stroke != null)
{
Point pos = e.GetPosition(inkCanvas);
StylusPoint sp = new StylusPoint();
sp.X = pos.X;
sp.Y = pos.Y;
_stroke.StylusPoints.Add(sp);
}
this.inkCanvas.ReleaseMouseCapture();
_stroke = null;
_epoint = null;
}
完整的代码我发到这个帖子里面了
http://bbs.crfly.com/showtopic-6552.aspx
DuanJunyi &&清新的风
------------------------------
箫心网
www.crfly.com
it.crfly.com
------------------------------
箫心网
www.crfly.com
it.crfly.com