Silverlight实现的简单拖拽效果

简介: MainPage.xaml文件代码:                                    -------------------------------------...

MainPage.xaml文件代码:

 

<UserControl
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="SilverlightApplication2.MainPage"
 Width="640" Height="480">

 <Canvas x:Name="LayoutRoot" Background="White">
  <StackPanel MouseLeftButtonDown="OnMouseDown" MouseMove="OnMouseMove" MouseLeftButtonUp="OnMouseLeftButtonUp" Width="200" Height="80" Canvas.Left="20" Canvas.Top="20">
   <Border BorderBrush="Red" BorderThickness="3">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
     <Image Source="qq.gif"/>
     <TextBlock Text="拖动"  Margin="20" VerticalAlignment="Center"/>
    </StackPanel>
   </Border>
  </StackPanel>
  <TextBlock x:Name="status" Canvas.Top="200" Text="" Canvas.Left="60"></TextBlock>
 </Canvas>
</UserControl>

---------------------------------------------------------------------------------------------------------------------------------

MainPage.xaml.cs文件如下:

 

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication2
{
 public partial class MainPage : UserControl
 {
  public MainPage()
  {
   // 为初始化变量所必需
   InitializeComponent();
  }

  //定义两个全局变量,一个用来获取鼠标的当前位置,另一个用来记录鼠标是否移动
  bool trackingMouseMove = false;
  Point mousePosition;
  private void OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  {
   // TODO: Add event handler implementation here.
   FrameworkElement element = sender as FrameworkElement;
   mousePosition = e.GetPosition(null);
   trackingMouseMove = true;
   if(null!=null)
   {
    element.CaptureMouse();
    element.Cursor = Cursors.Hand;
   }
   status.Text = "按下鼠标";
  }

  private void OnMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
  {
   // TODO: Add event handler implementation here.
   FrameworkElement element = sender as FrameworkElement;
   trackingMouseMove = false;
   element.ReleaseMouseCapture();
   mousePosition.X = mousePosition.Y = 0;
   element.Cursor = null;
   status.Text = "鼠标释放";
  }

  private void OnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  {
   // TODO: Add event handler implementation here.
   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);
   }
   status.Text = "鼠标移动中";
  }
 }
}

相关文章
|
前端开发 BI UED
Silverlight中的拖拽实现的图片上传
原文 http://blog.csdn.net/dujingjing1230/article/details/5443003 在Silverlight中因为可以直接从系统的文件夹里面拖出来一个文件直接放到浏览器中,我在想使用这个功能来做成图片上传(或者文件上传),这样的用户体验将会是非常好的。
804 0

热门文章

最新文章