WPF拖到、移动控件
只需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拖到、移动控件的更多相关文章
- WPF拖拽文件(拖入拖出),监控拖拽到哪个位置,类似百度网盘拖拽
1.往wpf中拖文件 // xaml <Grid x:Name="grid_11" DragOver="Grid_11_DragOver" Drop=&q ...
- wpf 拖图片到窗体
前台代码:<Window x:Class="拖拽.MainWindow" xmlns="http://schemas.microsoft.com/wi ...
- wpf拖拽
简单拖拽的实现是,实现源控件的MouseDown事件,和目标控件Drop事件.调用DragDrop.DoDragDrop()以启动拖放操作,DragDrop.DoDragDrop()函数接受三个参数: ...
- 【WPF】拖拽ListBox中的Item
整理了两个关于WPF拖拽ListBox中的Item的功能.项目地址 https://github.com/Guxin233/WPF-DragItemInListBox 需求一: 两个ListBox,拖 ...
- WPF拖动总结[转载]
WPF拖动总结 这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中 ...
- [转载]WPF控件拖动
这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中间动画) 2.3拖动 ...
- wpf图片查看器,支持鼠标滚动缩放拖拽
最近项目需要,要用到一个图片查看器,类似于windows自带的图片查看器那样,鼠标滚动可以缩放,可以拖拽图片,于是就写了这个简单的图片查看器. 前台代码: <Window x:Class=&qu ...
- WPF如何实现拖拽打开文件(将文件拖进窗体打开)
在WPF中的实现和WinForm中的实现99%相似,将要实现接受拖拽释放的控件添加DragEnter事件和Drop事件,本例中控件Grid grid作为接受控件,添加事件操作如下: private v ...
- WPF.UIShell UIFramework之自定义窗口的深度技术 - 模态闪动(Blink)、窗口四边拖拽支持(WmNCHitTest)、自定义最大化位置和大小(WmGetMinMaxInfo)
无论是在工作和学习中使用WPF时,我们通常都会接触到CustomControl,今天我们就CustomWindow之后的一些边角技术进行探讨和剖析. 窗口(对话框)模态闪动(Blink) 自定义窗口的 ...
随机推荐
- php中将url中的参数含有%20进行转换或解码
我的url: .......index.php?action=search&start=12&search=star wave&orderby=categories&s ...
- CentOS 6.6 中设置Terminal快捷键
排版比较乱,参见 https://www.zybuluo.com/Jpz/note/144583 CentOS 6.6 中设置Terminal快捷键 Linux开发环境配置 Terminal是Cent ...
- ABAP DEMO
sap Program DEMO 介绍 Program Description BALVBT01 Example SAP program for displying multiple ALV repo ...
- Oracle字符集的查看查询和Oracle字符集的设置修改
本文主要讨论以下几个部分:如何查看查询oracle字符集. 修改设置字符集以及常见的oracle utf8字符集和oracle exp 字符集问题. 一.什么是Oracle字符集 Oracle字符集是 ...
- 【P1303】苹果二叉树
树归入门题 原题: 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点).这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1.我们用一根树枝两端连接的结点 ...
- kuangbin_ShortPath P (HDU 4725)
很有挑战的一题 直接暴力建图的话毫无疑问O(n^2)会TLE 每层虚拟一个点又会让没有点的层也能连过去 参考kuangbin菊苣的方法每层用了两个虚拟点 n+i*2-1 是入口 n+i*2 是出口 然 ...
- JAVA Lambda Expressions streams
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html https:// ...
- sed 神器
1. 删除一行 sed -i "1d" sedtest.txt 删除 sedtest.txt 第一行 2. 插入一行 sed '1i\0 ' sedtest.txt &g ...
- 二十四种设计模式:状态模式(State Pattern)
状态模式(State Pattern) 介绍允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它所属的类. 示例有一个Message实体类,对它的操作有Insert()和Get()方法, ...
- ASP.NET 各种缓存
原文地址:http://www.cnblogs.com/ltp/archive/2009/06/30/1514311.html 有时候总听到网友说网站运行好慢,不知如何是好:有时候也总见到一些朋友 ...