private void RegisterMessenger()
        {
            Messenger.Default.Register <List <Rectangle> >(this, MessageKey.InitRects, data =>
            {
                //Parallel.For(0, data.Count, ii => this.Dispatcher.InvokeAsync(() =>
                //{
                //    this.cans.Children.Add(data[ii]);
                //    DragControlHelper.SetIsEditable(data[ii], true);
                //    DragControlHelper.SetIsSelectable(data[ii], true);
                //}));

                foreach (var item in data)
                {
                    this.Dispatcher.InvokeAsync(() =>
                    {
                        item.MouseEnter += (sender, e) => (sender as Rectangle).StrokeThickness = 7;
                        item.MouseLeave += (sender, e) => (sender as Rectangle).StrokeThickness = 3;
                        this.cans.Children.Add(item);
                        DragControlHelper.SetIsEditable(item, true);
                        DragControlHelper.SetIsSelectable(item, true);
                    });
                }
            });
            Messenger.Default.Register <bool>(this, MessageKey.IsSelected, data =>
            {
                _isSelected = data;
            });

            Messenger.Default.Register <Rectangle>(this, MessageKey.DeleteSelectedRect, data =>
            {
                this.cans.Children.Remove(data);
            });
        }
        private void cans_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_isSelected)
            {
                return;
            }


            //var x = VisualTreeHelper.HitTest(this.cans, e.GetPosition(this.cans));
            // 右键绘制
            if (e.ChangedButton == MouseButton.Right && e.RightButton == MouseButtonState.Pressed && e.ClickCount == 1)
            {
                //if (!_isDrawStart)
                //{
                //    return;
                //}
                _isDrawing = true;
                _point     = e.GetPosition(this.cans);
                Rectangle  = new Rectangle {
                    Stroke = new SolidColorBrush(Colors.Blue), StrokeThickness = 3, Tag = Guid.NewGuid().ToString()
                };
                DragControlHelper.SetIsEditable(Rectangle, true);
                DragControlHelper.SetIsSelectable(Rectangle, true);

                this.cans.Children.Add(Rectangle);
                Canvas.SetLeft(Rectangle, _point.X);
                Canvas.SetTop(Rectangle, _point.Y);
            }
        }
Пример #3
0
        private void InitDraggableBorder(DraggableBorder draggableBorder, int index)
        {
            draggableBorder.Index                  = index;
            draggableBorder.MyDragEvent           += draggableBorder_MyDragEvent;
            draggableBorder.MyMouseLeftButtonDown += draggableBorder_MyMouseLeftButtonDown;
            draggableBorder.MyMouseLeftButtonUp   += draggableBorder_MyMouseLeftButtonUp;
            draggableBorder.MyMouseDownClick      += draggableBorder_MyMouseDownClick;
            //添加右键菜单
            ContextMenu contextMenu    = new ContextMenu();
            MenuItem    deleteMenuItem = new MenuItem();

            deleteMenuItem.Header = "删除";
            deleteMenuItem.Click += (sender1, e1) =>
            {
                Canvas parentCanvas = draggableBorder.Parent as Canvas;

                if (parentCanvas.Children.Contains(draggableBorder))
                {
                    parentCanvas.Children.Remove(draggableBorder);

                    if (this.canvasBorderList.Contains(draggableBorder))
                    {
                        this.canvasBorderList.Remove(draggableBorder);
                    }

                    if (this.boardKeyCodes.ContainsKey(draggableBorder.KeyCode))
                    {
                        this.boardKeyCodes.Remove(draggableBorder.KeyCode);
                    }
                }
            };
            contextMenu.Items.Add(deleteMenuItem);

            MenuItem resizeMenuItem = new MenuItem();

            resizeMenuItem.Header = "设置大小";
            resizeMenuItem.Click += (sender1, e1) =>
            {
                //设置属性
                DragControlHelper.SetIsEditable(draggableBorder, true);    //允许编辑
                DragControlHelper.SetIsSelectable(draggableBorder, true);  //允许选中
                DragControlHelper.SetIsAutoResetES(draggableBorder, true); //焦点回到Canvas取消编辑状态

                //引发TargetElementChanged事件
                this.PreviewCanvas.Focus();
                this.UICommon_DragControlHelper.TargetElement = draggableBorder;
                draggableBorder.Focus();
            };
            contextMenu.Items.Add(resizeMenuItem);

            draggableBorder.ContextMenu = contextMenu;

            Canvas.SetZIndex(draggableBorder, 0);
        }