一步一步学Silverlight 2系列(5):实现简单的拖放功能

一步一步学Silverlight 2系列(5):实现简单的拖放功能,第1张

概述概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章带您快速进入Silverlight 2开发。 概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic,Visual C#,IronRuby,Ironpython,对JsON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章带您快速进入Silverlight 2开发。 本文为系列文章第五篇,利用前面讲过的鼠标事件处理实现简单的拖放功能。 准备XAML 在实现拖放功能中,分为三个步骤: 1.按下鼠标,触发MouseleftbuttonDown事件,选择要拖动的对象。 2.移动鼠标,触发MouseMove事件,移动选择的对象。 3.放开鼠标,触发MouseleftbuttonUp事件,停止捕捉事件。 做一个简单的界面,用一个按钮来显示拖放,如下XAML声明:
<Canvas Background="#46461F">    <button        MouseleftbuttonDown="OnMouseDown"        MouseMove="OnMouseMove"       MouseleftbuttonUp="onmouseup"        Canvas.left="50" Canvas.top="50" Background="Red"       FontSize="18"       WIDth="160" Height="80">        <button.Content>            <StackPanel OrIEntation="Horizontal" HorizontalAlignment="Center"                        VerticalAlignment="Center">                <Image Source="smile_6.png"></Image>                <TextBlock Text="拖动我" VerticalAlignment="Center" margin="10"></TextBlock>            </StackPanel>        </button.Content>    </button></Canvas>
这里为了界面显示效果,使用了控件模板,后续会专门讲到。 开始拖放 *** 作 开始拖放 *** 作,实现MouseleftbuttonDown事件处理程序,用两个全局变量来记录当前鼠标的位置和鼠标是否保持移动。
bool trackingMouseMove = false;Point mouseposition;voID OnMouseDown(object sender,MousebuttonEventArgs e){    FrameworkElement element = sender as FrameworkElement;    mouseposition = e.Getposition(null);    trackingMouseMove = true;    if (null != element)    {        element.CaptureMouse();        element.Cursor = Cursors.Hand;    }}
移动对象 移动对象,实现MouseMove事件处理程序,计算元素的位置并更新,同时更新鼠标的位置。
voID OnMouseMove(object sender,MouseEventArgs e){    FrameworkElement element = sender as FrameworkElement;    if (trackingMouseMove)    {        double deltaV = e.Getposition(null).Y - mouseposition.Y;        double deltaH = e.Getposition(null).X - mouseposition.X;        double newtop = deltaV + (double)element.GetValue(Canvas.topProperty);        double newleft = deltaH + (double)element.GetValue(Canvas.leftProperty);        element.SetValue(Canvas.topProperty,newtop);        element.SetValue(Canvas.leftProperty,newleft);        mouseposition = e.Getposition(null);    }}
完成拖放 *** 作 完成拖放 *** 作,实现MouseleftbuttonUp事件处理程序。
voID onmouseup(object sender,MousebuttonEventArgs e){    FrameworkElement element = sender as FrameworkElement;    trackingMouseMove = false;    element.ReleaseMouseCapture();    mouseposition.X = mouseposition.Y = 0;    element.Cursor = null;}
效果显示 最终,完成后的效果如下

  拖动按钮

  结束语 本文实现了一个简单的拖放功能(示例来自于Silverlight 2 SDK),点击 下载文本示例代码。 总结

以上是内存溢出为你收集整理的一步一步学Silverlight 2系列(5):实现简单的拖放功能全部内容,希望文章能够帮你解决一步一步学Silverlight 2系列(5):实现简单的拖放功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1062738.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存