如何实现能像windows 窗体一样改变大小的控件 Silverlight
众所周知,我们可以将鼠标放在windows窗体的边框上,按住鼠标左键改变窗体大小。那么,在silverlight上如何实现呢?
1. 需要将改控件放置在canvas上。
2. 判断鼠标位置,然后将Arrow鼠标形状改变为相应的Resize形状(本实例默认当鼠标处于边框内5px时,可resize):
//the left top corner
if (location.Y < && location.X <)
{
this.Cursor = Cursors.SizeNWSE;
currentEdgeCorner = EdgeCorner.LeftTopCorner;
}
//the right top corner
else if (location.Y < && this.Width - location.X <)
{
this.Cursor = Cursors.SizeNESW;
currentEdgeCorner = EdgeCorner.RightTopCorner;
}
//the right bottom corner
else if (this.Width - location.X < && this.Height - location.Y <)
{
this.Cursor = Cursors.SizeNWSE;
currentEdgeCorner = EdgeCorner.RightBottomCorner;
}
// the left bottom corner
else if (location.X < && this.Height - location.Y <)
{
this.Cursor = Cursors.SizeNESW;
currentEdgeCorner = EdgeCorner.LeftBottomCorner;
}
//the left edge
else if (location.X <)
{
this.Cursor = Cursors.SizeWE;
currentEdgeCorner = EdgeCorner.LeftEdge;
}
//the right edge
else if (this.Width - location.X <)
{
this.Cursor = Cursors.SizeWE;
currentEdgeCorner = EdgeCorner.RightEdge;
}
//the bottom edge
else if (this.Height - location.Y <)
{
this.Cursor = Cursors.SizeNS;
currentEdgeCorner = EdgeCorner.BottomEdge;
}
//the top edge
else if (location.Y <)
{
this.Cursor = Cursors.SizeNS;
currentEdgeCorner = EdgeCorner.TopEdge;
}
else
{
this.Cursor = Cursors.Arrow;
currentEdgeCorner = EdgeCorner.Center;
}
2. 在控件的mousemove事件里视情况设置高度,宽度,位置信息:
2.1 当移动右边框时,只需要改变宽度。
2.2 当移动左边框时,在改变宽度的同时要改变控件的位置:当宽度增加向量△,那么Canvas.Left要减少向量△。
2.3 其他位置同理:
Point _current = e.GetPosition(this.Parent as UIElement);
double newHeight = this.Height;
double newWidth = this.Width; if (this.Cursor == Cursors.SizeWE)
{
if (currentEdgeCorner == EdgeCorner.RightEdge)
{
newWidth = orgSize.X + (_current.X - _rootPosition.X);
if (newWidth < )
return;
}
else
{
newWidth = orgSize.X - (_current.X - _rootPosition.X);
if (newWidth < )
return;
this.SetValue(Canvas.LeftProperty, orgLoc.X + (_current.X - _rootPosition.X));
}
}
if (this.Cursor == Cursors.SizeNS)
{
if (currentEdgeCorner == EdgeCorner.BottomEdge)
{
newHeight = orgSize.Y + (_current.Y - _rootPosition.Y);
if (newHeight < )
return;
}
else
{
newHeight = orgSize.Y - (_current.Y - _rootPosition.Y);
if (newHeight < )
return;
this.SetValue(Canvas.TopProperty, orgLoc.Y + (_current.Y - _rootPosition.Y));
}
} if (this.Cursor == Cursors.SizeNESW)
{
if (currentEdgeCorner == EdgeCorner.RightTopCorner)
{
newHeight = orgSize.Y - (_current.Y - _rootPosition.Y);
newWidth = orgSize.X + (_current.X - _rootPosition.X);
if (newHeight < || newWidth < )
return;
this.SetValue(Canvas.TopProperty, orgLoc.Y + (_current.Y - _rootPosition.Y));
}
else
{
newHeight = orgSize.Y + (_current.Y - _rootPosition.Y);
newWidth = orgSize.X - (_current.X - _rootPosition.X);
if (newHeight < || newWidth < )
return;
this.SetValue(Canvas.LeftProperty, orgLoc.X + (_current.X - _rootPosition.X));
}
}
if (this.Cursor == Cursors.SizeNWSE)
{
if (currentEdgeCorner == EdgeCorner.LeftTopCorner)
{
newHeight = orgSize.Y - (_current.Y - _rootPosition.Y);
newWidth = orgSize.X - (_current.X - _rootPosition.X);
if (newHeight < || newWidth < )
return;
this.SetValue(Canvas.TopProperty, orgLoc.Y + (_current.Y - _rootPosition.Y));
this.SetValue(Canvas.LeftProperty, orgLoc.X + (_current.X - _rootPosition.X));
}
else
{
newHeight = orgSize.Y + (_current.Y - _rootPosition.Y);
newWidth = orgSize.X + (_current.X - _rootPosition.X);
if (newHeight < || newWidth < )
return;
}
}
this.Height = newHeight;
this.Width = newWidth;
当要设置位置信息Canvas.Top, Canvas.Left时,必须特别用此控件的父类或者其他不动点的相对值,即
Point _current = e.GetPosition(this.Parent as UIElement);
是正确的,但
Point _current = e.GetPosition(this);
是不正确的。
如何实现能像windows 窗体一样改变大小的控件 Silverlight的更多相关文章
- c# 可移动可改变大小的控件
因为业务需要,百度了个可移动可改变大小的控件,然后自己修改了下,功能类似vs的设计面板中的功能差不多,可拖拽,改变大小 拖动的 public class MoveControl { #region 自 ...
- 快速构建Windows 8风格应用4-FlipView数据控件
原文:快速构建Windows 8风格应用4-FlipView数据控件 本篇博文主要介绍为什么使用FlipView控件.什么是FlipView控件.如何使用FlipView控件和FlipView控件最佳 ...
- 快速构建Windows 8风格应用5-ListView数据控件
原文:快速构建Windows 8风格应用5-ListView数据控件 本篇博文主要介绍什么是ListView数据控件.如何构建ListView数据控件. 什么是ListView数据控件? 1) Li ...
- 快速构建Windows 8风格应用6-GridView数据控件
原文:快速构建Windows 8风格应用6-GridView数据控件 本篇博文主要介绍什么是GridView数据控件.如何构建常用的GridView数据呈现样式. 什么是GridView数据控件? G ...
- 快速构建Windows 8风格应用19-基础控件II
原文:快速构建Windows 8风格应用19-基础控件II 本篇博文接着上篇博文<快速构建Windows 8风格应用18-基础控件I>介绍开发Windows 8风格应用中常用控件. Sli ...
- DELPHI中如何让FORM窗体透明,只显示控件?
DELPHI中如何让FORM窗体透明,只显示控件?分享到: 对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 回复次数:7largewanglargewanglargewang等级:Blank ...
- 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图
[源码下载] 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Co ...
- 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性
[源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件 ...
- 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup
[源码下载] 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup 作者:webabcd 介绍重新想象 Wind ...
随机推荐
- js实现cookie记住密码
近来做记住密码时,用js的实现方式做了一下. login.jsp页面代码 <%@ page language="java" import="java.util.*& ...
- java 压缩文件 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件
/** * 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件 * * @param files * 文件数组 * @param strZipName * 压缩文件路径及文件名 * @thr ...
- 自动选中div中的文字
<html> <head> <title></title> <script type="text/javascript" de ...
- [WCF]设置拦截器捕捉到request和reply消息
WCF进阶学习ing... 在熟练掌握了ABC的使用以后,就开始想着去了解WCF是怎么通信的了.首先是服务描述语言wsdl,它定义了服务的描述等等,用于让外界知道这个服务的ABC是什么.另外一个比较重 ...
- Rabbitmq集群高可用测试
Rabbitmq集群高可用 RabbitMQ是用erlang开发的,集群非常方便,因为erlang天生就是一门分布式语言,但其本身并不支持负载均衡. Rabbit模式大概分为以下三种:单一模式.普通模 ...
- WS-Security
ylbtech-Miscellaneos: WS-Security A,返回顶部 1, WS-Security (Web服务安全) 是一种提供在Web服务上应用安全的方法的网络传输协议. 2004年4 ...
- 解决【win10管理员已阻止程序运行】问题时有感
今天在安装loadrunner11的时候点击setup弹出以下报错 然后试了很多方法,从网上找了各种解决方案:修改UAC.修改本地组策略,均未解决ps:本人电脑是win10家庭中文版. 研究了半天未果 ...
- POS管理系统之设备出库
JSP: <html> <head> <title>登录</title> <style type="text/css&quo ...
- 主机+虚拟机ubuntu+mini2440开发板互相ping通
折腾这么久,终于将主机,虚拟机和开发板三者之间能够相互ping通,虽然还没有实现我要的功能,不管怎么说先将步骤简单的概括下,用交叉网线将开发板与主机相连,开发板与主机的ip要设置在同一网段内,在配置u ...
- 为什么C++中空类和空结构体大小为1?(转载)
原文链接:http://www.spongeliu.com/260.html 对于结构体和空类大小是1这个问题,首先这是一个C++问题,在C语言下空结构体大小为0(当然这是编译器相关的).这里的空类和 ...