Exemplo n.º 1
0
 /// <summary>
 /// 判断界面上的一个点是否在矩形内
 /// </summary>
 /// <param name="point"></param>
 /// <param name="uc"></param>
 /// <returns></returns>
 private bool IsPointInRect(Point point, UCRectBorder uc)
 {
     if (point.X >= uc.StartPoint.X &&
         point.X <= uc.EndPoint.X &&
         point.Y >= uc.StartPoint.Y &&
         point.Y <= uc.EndPoint.Y
         )
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 2
0
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var sPoint = e.GetPosition(canvasPanel);

            if (_isAdd)
            { //添加矩形
                var uc = new UCRectBorder();
                //默认长宽为20的矩形,否则用户按下鼠标左键后看不到矩形
                uc.StartPoint        = new Point(sPoint.X - 20, sPoint.Y - 20);
                uc.EndPoint          = new Point(sPoint.X, sPoint.Y);
                uc.MouseDoubleClick += (o, s) =>
                {
                    //若已选择则取消选择
                    if (SelectedRect == uc && SelectedRect.IsSelected == true)
                    {
                        SelectedRect.IsSelected = false;
                    }
                    else
                    {
                        SelectedRect            = uc;
                        SelectedRect.IsSelected = true;
                    }
                };
                canvasPanel.Children.Add(uc);
                SelectedRect = uc;
                //刚添加时默认移动矩形右下角
                _operateStatus = OperateStatus.RightBottom;
                _tmpRect       = new Rect(SelectedRect.StartPoint, SelectedRect.EndPoint);
            }
            else if (SelectedRect != null && IsPointInRect(sPoint, SelectedRect))
            {//编辑矩形
                _downPoint = sPoint;

                if (SelectedRect.Cursor != Cursors.Arrow)
                {
                    _tmpRect = new Rect(SelectedRect.StartPoint, SelectedRect.EndPoint);

                    var point = e.GetPosition(SelectedRect);
                    if (SelectedRect.Cursor == Cursors.SizeAll)
                    {//移动
                        _operateStatus = OperateStatus.Move;
                    }
                    else if (SelectedRect.Cursor == Cursors.SizeNS)
                    {//上下
                        if (point.Y < SelectedRect.ActualHeight / 2)
                        {
                            _operateStatus = OperateStatus.Top;
                        }
                        else
                        {
                            _operateStatus = OperateStatus.Bottom;
                        }
                    }
                    else if (SelectedRect.Cursor == Cursors.SizeWE)
                    {//左右
                        if (point.X < SelectedRect.ActualWidth / 2)
                        {
                            _operateStatus = OperateStatus.Left;
                        }
                        else
                        {
                            _operateStatus = OperateStatus.Right;
                        }
                    }
                    else if (SelectedRect.Cursor == Cursors.SizeNWSE)
                    {//左上 右下
                        if (point.X < SelectedRect.ActualWidth / 2)
                        {
                            _operateStatus = OperateStatus.LeftTop;
                        }
                        else
                        {
                            _operateStatus = OperateStatus.RightBottom;
                        }
                    }
                    else if (SelectedRect.Cursor == Cursors.SizeNESW)
                    {//左下 右上
                        if (point.X < SelectedRect.ActualWidth / 2)
                        {
                            _operateStatus = OperateStatus.LeftBottom;
                        }
                        else
                        {
                            _operateStatus = OperateStatus.RightTop;
                        }
                    }
                }
                else
                {
                    _operateStatus = OperateStatus.None;
                }
            }
        }