微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight实现的简单拖拽效果

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 = "鼠标移动中";  } }}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐