代码之家  ›  专栏  ›  技术社区  ›  Aymeric

如何将项目位置限制在画布中?

  •  1
  • Aymeric  · 技术社区  · 15 年前

    好吧,

    我编辑了这篇文章,因为我当时发布的代码与我现在要做的事情没有真正的联系,但问题是一样的。

    当我谈到将对象限制在画布上时,它更像是一个鼠标剪辑,但是当我在许多线程上阅读时,这个功能在SL中并不存在。所以我在所有论坛中搜索了一下,得到了这个 link . 但我没能完全复制出来。以下是用于拖放事件的代码:

    public class RoomImage : ContentControl
    {
        public RoomImage()
        {
            DefaultStyleKey = typeof(RoomImage);
        }
    
        public static readonly DependencyProperty BackgroundImageProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(RoomImage), null);
        public ImageSource BackgroundImage
        {
            get { return (ImageSource)GetValue(BackgroundImageProperty); }
            set { SetValue(BackgroundImageProperty, value); }
        }
    
        //Instance Drag variable
    
        private FrameworkElement _translateZone;
        bool _isDrag;
        Point StartingDragPoint;
    
        public double Top
        {
            get { return (double)GetValue(Canvas.TopProperty); }
            set { SetValue(Canvas.TopProperty, value); }
        }
    
        public double Left
        {
            get { return (double)GetValue(Canvas.LeftProperty); }
            set { SetValue(Canvas.LeftProperty, value); }
        }
    
        //Instance Drag events
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
    
            _translateZone = GetTemplateChild("PART_TranslateZone") as FrameworkElement;
            DefineDragEvents();
        }
    
        private void DefineDragEvents()
        {
            if (_translateZone != null)
            {
                _translateZone.MouseLeftButtonDown += new MouseButtonEventHandler(translateZone_MouseLeftButtonDown);
                _translateZone.MouseLeftButtonUp += new MouseButtonEventHandler(translateZone_MouseLeftButtonUp);
                _translateZone.MouseMove += new MouseEventHandler(translateZone_MouseMove);
            }
        }
    
        private void translateZone_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _isDrag = true;
    
            //start the drag
            FrameworkElement DragBar = (FrameworkElement)sender;
            DragBar.CaptureMouse();
    
            // drag starting point
            StartingDragPoint = e.GetPosition(this);
        }
    
        private void translateZone_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement translateZone = (FrameworkElement)sender;
            translateZone.ReleaseMouseCapture();
    
            _isDrag = false;
        }
    
        private void translateZone_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDrag)
            {
                UIElement ui = (UIElement)this.Parent;
                Point Point = e.GetPosition(ui);
    
                Move(Point.X - StartingDragPoint.X, Point.Y - StartingDragPoint.Y);
            }
        }
    
        public void Move(double left, double top)
        {
            Left = left;
            Top = top;
        }
    
    }
    

    如果有人能给我提供一些代码,或者我应该在哪里执行,那就太棒了!

    谢谢你,伊菲斯门。

    1 回复  |  直到 15 年前
        1
  •  1
  •   iCollect.it Ltd    15 年前

    就孩子们而言,画布没有大小。它只是渲染的相对起点。固定画布大小仅与画布的父级相关。

    如果您的意思是在画布矩形外绘制对象,那么这是画布的正确行为。

    要停止在画布外部绘制对象,需要在画布的“剪辑”属性中设置剪辑矩形。

    更新:

    Here is a very nice example here of how to have a ClipToBounds attached property . 这绝对是我见过的实现边界裁剪的最简单的方法。

    另一个更新:

    所以您只想将子项保留在父画布中。如果您的项目在大小和形状上有所不同,则基本上是侧面碰撞测试,并限制最小/最大左/顶值。您正在删除的形状有多复杂?矩形显然很容易计算(圆也是如此)。