源代码下载(vs2010版本):/Files/timy/dragAndreSize.rar
以下是拖放控件的实例代码:
<UserControl x:Class="SilverlightApplication26.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="#FFD9D7D7">
<StackPanel>
<Border Width="100" Height="50" CornerRadius="5" BorderBrush="Black" BorderThickness="3" MouseLeftButtonDown="Border_MouseLeftButtonDown" MouseMove="Border_MouseMove" MouseEnter="Border_MouseEnter" MouseLeave="Border_MouseLeave" MouseLeftButtonUp="Border_MouseLeftButtonUp">
<Button></Button>
</Border>
</StackPanel>
</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;
using System.Xml;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.IO;
using System.Windows.Browser;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Windows.Threading;
using System.Threading;
namespace SilverlightApplication26
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
bool captured = false;
double ox = -1, oy = -1;
enum State { None, Left, Buttom, Top, Right };
State currentstate;
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Border b = (Border)sender;
b.CaptureMouse();
captured = true;
}
private void Border_MouseMove(object sender, MouseEventArgs e)
{
CheckCursor(sender, e);
if (captured)
{
Border b = (Border)sender;
Point p = e.GetPosition(null);
if (ox != -1)
{
switch (currentstate)
{
case State.Right: { b.Width += (p.X - ox)*2; break;}
case State.Buttom: { b.Height += (p.Y - oy)*2; break; }
case State.Top: { b.Height += (oy - p.Y)*2; break; }
case State.Left: { b.Width += (ox - p.X)*2; break; }
}
}
ox = p.X;
oy = p.Y;
}
}
private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Cursor = Cursors.Arrow;
Border b = (Border)sender;
b.ReleaseMouseCapture();
captured = false;
ox = -1; oy = -1;
}
private void Border_MouseLeave(object sender, MouseEventArgs e)
{if(!captured)
Cursor = Cursors.Arrow;
}
private void Border_MouseEnter(object sender, MouseEventArgs e)
{
CheckCursor(sender, e);
}
void CheckCursor(object sender, MouseEventArgs e)
{
Border b = (Border)sender;
Point p = e.GetPosition(b);
if (p.X >= b.ActualWidth - b.BorderThickness.Right || p.X <= b.BorderThickness.Left)
{
Cursor = Cursors.SizeWE;
if (p.X >= b.ActualWidth - b.BorderThickness.Right) { currentstate = State.Right; }
else { currentstate = State.Left; }
}
else if (p.Y >= b.ActualHeight - b.BorderThickness.Bottom || p.Y <= b.BorderThickness.Top)
{
Cursor = Cursors.SizeNS;
if (p.Y >= b.ActualHeight - b.BorderThickness.Bottom) { currentstate = State.Buttom; }
else
{
currentstate = State.Top;
}
}
}
}
}
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="#FFD9D7D7">
<StackPanel>
<Border Width="100" Height="50" CornerRadius="5" BorderBrush="Black" BorderThickness="3" MouseLeftButtonDown="Border_MouseLeftButtonDown" MouseMove="Border_MouseMove" MouseEnter="Border_MouseEnter" MouseLeave="Border_MouseLeave" MouseLeftButtonUp="Border_MouseLeftButtonUp">
<Button></Button>
</Border>
</StackPanel>
</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;
using System.Xml;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.IO;
using System.Windows.Browser;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Windows.Threading;
using System.Threading;
namespace SilverlightApplication26
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
bool captured = false;
double ox = -1, oy = -1;
enum State { None, Left, Buttom, Top, Right };
State currentstate;
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Border b = (Border)sender;
b.CaptureMouse();
captured = true;
}
private void Border_MouseMove(object sender, MouseEventArgs e)
{
CheckCursor(sender, e);
if (captured)
{
Border b = (Border)sender;
Point p = e.GetPosition(null);
if (ox != -1)
{
switch (currentstate)
{
case State.Right: { b.Width += (p.X - ox)*2; break;}
case State.Buttom: { b.Height += (p.Y - oy)*2; break; }
case State.Top: { b.Height += (oy - p.Y)*2; break; }
case State.Left: { b.Width += (ox - p.X)*2; break; }
}
}
ox = p.X;
oy = p.Y;
}
}
private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Cursor = Cursors.Arrow;
Border b = (Border)sender;
b.ReleaseMouseCapture();
captured = false;
ox = -1; oy = -1;
}
private void Border_MouseLeave(object sender, MouseEventArgs e)
{if(!captured)
Cursor = Cursors.Arrow;
}
private void Border_MouseEnter(object sender, MouseEventArgs e)
{
CheckCursor(sender, e);
}
void CheckCursor(object sender, MouseEventArgs e)
{
Border b = (Border)sender;
Point p = e.GetPosition(b);
if (p.X >= b.ActualWidth - b.BorderThickness.Right || p.X <= b.BorderThickness.Left)
{
Cursor = Cursors.SizeWE;
if (p.X >= b.ActualWidth - b.BorderThickness.Right) { currentstate = State.Right; }
else { currentstate = State.Left; }
}
else if (p.Y >= b.ActualHeight - b.BorderThickness.Bottom || p.Y <= b.BorderThickness.Top)
{
Cursor = Cursors.SizeNS;
if (p.Y >= b.ActualHeight - b.BorderThickness.Bottom) { currentstate = State.Buttom; }
else
{
currentstate = State.Top;
}
}
}
}
}