众所周知,我们可以将鼠标放在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的更多相关文章

  1. c# 可移动可改变大小的控件

    因为业务需要,百度了个可移动可改变大小的控件,然后自己修改了下,功能类似vs的设计面板中的功能差不多,可拖拽,改变大小 拖动的 public class MoveControl { #region 自 ...

  2. 快速构建Windows 8风格应用4-FlipView数据控件

    原文:快速构建Windows 8风格应用4-FlipView数据控件 本篇博文主要介绍为什么使用FlipView控件.什么是FlipView控件.如何使用FlipView控件和FlipView控件最佳 ...

  3. 快速构建Windows 8风格应用5-ListView数据控件

    原文:快速构建Windows 8风格应用5-ListView数据控件 本篇博文主要介绍什么是ListView数据控件.如何构建ListView数据控件. 什么是ListView数据控件? 1)  Li ...

  4. 快速构建Windows 8风格应用6-GridView数据控件

    原文:快速构建Windows 8风格应用6-GridView数据控件 本篇博文主要介绍什么是GridView数据控件.如何构建常用的GridView数据呈现样式. 什么是GridView数据控件? G ...

  5. 快速构建Windows 8风格应用19-基础控件II

    原文:快速构建Windows 8风格应用19-基础控件II 本篇博文接着上篇博文<快速构建Windows 8风格应用18-基础控件I>介绍开发Windows 8风格应用中常用控件. Sli ...

  6. DELPHI中如何让FORM窗体透明,只显示控件?

    DELPHI中如何让FORM窗体透明,只显示控件?分享到: 对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 回复次数:7largewanglargewanglargewang等级:Blank ...

  7. 重新想象 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 ...

  8. 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性

    [源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件 ...

  9. 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup

    [源码下载] 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup 作者:webabcd 介绍重新想象 Wind ...

随机推荐

  1. Selenium 中 cssSelector定位

    一.为什么使用cssSelector定位元素? 目前针对一些常规定位方式有:By.id.By.name.By.LinkTest(针对<a>标签).By.ClassName 针对不太好定位的 ...

  2. Ubuntu下两个gcc版本切换

    Ubuntu系统使用的gcc版本随着发布版本的不同而不同,在编译Android系统时不同的版本推荐用不同的gcc去编译,那么可不可以改变系统的gcc来适应android编译环境的需求呢?答案是可以的. ...

  3. Python for loop and while loop

    #!pyton2 #-*- coding:utf-8 -*- for letter in "Python": print "Current letter is:" ...

  4. LVS+Keepalived负载均衡配置

    简介 lvs一般是和keepalived一起组合使用的,虽然也可以单独使用lvs,但配置比较繁琐,且可用性也没有前者高. lvs和keepalived组合使用后,配置lvs的VIP和负载均衡就都在ke ...

  5. List和Map之间的转换和关联

    首先,Map.values返回的是此Map中包含的所有值的collection视图. 然后利用ArrayList的构造器ArrayList(Collection<? extends E> ...

  6. Oracle存储过程 输出参数赋值异常:“Oracle.DataAccess.Types.OracleString”的类型初始值设定项引发异常。

    场景: 写了一个有返回参数的存储过程,在个另开发人员机器上都正常.其它机器报如题错误.让人郁闷的是,所有调用方都是客户端,根本不存在网上众贴所说的版本不一致问题. 分析: 虽然网上的帖子没有根本解决问 ...

  7. loadView、viewDidLoad、initWithCoder、initWithNibName、awakeFromNib的用法

    转载,原地址为:http://jianyu996.blog.163.com/blog/static/11211455520131226840879/ 请尊重原创: 1,无论XIB还是代码创建都会调用l ...

  8. webpack +vue开发(1)

    首先安装 node.js这是毋庸置疑的,安装完了之后安装webpack npm install webpack -g 接下来创建一个自己的文件夹 webpack-learn在里面创建一个index.h ...

  9. springboot + swagger

    swagger用于定义API文档. 好处: 前后端分离开发 API文档非常明确 测试的时候不需要再使用URL输入浏览器的方式来访问Controller 传统的输入URL的测试方式对于post请求的传参 ...

  10. python装饰器示例

    https://wiki.python.org/moin/PythonDecoratorLibrary#Property_Definition