众所周知,我们可以将鼠标放在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. 第七课第三节,T语言流程语句(版本5.0)

    for语句 for和while语句一样,都是用来做循环操作的,只是他们的使用方法不一样 (注:关键字,for,end) 执行流程图解: 实例代码: for(var i=0;i<20;i++) / ...

  2. java 压缩文件 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件

    /** * 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件 * * @param files * 文件数组 * @param strZipName * 压缩文件路径及文件名 * @thr ...

  3. 20151214下拉列表:DropDownList

    注意: .如果用事件的话就要把控件的AutoPostBack设置成true .防止网页刷新用一个判断 if (!IsPostBack)//判断是第一个开始还是取的返回值 { } 下拉列表:DropDo ...

  4. 【转载】IIS7.5(经典模式)访问静态资源(.css和.js文件)提示:未能执行 URL

    IIS7.5(经典模式)静态资源(.css和.js文件)提示:未能执行 URL “/”应用程序中的服务器错误. 未能执行 URL. 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪 ...

  5. 为什么要使用sass

    或许你已经听过一个叫作Sass的东东?可能你已经了解它,并且你能像大师一样写出一些函数? 对于不清楚我在讲什么的读者或者客户,你们可以想想web开发过程,你们的期望和站点用户的体验想要怎样的.无论如何 ...

  6. iOS 定位于地理反编码

    - (void)viewDidLoad { [self startLocation]; } //开始定位 -(void)startLocation{ self.locationManager = [[ ...

  7. springfox.documentation.service.ApiInfo配置示例

    Java Code Examples for springfox.documentation.service.ApiInfo The following are top voted examples ...

  8. webpack +vue开发(1)

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

  9. yield个人理解及简明示例

    1.写法有2种:yield return <expression>和yield breakyield用于在迭代中返回一个值,并将值带入下一次迭代中.yield break则意味着停止迭代. ...

  10. Visual Studio Enterprise 2015下载 Update3

    Visual Studio 2015 是一个丰富的集成开发环境,可用于创建出色的 Windows.Android 和 iOS 应用程序以及新式 Web 应用程序和云服务. 1.适用于各种规模和复杂程度 ...