SilverLight拖动具体实现方式介绍

开发 开发工具
SilverLight拖动的代码示例将会在文章为大家呈现出来。希望初学者们可以通过这些代码示例清晰的解读相关概念,以达到自己学习的目的。

SilverLight的使用可以帮助开发人员轻松的解决一些以前爱只能依靠美工才能解决的相关开发问题。现在我们将会学到其中的一个使用技巧,就是SilverLight拖动的实现方式。#t#

SilverLight拖动前台代码:

 

 

  1. < UserControl 
  2. xmlns="http://schemas.microsoft.
    com/winfx/2006/xaml/presentation"
     
  3. xmlns:x="http://schemas.
    microsoft.com/winfx/2006/xaml"
     
  4. x:Class="SilverlightApplic
    ation6.Page"
     
  5. Width="640" Height="480"> 
  6. < Canvas x:Name="LayoutRoot" 
    Background="White"> 
  7. < Image Margin="263,185,249,167" 
    Source="1.png" Stretch="Fill" 
    MouseLeftButtonDown="Image_
    MouseLeftButtonDown"
       
  8. MouseMove="Image_MouseMove" 
  9. MouseLeftButtonUp="Image_
    MouseLeftButtonUp"
    /> 
  10. < /Canvas> 
  11. < /UserControl> 

 

 

SilverLight拖动后台代码:

 

 

  1. using System;  
  2. using System.Windows;  
  3. using System.Windows.Controls;  
  4. using System.Windows.Documents;  
  5. using System.Windows.Ink;  
  6. using System.Windows.Input;  
  7. using System.Windows.Media;  
  8. using System.Windows.Media.Animation;  
  9. using System.Windows.Shapes;  
  10. namespace SilverlightApplication6  
  11. {  
  12. public partial class Page : UserControl  
  13. {  
  14. bool trackingMouseMove = false;  
  15. Point mousePosition;  
  16. public Page()  
  17. {  
  18. // 需要初始化变量  
  19. InitializeComponent();  
  20. }  
  21. private void Image_MouseLeftButtonDown
    (object sender, MouseButtonEventArgs e)  
  22. {  
  23. FrameworkElement element = 
    sender as FrameworkElement;  
  24. mousePosition = e.GetPosition(null);  
  25. trackingMouseMove = true;  
  26. if (null != element)  
  27. {  
  28. element.CaptureMouse();  
  29. element.Cursor = Cursors.Hand;  
  30. }  
  31. }  
  32. private void Image_MouseMove(object 
    sender, MouseEventArgs e)  
  33. {  
  34. FrameworkElement element = sender 
    as FrameworkElement;  
  35. if (trackingMouseMove)  
  36. {  
  37. double deltaV = e.GetPosition(null).
    Y - mousePosition.Y;  
  38. double deltaH = e.GetPosition(null).
    X - mousePosition.X;  
  39. double newTop = deltaV + (double)element.
    GetValue(Canvas.TopProperty);  
  40. double newLeft = deltaH + (double)
    element.GetValue(Canvas.LeftProperty);  
  41. element.SetValue(Canvas.TopProperty, newTop);  
  42. element.SetValue(Canvas.LeftProperty, newLeft);  
  43. mousePosition = e.GetPosition(null);  
  44. }  
  45. }  
  46. private void Image_MouseLeftButtonUp
    (object sender, MouseButtonEventArgs e)  
  47. {  
  48. FrameworkElement element = sender 
    as FrameworkElement;  
  49. trackingMouseMove = false;  
  50. element.ReleaseMouseCapture();  
  51. mousePositionmousePosition.X = 
    mousePosition
    .Y = 0;  
  52. element.Cursor = null;  
  53. }  
  54. }  

 

以上就是对SilverLight拖动相关实现方法做得具体介绍。

责任编辑:曹凯 来源: 博客园
相关推荐

2010-01-04 16:06:34

Silverlight

2009-12-29 18:34:21

Silverlight

2009-12-15 13:47:33

Silverlight

2009-12-31 15:05:00

Silverlight

2009-12-31 17:21:41

Silverlight

2009-12-31 14:12:40

Silverlight

2009-12-30 17:19:09

Silverlight

2009-12-30 16:43:47

Silverlight

2009-12-30 16:10:10

Silverlight

2009-12-28 13:23:19

WPF导出图片

2010-01-04 13:09:51

Silverlight

2009-09-07 13:25:56

Silverlight

2010-01-04 16:30:06

Silverlight

2009-12-30 17:44:22

Silverlight

2009-11-27 13:14:07

PHP函数strist

2010-06-11 16:19:23

vmware安装ope

2009-12-31 17:31:23

Silverlight

2009-12-30 14:36:29

Silverlight

2010-02-22 15:13:04

WCF分布式事务

2010-01-27 16:30:47

Android选项卡
点赞
收藏

51CTO技术栈公众号