如何实现能像windows 窗体一样改变大小的控件 Silverlight

众所周知,我们可以将鼠标放在windows窗体的边框上,按住鼠标左键改变窗体大小。那么,在silverlight上如何实现呢?

1. 需要将改控件放置在canvas上。

2. 判断鼠标位置,然后将Arrow鼠标形状改变为相应的Resize形状(本实例默认当鼠标处于边框内5px时,可resize):

                //the left top corner
                if (location.Y <5 && location.X <5)
                {
                    this.Cursor = Cursors.SizeNWSE;
                    currentEdgeCorner = EdgeCorner.LeftTopCorner;
                }
                //the right top corner
                else if (location.Y <5 && this.Width - location.X <5)
                {
                    this.Cursor = Cursors.SizeNESW;
                    currentEdgeCorner = EdgeCorner.RightTopCorner;
                }
                //the right bottom corner
                else if (this.Width - location.X <5 && this.Height - location.Y <5)
                {
                    this.Cursor = Cursors.SizeNWSE;
                    currentEdgeCorner = EdgeCorner.RightBottomCorner;
                }
                // the left bottom corner
                else if (location.X <5 && this.Height - location.Y <5)
                {
                    this.Cursor = Cursors.SizeNESW;
                    currentEdgeCorner = EdgeCorner.LeftBottomCorner;
                }
                //the left edge
                else if (location.X <5)
                {
                    this.Cursor = Cursors.SizeWE;
                    currentEdgeCorner = EdgeCorner.LeftEdge;
                }
                //the right edge
                else if (this.Width - location.X <5)
                {
                    this.Cursor = Cursors.SizeWE;
                    currentEdgeCorner = EdgeCorner.RightEdge;
                }
                //the bottom edge
                else if (this.Height - location.Y <5)
                {
                    this.Cursor = Cursors.SizeNS;
                    currentEdgeCorner = EdgeCorner.BottomEdge;
                }
                //the top edge
                else if (location.Y <5)
                {
                    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 < 5)
                            return;
                    }
                    else
                    {
                        newWidth = orgSize.X - (_current.X - _rootPosition.X);
                        if (newWidth < 5)
                            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 < 5)
                            return;
                    }
                    else
                    {
                        newHeight = orgSize.Y - (_current.Y - _rootPosition.Y);
                        if (newHeight < 5)
                            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 < 5 || newWidth < 5)
                            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 < 5 || newWidth < 5)
                            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 < 5 || newWidth < 5)
                            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 < 5 || newWidth < 5)
                            return;
                    }
                }
                this.Height = newHeight;
                this.Width = newWidth;
            

当要设置位置信息Canvas.Top, Canvas.Left时,必须特别用此控件的父类或者其他不动点的相对值,即

Point _current = e.GetPosition(this.Parent as UIElement);

  是正确的,但

Point _current = e.GetPosition(this);

  是不正确的。

原文地址:https://www.cnblogs.com/crazyghostvon/p/Resize_SilverlightControl.html