只需2个事件和一个point变量即可:

 Point mouse_offset = , );
         void TC_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
             mouse_offset = Mouse.GetPosition(e.Source as FrameworkElement);
         }

         void TC_MouseMove(object sender, MouseEventArgs e)
         {
             FrameworkElement element = sender as FrameworkElement;

             if (e.LeftButton == MouseButtonState.Pressed)
             {
                 element.Cursor = Cursors.Hand;
                 Point mousePos = Mouse.GetPosition(e.Source as FrameworkElement);

                 double nTop = Canvas.GetTop(element) + mousePos.Y - mouse_offset.Y;

                 double nLeft = Canvas.GetLeft(element) + mousePos.X - mouse_offset.X;

                 //防止控件被拖出容器。
                 )
                     nTop = ;
                 if (nTop >= (CI.Height - element.DesiredSize.Height))
                     nTop = CI.Height - element.DesiredSize.Height;
                 )
                     nLeft = ;
                 if (nLeft >= (CI.Width - element.DesiredSize.Width))
                     nLeft = CI.Width - element.DesiredSize.Width;

                 Canvas.SetLeft(element, nLeft);
                 Canvas.SetTop(element, nTop);
             }
             else
             {
                 element.Cursor = null;
             }
         }

2014-09-15 Update:

后来发现,WPF当中用到界面变换的应用很多,所以,把这个抽象成一个类,如果要使用这些变换,只需要调用这个方法就可以了。

1、用于附加变换的类。

     public class RYMatrix
     {
         public RYMatrix(FrameworkElement Container,
             MouseButtonEventHandler MouseDown = null,
             MouseButtonEventHandler MouseUp = null,
             MouseWheelEventHandler MouseWheel = null,
             MouseEventHandler MouseMove = null)
         {
             Container.MouseDown -= Container_MouseDown;
             Container.MouseUp -= Container_MouseUp;
             Container.MouseWheel -= Container_MouseWheel;
             Container.MouseMove -= Container_MouseMove;

             Container.MouseDown += Container_MouseDown;
             Container.MouseUp += Container_MouseUp;
             Container.MouseWheel += Container_MouseWheel;
             Container.MouseMove += Container_MouseMove;

             this.MouseDown = MouseDown;
             this.MouseUp = MouseUp;
             this.MouseWheel = MouseWheel;
             this.MouseMove = MouseMove;
         }

         MouseButtonEventHandler MouseDown;
         MouseButtonEventHandler MouseUp;
         MouseWheelEventHandler MouseWheel;
         MouseEventHandler MouseMove;
         private bool CanRotate { get; set; }
         private Point RotatePoint { get; set; }
         private DateTime LastClick { get; set; }
         private Point LastPoint { get; set; }

         private void Container_MouseUp(object sender, MouseButtonEventArgs e)
         {
             RotatePoint = , -);
             if (CanRotate)
             {
                 CanRotate = false;
             }
             if (MouseUp != null)
                 MouseUp(sender, e);
         }
         private void Container_MouseDown(object sender, MouseButtonEventArgs e)
         {
             FrameworkElement FE = sender as FrameworkElement;
             if (FE == null)
                 return;
             Point p = e.MouseDevice.GetPosition(FE);
              && RotatePoint.Y == -))
             {
                 RotatePoint = p;
             }
             else if (Mouse.LeftButton == MouseButtonState.Pressed)
             {
                 )
                 {
                     Matrix m = FE.RenderTransform.Value;
                     m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
                     FE.RenderTransform = new MatrixTransform(m);
                 }
                 LastClick = DateTime.Now;
                 LastPoint = p;
             }
             else if (Mouse.RightButton == MouseButtonState.Pressed)
             {
             }
             if (MouseDown != null)
                 MouseDown(sender, e);
         }
         private void Container_MouseWheel(object sender, MouseWheelEventArgs e)
         {
             FrameworkElement FE = sender as FrameworkElement;
             Point p = e.MouseDevice.GetPosition(FE);
             Matrix m = FE.RenderTransform.Value;
             )
                 m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
             else
                 m.ScaleAtPrepend( /  / 1.1, p.X, p.Y);
             FE.RenderTransform = new MatrixTransform(m);
             if (MouseWheel != null)
             {
                 MouseWheel(sender, e);
             }
         }
         private void Container_MouseMove(object sender, MouseEventArgs e)
         {
             FrameworkElement FE = sender as FrameworkElement;
             Point NowPoint = e.GetPosition(FE);
             if (e.LeftButton == MouseButtonState.Pressed)
             {
                 double resultY = NowPoint.Y - LastPoint.Y + (double)FE.GetValue(Canvas.TopProperty);
                 double resultX = NowPoint.X - LastPoint.X + (double)FE.GetValue(Canvas.LeftProperty);
                 Matrix m = FE.RenderTransform.Value;
                 m.TranslatePrepend(resultX, resultY);
                 FE.RenderTransform = new MatrixTransform(m);
                 FE.Cursor = Cursors.Hand;
             }
             else
                  && RotatePoint.Y != -)
                 {
                     Matrix m = FE.RenderTransform.Value;
                     if (NowPoint.X > RotatePoint.X)
                     {
                         m.RotateAtPrepend(0.1, RotatePoint.X, RotatePoint.Y);
                     }
                     else if (NowPoint.X < RotatePoint.X)
                     {
                         m.RotateAtPrepend(-0.1, RotatePoint.X, RotatePoint.Y);
                     }
                     Debug.WriteLine(RotatePoint.X + "|" + RotatePoint.Y);
                     FE.RenderTransform = new MatrixTransform(m);
                     LastPoint = NowPoint;
                 }
                 else if (Mouse.RightButton == MouseButtonState.Pressed)
                 {
                     Matrix m = FE.RenderTransform.Value;
                     Debug.WriteLine(RotatePoint.X + "|" + RotatePoint.Y);
                     FE.RenderTransform = new MatrixTransform(m);
                     LastPoint = NowPoint;
                 }
                 else
                 {
                     FE.Cursor = null;
                 }
             if (MouseMove != null)
             {
                 MouseMove(sender, e);
             }
         }
     }

2、调用这个类的前端页面:

 <Window x:Class="WPFClient.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Title="MainWindow" Loaded="Window_Loaded">
     <Grid>
         <Canvas x:Name="Container"/>
     </Grid>
 </Window>

3、后台代码:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Canvas CI = new Canvas();
    Canvas.SetTop(CI, );
    Canvas.SetLeft(CI, );
    Image I = new Image();
    I.Source = new BitmapImage(new Uri("http://d.hiphotos.baidu.com/image/pic/item/00e93901213fb80e68fd3dd734d12f2eb9389485.jpg", UriKind.Absolute));
    CI.Children.Add(I);
    new RuyeeSoft.WPF.UIProcess.RYMatrix(CI,null,new MouseButtonEventHandler((o,d)=>{ MessageBox.Show("xx");}));
}

WPF拖到、移动控件的更多相关文章

  1. WPF拖拽文件(拖入拖出),监控拖拽到哪个位置,类似百度网盘拖拽

    1.往wpf中拖文件 // xaml <Grid x:Name="grid_11" DragOver="Grid_11_DragOver" Drop=&q ...

  2. wpf 拖图片到窗体

    前台代码:<Window x:Class="拖拽.MainWindow"        xmlns="http://schemas.microsoft.com/wi ...

  3. wpf拖拽

    简单拖拽的实现是,实现源控件的MouseDown事件,和目标控件Drop事件.调用DragDrop.DoDragDrop()以启动拖放操作,DragDrop.DoDragDrop()函数接受三个参数: ...

  4. 【WPF】拖拽ListBox中的Item

    整理了两个关于WPF拖拽ListBox中的Item的功能.项目地址 https://github.com/Guxin233/WPF-DragItemInListBox 需求一: 两个ListBox,拖 ...

  5. WPF拖动总结[转载]

    WPF拖动总结   这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中 ...

  6. [转载]WPF控件拖动

    这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中间动画) 2.3拖动 ...

  7. wpf图片查看器,支持鼠标滚动缩放拖拽

    最近项目需要,要用到一个图片查看器,类似于windows自带的图片查看器那样,鼠标滚动可以缩放,可以拖拽图片,于是就写了这个简单的图片查看器. 前台代码: <Window x:Class=&qu ...

  8. WPF如何实现拖拽打开文件(将文件拖进窗体打开)

    在WPF中的实现和WinForm中的实现99%相似,将要实现接受拖拽释放的控件添加DragEnter事件和Drop事件,本例中控件Grid grid作为接受控件,添加事件操作如下: private v ...

  9. WPF.UIShell UIFramework之自定义窗口的深度技术 - 模态闪动(Blink)、窗口四边拖拽支持(WmNCHitTest)、自定义最大化位置和大小(WmGetMinMaxInfo)

    无论是在工作和学习中使用WPF时,我们通常都会接触到CustomControl,今天我们就CustomWindow之后的一些边角技术进行探讨和剖析. 窗口(对话框)模态闪动(Blink) 自定义窗口的 ...

随机推荐

  1. Debian 入门安装与配置2

    Debian 入门安装与配置2 1. C/C++开发必装软件 atp-get install gcc    这个不用说,用来编译C程序 apt-get install g++ 用来编译C++程序 ap ...

  2. PAT (Basic Level) Practise:1005. 继续(3n+1)猜想

    [题目链接] 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进 ...

  3. poj 1475 uva 589 - Pushing Boxes

    题目大意 人推箱子从起点到终点,要求推箱子的次数最少,并打印出来人移动的路径. 题目分析 对于箱子进行宽搜的同时,要兼顾人是否能够把箱子推到相应的位置 每一次对箱子bfs 然后对人再bfs #incl ...

  4. UI学习笔记---第十二天UITabBarController

    页签视图控制器-UITabBarController   自定义UITabBar     block高级 一.UITabBarController 结构为三层:Tab bar controller v ...

  5. 关于kinect的安装与配置工作

    一 关于kinect的安装与配置工作 首先要注意的是,使用kinect进行开发,目前有两种不同的驱动方案,经测试这两种方案的驱动是不能兼容的,所以请务必选定其中一种(最好是卸载另外一种). 方案一:使 ...

  6. JavaWeb学习记录(一)——response响应头之缓存设置与下载功能的实现

    一.HTTP中常用响应头 Location: http://www.it315.org/index.jsp Server:apache tomcat Content-Encoding: gzip Co ...

  7. 《Java程序设计》第8周学习总结

    学号20145220 <Java程序设计>第8周学习总结 教材学习内容总结 15.1.1日志API简介 java.util.logging包提供了日志功能相关类与接口,不必额外配置日志组件 ...

  8. 利用Web服务器网络打洞

    好了有些标题党了.这里想说的是:某些网络,除了http 80服务,其它端口的服务都被限制了,这个时候可以用http web服务器来进行代理转发. 以Apache为例,支持ssh登录到其它服务器的配置如 ...

  9. Python学习(3)——if语句

    虽然在之前接触过C.C++.Java等,但是还是觉得Python写出来的好看o(≧v≦)o~,简洁明了! score = raw_input("score:") score=int ...

  10. IOS中打开应用实现检查更新的功能

    //检查更新页面 - (void)Renew{        NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary];        ...