这次实现了拖动和拖动边界控制:
XMAL:
<UserControl x:Class="DesignerSL.Views.Gifts.MiniPhotoControl"
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"
xmlns:adorners="clr-namespace:Adorners"
mc:Ignorable="d"
d:DesignHeight="301" d:DesignWidth="222" Loaded="UserControl_Loaded">
<Grid x:Name="LayoutRoot" Background="White">
<Grid Name="gidPanel" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.ColumnSpan="3" Grid.RowSpan="3" Margin="0" >
<Canvas x:Name="panelDisplay">
<Canvas.Clip>
<RectangleGeometry x:Name="RectangGeome" />
</Canvas.Clip>
<Image Name="imgPhoto" Height="auto" Width="auto" Canvas.ZIndex="0" MouseLeftButtonDown="imgPhoto_MouseLeftButtonDown" MouseLeftButtonUp="imgPhoto_MouseLeftButtonUp" MouseMove="imgPhoto_MouseMove">
<!--<Image.Clip>
<RectangleGeometry x:Name="RectangGeome" />
</Image.Clip>-->
</Image>
</Canvas>
<!--<adorners:CursorPlane x:Name='cursorPlane' Canvas.Left="-38" Canvas.Top="-2" IsHitTestVisible="True" />-->
</Grid>
</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.Windows.Media.Imaging;
using Adorners;
using Adorners.Frames;
namespace DesignerSL.Views.Gifts
{
public partial class MiniPhotoControl : UserControl
{
CropFrame frame2 = new CropFrame();
public double ImageX;
public double ImageY;
public PhotoInfo MiniImage { get; set; }
public double ControlHeight;
public double ControlWidth;
private double MengbanX;
private double MengbanY;
//bool trackingMouseMove = false;
//double deltax;
//double deltay;
//Point _point;
public MiniPhotoControl()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
this.Height = ControlHeight;
this.Width = ControlWidth;
imgPhoto.RenderTransform = new TranslateTransform();
RectangGeome.Rect = new Rect(0, 0, ControlWidth, ControlHeight);
AddPhoto();
}
private void AddPhoto()
{
WriteableBitmap PhotoData = MiniImage.PhotoData;
if (ControlHeight / PhotoData.PixelHeight < ControlWidth / PhotoData.PixelWidth)
{
imgPhoto.Width = ControlWidth;
imgPhoto.Height = PhotoData.PixelHeight * (ControlWidth / PhotoData.PixelWidth);
MengbanX = ImageX = 0;
MengbanY = ImageY = -(imgPhoto.Height - ControlHeight) / 2;
Canvas.SetLeft(imgPhoto, 0);
Canvas.SetTop(imgPhoto, MengbanY);
//RectangGeome.Rect = new Rect(0, (imgPhoto.Height - ControlHeight) / 2, ControlWidth, ControlHeight);
}
else
{
imgPhoto.Height = ControlHeight;
imgPhoto.Width = PhotoData.PixelWidth * (ControlHeight / PhotoData.PixelHeight);
MengbanX = ImageX = -(imgPhoto.Width - ControlWidth) / 2;
MengbanY = ImageY = 0;
Canvas.SetTop(imgPhoto, 0);
Canvas.SetLeft(imgPhoto, MengbanX);
//RectangGeome.Rect = new Rect((imgPhoto.Width - ControlWidth) / 2,0, ControlWidth, ControlHeight);
}
imgPhoto.Source = PhotoData;
}
void imgPhoto_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
trackingMouseMove = false;
element.ReleaseMouseCapture();
mousePosition.X = mousePosition.Y = 0;
element.Cursor = null;
ImageX = (double)element.GetValue(Canvas.LeftProperty);
ImageY = (double)element.GetValue(Canvas.TopProperty);
}
bool trackingMouseMove = false;
Point mousePosition;
void imgPhoto_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
mousePosition = e.GetPosition(null);
trackingMouseMove = true;
if (null != element)
{
element.CaptureMouse();
element.Cursor = Cursors.Hand;
}
}
void imgPhoto_MouseMove(object sender, MouseEventArgs e)
{
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);
if (newTop < 0 & newTop > -System.Math.Abs(element.Height - ControlHeight))
element.SetValue(Canvas.TopProperty, newTop);
else if (newTop > 0)
{
element.SetValue(Canvas.TopProperty, 0.00);
//trackingMouseMove = false;
}
else if (newTop < -System.Math.Abs(element.Height - ControlHeight))
{
element.SetValue(Canvas.TopProperty, -System.Math.Abs(element.Height - ControlHeight));
//trackingMouseMove = false;
}
if (newLeft < 0 & newLeft > -System.Math.Abs(element.Width - ControlWidth))
element.SetValue(Canvas.LeftProperty, newLeft);
else if (newLeft > 0)
{
element.SetValue(Canvas.LeftProperty, 0.00);
//trackingMouseMove = false;
}
else if (newLeft < -System.Math.Abs(element.Width - ControlWidth))
{
element.SetValue(Canvas.LeftProperty, -System.Math.Abs(element.Width - ControlWidth));
//trackingMouseMove = false;
}
mousePosition = e.GetPosition(null);
}
}
}
}