众所周知,我们可以将鼠标放在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. UVA-11297 Census(线段树套线段树)

    题目大意:二维空间点修改,询问矩形区域最值. 题目分析:还是比较简单的. 代码如下: # include<iostream> # include<cstdio> # inclu ...

  2. 电脑结构和CPU、内存、硬盘三者之间的关系

    前面提到了,电脑之父——冯·诺伊曼提出了计算机的五大部件:输入设备.输出设备.存储器.运算器和控制器. 我们看一下现在我们电脑的: 键盘鼠标.显示器.机箱.音响等等. 这里显示器为比较老的CRT显示器 ...

  3. sqlserver2008 ,只能选C盘目录,不能选其它盘目录

    数据库sql2008安装后,无论备份或还原,只能看到C盘,手工输入路径,错误提示如下:尝试打开或创建物理文件 'D:\数据库\db.mdf' 时,CREATE FILE 遇到操作系统错误 5(拒绝访问 ...

  4. 一个服务器上面配置多个IP ,实现指定IP的域名请求

    //配置多个IP命名using System.Net; //********************************************************************** ...

  5. Struts2实现简单的在线人数统计

    用Strust2框架的知识简单实现一个统计在线人数的问题. 1 搭建开发环境:(配置文件,jar包等问题) 2 index.jsp <%@ page language="java&qu ...

  6. Parquet文件结构笔记

    Parquet是面向分析型业务的列式存储格式,由Twitter和Cloudera合作开发,2015年5月从Apache的孵化器里毕业成为Apache顶级项目,那么这里就总结下Parquet数据结构到底 ...

  7. Android中RelativeLayout各个属性的含义

    android:layout_above="@id/xxx"  --将控件置于给定ID控件之上android:layout_below="@id/xxx"  - ...

  8. NGUI之渲染DrawCall的合并

    在Unity中,每次引擎准备数据并通知GPU的过程称为一次Draw Call.Draw Call值越低,会得到更好的渲染性能. (NGUI 查看DrawCall工具(NGUI-OPEN-Draw Ca ...

  9. Esfog_UnityShader教程_镜面反射SpecularReflection

    系列教程第四篇,本来打算昨天写的,有些小偷懒就今天写了,这一期我们来讨论一下关于镜面反射的基本原理和具体代码.这一篇是承接着上一篇<Esfog_UnityShader教程_漫反射DiffuseR ...

  10. 62. Divide Two Integers

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...