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) 自定义窗口的 ...
随机推荐
- Core Java Volume I — 4.5. Method Parameters
4.5. Method ParametersLet us review the computer science terms that describe how parameters can be p ...
- JSONP解决ajax跨域问题
在A域名下,用ajax请求B域名下的请求,会报类似这样的错误:No 'Access-Control-Allow-Origin' header is present on the requested r ...
- interproscan 的使用和遇到的问题
错误一: 2014-10-08 13:09:32,238 [uk.ac.ebi.interpro.scan.jms.worker.LocalJobQueueListener:193] ERROR - ...
- Linux虚拟主机通过程序实现二级域名绑定到子目录
虚拟主机中CP控制台不支持将二级域名绑定到子目录的功能,用户可以通过程序实现将二级域名绑定到子目录. 有两种方法将二级域名绑定到子目录: 1. 配置.htaccess, 通过伪静态代码实现.具体实现方 ...
- 关于获取目录的N种方法 的汇总
前段时间在Global.asax.cs中的Session_End中使用Server.MapPath() 出现"服务器操作在此上下文中不可用"异常. 网络上给出的解决方案:Syste ...
- UVa 1394 约瑟夫问题的变形
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- leetcode 110 Balanced Binary Tree ----- java
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- scala言语基础学习十
类型参数 泛型函数 多个参数 使用泛型参数时候,不给类型scala也能自己判断 上边界bounds 下边界bounds 专门用于打包泛型数组
- const 常引用
常类型是指使用类型修饰符 const 说明的类型,常类型的变量或对象的值是不能被更新的. 这篇主要说常引用.常引用是指所引用的对象不能被更新. 在实际应用中,常引用往往用来作为函数的形参,这样的参数称 ...
- Nginx重写规则指南 转
http://www.ttlsa.com/nginx/nginx-rewriting-rules-guide/ Nginx重写规则指南 当运维遇到要重写情况时,往往是要程序员把重写规则写好后,发给你, ...