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) 自定义窗口的 ...
随机推荐
- TypeError: The CanvasRenderingContext2D.webkitBackingStorePixelRatio getter can only be used on instances of CanvasRenderingContext2D
ios10: CanvasRenderingContext2D.prototype.webkitBackingStorePixelRatio 报异常
- Think Python - Chapter 16 - Classes and functions
16.1 TimeAs another example of a user-defined type, we’ll define a class called Time that records th ...
- editplus 常用快捷键汇总 大小写代码折叠
文本类 新建普通文本:Ctrl+N新建浏览器窗口:Ctrl+Shift+B新建HTML页:Ctrl+Shift+N打开:Ctrl+O打开一个现有的文档文件结尾:Ctrl+End选区扩展到文档结尾处:C ...
- DISCOVAR de novo
海宝建议用这个拼接软件 http://www.broadinstitute.org/software/discovar/blog/?page_id=98 DISCOVAR – variant call ...
- andorid中Html.fromHtml方法
在android中,有一个容易遗忘的Html.fromhtml方法,意思是可以将比如文本框中的字符串进行HTML格式化,支持的还是很多的, 但要注意的是要在string.xml中用<!--cda ...
- Lock锁
Lock lock = new ReentrantLock(); lock.lock(); try { } finally { } 注意:不要将获取锁的过程写在try块中,因为如果在获取锁(自定义锁的 ...
- 全国信息学奥林匹克联赛 ( NOIP2014) 复赛 模拟题 Day1 长乐一中
题目名称 正确答案 序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer. ...
- UVa 1583 Digit Generator --- 水题+打表
UVa 1583 题目大意:如果x加上x的各个数字之和得到y,那么称x是y的生成元. 给定数字n,求它的最小生成元 解题思路:可以利用打表的方法,提前计算出以i为生成元的数,设为d,并保存在a[d]中 ...
- js类型判断
console.log('---------------------'); var a="string"; console.log(a); //string var a=1; co ...
- 论文笔记之:Learning to Track: Online Multi-Object Tracking by Decision Making
Learning to Track: Online Multi-Object Tracking by Decision Making ICCV 2015 本文主要是研究多目标跟踪,而 online ...