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) 自定义窗口的 ...
随机推荐
- jquery ui tab标签
<!DOCTYPE html> <html> <head> <title>tab</title> <style type=" ...
- Core Java Volume I — 4.10. Class Design Hints
4.10. Class Design HintsWithout trying to be comprehensive or tedious, we want to end this chapter w ...
- vcffilter 工具bug以及解决办法
1,使用说明: usage: vcffilter [options] <vcf file> options: -f, --info-filter specifies a filte ...
- (实用篇)php中计算中文字符串长度、截取中文字符串的函数代码
在PHP中,我们都知道有专门的mb_substr和mb_strlen函数,可以对中文进行截取和计算长度,但是,由于这些函数并非PHP的核心函数,所以,它们常常有可能没有开启.当然,如果是用的自己的服务 ...
- mac中open用法
sage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <applicatio ...
- js图片无缝滚动代码
想必大家都注意到<marquee>的不循环滚动,所以出现了很多替代脚本,或iframe或JS输出<marquee>,不管怎么做,都略显麻烦.下面说一下这个相对简单的实现思路:一 ...
- eBay_GTC和Relist
1.销售里面有个GTC 那个可以给你做累计销量,如果累计销量高,能大幅提升你的排名位置也可以用30天的摆放方法,修改里面数量,但是30天结束后relist就不知结果了关于累计销量 google搜索里面 ...
- leetcode 146. LRU Cache ----- java
esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...
- POJ 1469 COURSES(二部图匹配)
COURSES Time Limit: 1000MS Memory ...
- Python文件格式化写入
[root@localhost test]# cat 1.py fd = open('format.txt','w') head = "%10s%10s%10s\n"%('id', ...