MatrixTransform 控件拖动边界检测

原项目演示地址 http://www.youpvp.com/misc/adorners.html
但那个项目没有加上边界检测,要加上边界检测,只需修改方法 adorned_MouseMove

void adorned_MouseMove(object sender, MouseEventArgs e)

{

    if (isDragging)

    {

        Point pt = e.GetPosition(null);//返回鼠标在整个 Silverlight 插件内容区域内的坐标位置。

        Point ee = e.GetPosition(this);//返回鼠标在当前控件内容区域内的坐标位置。

        Matrix mx = (current.RenderTransform as MatrixTransform).Matrix;

        double Height = (double)this.Parent.GetValue(ActualHeightProperty);//获取 Silverlight 插件的呈现区域的高度

        double Width = (double)this.Parent.GetValue(ActualWidthProperty);//获取 Silverlight 插件的呈现区域的宽度

        double MinX = Math.Max(ee.X, pt.X);

        double MinY = Math.Max(ee.Y, pt.Y);

        double MaxX = current.ActualWidth - ee.X + pt.X;//当前控件右边的位置

        double MaxY = current.ActualHeight - ee.Y + pt.Y;//当前控件下边的位置



        if (MaxX < Width)

        {

            mx.OffsetX += MinX - dragAnchor.X;

        }

        else

        {

            mx.OffsetX += -1;

        }

        if (MaxY < Height)

        {

            mx.OffsetY += MinY - dragAnchor.Y;

        }

        else

        {

            mx.OffsetY += -1;

        }



        //mx.OffsetX += pt.X - dragAnchor.X;

        //mx.OffsetY += pt.Y - dragAnchor.Y;

        current.RenderTransform = new MatrixTransform() { Matrix = mx };

        dragAnchor = pt;

    }

}

你可能感兴趣的:(transform)