惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
量子位
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
博客园 - Franky
M
MIT News - Artificial intelligence
U
Unit 42
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
J
Java Code Geeks
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
T
The Blog of Author Tim Ferriss
V
V2EX

博客园 - Duan Junyi

招聘.net 架构师和项目经理 招聘.net/silverlight/wpf开发人员-HP 12月初在微软维亚大厦做的WPF,Silverlight培训资料下载 8月25日上午10点Webcast:Windows 7开发系列课程(1):Windows7新特性开发概览(Level 300) Windows 安全体系部分概述以及windows兼容性开发培训录像 OpenSearch for MSDN via Windows 7 Search Federation 简单写了一个windows7 native boot step bystep的文档 wpf/sl的overview资料 下载 Silverlight2 for mobile?? 12月4日上海IE8开发培训资料下载 用silverlight2.0给微软DPE做的一个简单的demo,欢迎下载 [分享视频下载]11月29日高阳和我在北京邮电大学的演讲(IT职业规划和软件加服务) 11月22日北京.net俱乐部活动ppt下载以及11月19日微软西格玛IE8开发讲座ppt下载 2008年11月22日北京.net俱乐部活动,欢迎大家用livemeeting收听 (已经结束) 微软TECHED2008 我的讲座:DEV354使用VSTS快速开发高性能WPF复合应用程序的PPT 经过修改MSN2009安装包,终于把最新版MSN安装到window server 2008下了 6月28日北京.net俱乐部活动小记以及PPT下载 silverlight2.0之--------基本控件 4月22日上午在西格玛三楼的讲座(wcf,wf,silverlight,cardspace)
silverlight2 beta 2 实现简单的手写功能
Duan Junyi · 2008-06-17 · via 博客园 - Duan Junyi

silverlight beta2 刚出来不久,变化很大,sdk里面的例子也不是很丰富

下面简单用托管代码写了一个小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>

下面就是通过写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,代码如下

    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

  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;
        }

大概就这样,大家根据这个进行扩展就行了
完整的代码我发到这个帖子里面了
http://bbs.crfly.com/showtopic-6552.aspx