美女跟我走
前台:
<UserControl x:Class="SilverlightApplication1.SilverlightControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid Background="White" Name="LayOutGrid">
<Button Height="70" HorizontalAlignment="Left" ClickMode="Hover"
Margin="120,120,0,0" Name="button1" VerticalAlignment="Top" Width="164" MouseLeftButtonDown="button1_MouseLeftButtonDown" MouseLeftButtonUp="button1_MouseLeftButtonUp" MouseMove="button1_MouseMove">
<Button.Content>
<StackPanel Orientation="Horizontal" Width="157">
<Image Source="/SilverlightApplication1;component/img/1.jpg" Width="62"></Image>
<TextBlock FontSize="18" Text="跟我走" Height="46" Margin="20" Width="64"></TextBlock>
</StackPanel>
</Button.Content>
</Button>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="171,240,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Canvas.Left="-25" Canvas.Top="-8" />
</Grid>
</UserControl>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class SilverlightControl1 : UserControl
{
public SilverlightControl1()
{
InitializeComponent();
}
Point mousePosition;
bool trackingMouseMove = false;
//开始拖动对象
private void button1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBlock1.Text = "Test";
FrameworkElement element = sender as FrameworkElement;
trackingMouseMove = true;
mousePosition = e.GetPosition(null);
if (null != element)
{
//激活button拖动
element.CaptureMouse();
element.Cursor = Cursors.Hand;
}
}
//拖动完成
private void button1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
//停止button拖动
element.ReleaseMouseCapture();
element.Cursor = null;
trackingMouseMove = false;
mousePosition.X = mousePosition.Y = 0;
}
//移动对象
private void button1_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (trackingMouseMove)
{
double daltaV = e.GetPosition(null).X - mousePosition.X;
double daltaH = e.GetPosition(null).Y - mousePosition.Y;
Thickness tk = button1.Margin;
double newTop = daltaH + tk.Top;
double newLeft = daltaV + tk.Left;
tk.Top = newTop;
tk.Left = newLeft;
element.SetValue(Grid.MarginProperty, tk);
mousePosition = e.GetPosition(null);
textBlock1.Text = "X=" + tk.Top + ",Y=" + tk.Left + "," + tk.Bottom + "," + tk.Right;
}
}
}
}