Silverlight实现的简单拖拽效果
来源:互联网 发布:人工智能 电影 简介 编辑:程序博客网 时间:2024/05/21 08:00
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 = "鼠标移动中";
}
}
}
- Silverlight实现的简单拖拽效果
- Silverlight实例开发 简单的拖拽效果
- jq实现简单的拖拽效果
- JavaScript 实现简单的拖拽效果
- 使用Silverlight实现 FLASH的动画效果
- 在Silverlight中实现marquee的效果
- Silverlight结合html实现的 图表效果
- silverlight实现翻转效果
- silverlight 实现动画效果
- 使用CoreAnimation实现最简单的拖拽转盘效果
- JavaScript实现最简单的拖拽效果
- JavaScript实现最简单的拖拽效果
- Silverlight 4 报表打印的简单实现
- Silverlight 4 报表打印的简单实现
- Silverlight实现文件的下载[很简单]
- 简单探照灯遮照效果的几个Silverlight程序(Silverlight 2.0)
- Silverlight behavior 实现水印效果
- Silverlight behavior 实现倒影效果
- SecureCRT 连接远程主机速度慢的问题解决
- Linux的ls命令参数使用大全
- 蚊帐里的故事
- Linux的tail命令
- 如何为Oracle数据库进行备份
- Silverlight实现的简单拖拽效果
- 转载 微软软件测试的可借鉴之处
- ssh+Ext开发CRUD功能(1)
- C/C++中关于地址、指针和引用变量的学习笔记(三) : 结构
- websphere 备份与恢复
- 应用系统架构设计
- DBA警世录——有些习惯DBA须要养成(文摘)
- ssh+Ext开发CRUD功能(1)
- A tutorial on interfaces in C#