众所周知,我们可以将鼠标放在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. 分支语句 if的嵌套 循环语句

    0930 今天学习内容做以下总结: 语句的分类:顺序语句,分支语句(选择,条件),循环语句 分支语句 格式1:if(表达式(要么是true 要么是false)){} 格式2:if(){}slse{}  ...

  2. 龙威零式_团队项目例会记录_18 (Beta架构讨论)

    例会照片 任务更新 姓名 今日完成任务 实际花费时间 明日任务 预计花费时间 谢振威 继续构思beta版本架构并且输出文档 2h #40数据库模块接口定义 2h 杨金键 继续构思beta版本架构并且输 ...

  3. VS2013添加使用lib的方法

    使用第三方厂家的库,一般都会有三个文件: xxx.h xxx.lib xxx.dll 头文件很明显,就在需要用到的文件里面include就好. lib文件,可以直接在IDE中添加,具体步骤有两个: 1 ...

  4. mac 下安装 lua5.3 + cjson

    1.lua 5.3的安装 直接去官网下载 http://www.lua.org/ftp/lua-5.3.3.tar.gz make macosx sudo make install 2.CSJON 编 ...

  5. IE中Keep-Alive机制引起的错误

    我们知道Http协议是基于TCP/IP连接的,也就是说客户端浏览器向服务器发出一个Http请求并得到响应是要建立一条TCP/IP连接的,但是如果每发出一个Http请求客户端就要向服务器端建立一条TCP ...

  6. linux服务之irqbalance

    irqbalance 理论上:启用 irqbalance 服务,既可以提升性能,又可以降低能耗.irqbalance 用于优化中断分配,它会自动收集系统数据以分析使用模式,并依据系统负载状况将工作状态 ...

  7. 如果在代码中使用JS

    protected void Page_Load(object sender, EventArgs e)    {        //获得.js文件        string myscript = ...

  8. 如何解决虚拟机克隆导致"Bringing up interface eth0: Error: No suitable device found: no device found for connection 'System eth0'."

    在VMware的虚拟机中克隆CentOS,在重启网卡的时候报错: Bringing up interface eth0:  Error: No suitable device found: no de ...

  9. SSIS 部署到SQL Job

    微软 BI 系列随笔 - SSIS 基础 - 部署SQL Job 简介 在之前博客中,讲述了如何实现SSIS的项目部署以及利用SSIS的参数与环境加速部署,参见 微软 BI 系列随笔 - SSIS 基 ...

  10. 【MySQL】探究之常用SQL

    一些SQL命令(不断更新,我总记不住,哭) List 建库建表 表的重命名(不区分大小写) 列的重命名 编码 修改结构 添加删除索引 大批量删除 binlog相关 select相关 数据库备份和恢复 ...