private void draggableBorder_MyMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DraggableBorder draggableBorder = sender as DraggableBorder; Canvas parentCanvas = draggableBorder.Parent as Canvas; //置顶 Canvas.SetZIndex(draggableBorder, 1); //捕获鼠标 draggableBorder.CaptureMouse(); this.startPoint = Mouse.GetPosition(draggableBorder); if (this.canvasBorderList.Contains(draggableBorder)) { this.canvasBorderList.Remove(draggableBorder); } if (parentCanvas.Children.Contains(this.hithLightRectangle)) { parentCanvas.Children.Remove(this.hithLightRectangle); } if (this.hithLightRectangle == null) { this.hithLightRectangle = Utils.ControlLayoutHelper.DrawRoundRect(parentCanvas, draggableBorder.RectangleGeometry.Rect); } if (!parentCanvas.Children.Contains(this.hithLightRectangle)) { parentCanvas.Children.Add(this.hithLightRectangle); } this.PutHighLightRectangle(parentCanvas, this.hithLightRectangle, draggableBorder.RectangleGeometry.Rect); }
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); }
private void InitDraggableBorders(Canvas previewCanvas) { this.canvasBorderList = Utils.ControlSearchHelper.GetChildControls <DraggableBorder>(previewCanvas, ""); //初始化预览面板 for (int i = 0; i < this.canvasBorderList.Count; i++) { DraggableBorder draggableBorder = this.canvasBorderList[i]; InitDraggableBorder(draggableBorder, i); } }
private void ClearDraggableBorders(Canvas parentCanvas) { UIElement[] elements = new UIElement[parentCanvas.Children.Count]; parentCanvas.Children.CopyTo(elements, 0); foreach (var element in elements) { DraggableBorder border = element as DraggableBorder; if (border != null && parentCanvas.Children.Contains(border)) { parentCanvas.Children.Remove(border); } } }
private void UICommon_DragControlHelper_DragCompleted(object Sender, DragChangedEventArgs e) { //System.Console.WriteLine((Sender as DragControlHelper).TargetElement); DraggableBorder targetObj = (Sender as DragControlHelper).TargetElement as DraggableBorder; if (targetObj != null && targetObj.Width != targetObj.RectangleGeometry.Rect.Width || targetObj.Height != targetObj.RectangleGeometry.Rect.Height) { //draggableBorder支持拖动改变大小,修正RectangleGeometry targetObj.RectangleGeometry = new RectangleGeometry() { Rect = new Rect(Canvas.GetLeft(targetObj), Canvas.GetTop(targetObj), targetObj.Width, targetObj.Height) }; } }
private void Button_Add_Click(object sender, RoutedEventArgs e) { KeyInputDialog dialog = new KeyInputDialog("按键设置", "请按下按键:", -1, ""); dialog.OKButton.Click += (s1, e1) => { if (dialog.KeyCode != -1 && !string.IsNullOrEmpty(dialog.KeyText)) { if (this.boardKeyCodes.ContainsKey(dialog.KeyCode)) { DialogTip warningDialog = new DialogTip("警告", "该按键已经设置。"); warningDialog.ShowDialog(); return; } Size insertBoardSize = new Size(GRID_SIZE * dialog.WidthGridCount + (dialog.WidthGridCount - 1) * GRID_INTERVAL, GRID_SIZE * dialog.HeightGridCount + (dialog.HeightGridCount - 1) * GRID_INTERVAL); Point insertLocation = Utils.ControlLayoutHelper.SearchInsertLocation(this.PreviewCanvas, insertBoardSize, GRID_INTERVAL); if (insertLocation.X == -1 || insertLocation.Y == -1) { DialogTip warningDialog = new DialogTip("警告", "没有合适的位置插入按钮。"); warningDialog.ShowDialog(); return; } //绘制Border Rect borderRect = new Rect(insertLocation.X, insertLocation.Y, insertBoardSize.Width, insertBoardSize.Height); ButtonDefinition bDef = new ButtonDefinition(dialog.KeyCode, dialog.KeyText, borderRect); DraggableBorder draggableBorder = Utils.ControlLayoutHelper.DrawBorderOnCanvas(this, this.PreviewCanvas, bDef); this.InitDraggableBorder(draggableBorder, this.DraggableBorderIndex++); this.canvasBorderList.Add(draggableBorder); this.boardKeyCodes.Add(dialog.KeyCode, dialog.KeyText); } dialog.Close(); }; dialog.ShowDialog(); }
public void Get_Region_For_Single_Click(Point p) { TouchCanvas tc = new TouchCanvas(); DraggableBorder db1 = new DraggableBorder(); DraggableBorder db2 = new DraggableBorder(); DraggableBorder db3 = new DraggableBorder(); InteractiveBorder ib = new InteractiveBorder(); tc.Background = new SolidColorBrush(Colors.WhiteSmoke); db1.Background = new SolidColorBrush(Colors.SlateBlue); db2.Background = new SolidColorBrush(Colors.SlateGray); db3.Background = new SolidColorBrush(Colors.Tomato); ib.Background = new SolidColorBrush(Colors.Red); tc.Width = 300; tc.Height = 200; db1.Width = 50; db1.Height = 50; db2.Width = 50; db2.Height = 50; db3.Width = 50; db3.Height = 50; ib.Width = 10; ib.Height = 10; tc.Children.Add(db1); tc.Children.Add(db2); tc.Children.Add(db3); tc.Children.Add(ib); Canvas.SetLeft(db1, 25); Canvas.SetTop(db1, 100); Canvas.SetLeft(db2, 100); Canvas.SetTop(db2, 100); Canvas.SetLeft(db3, 175); Canvas.SetTop(db3, 100); Canvas.SetTop(ib, 0); Canvas.SetRight(ib, 0); PrimaryCanvas.Children.Add(tc); Canvas.SetLeft(tc, p.X); Canvas.SetTop(tc, p.Y); TouchInputManager.AddTouchContactDownHandler(ib, new TouchContactEventHandler(Kill_Building_Popup)); }
private void PutControl(DraggableBorder draggableBorder) { double left = Canvas.GetLeft(draggableBorder); double top = Canvas.GetTop(draggableBorder); Storyboard storyboard = new Storyboard(); DoubleAnimation animation = CreateAnimation(left, draggableBorder.RectangleGeometry.Rect.Left, draggableBorder, "(Canvas.Left)"); storyboard.Children.Add(animation); animation = CreateAnimation(top, draggableBorder.RectangleGeometry.Rect.Top, draggableBorder, "(Canvas.Top)"); storyboard.Children.Add(animation); storyboard.Completed += (sender1, e1) => { storyboard.Stop(); storyboard = null; Canvas.SetLeft(draggableBorder, draggableBorder.RectangleGeometry.Rect.Left); Canvas.SetTop(draggableBorder, draggableBorder.RectangleGeometry.Rect.Top); //取消置顶 Canvas.SetZIndex(draggableBorder, 0); }; storyboard.Begin(); }
private void draggableBorder_MyMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { DraggableBorder draggableBorder = sender as DraggableBorder; Canvas parentCanvas = draggableBorder.Parent as Canvas; //释放鼠标 draggableBorder.ReleaseMouseCapture(); //取消置顶 //Canvas.SetZIndex(draggableBorder, 0); this.PutControl(draggableBorder); if (!this.canvasBorderList.Contains(draggableBorder)) { this.canvasBorderList.Add(draggableBorder); } if (parentCanvas.Children.Contains(this.hithLightRectangle)) { parentCanvas.Children.Remove(this.hithLightRectangle); } this.hithLightRectangle = null; }
private void draggableBorder_MyDragEvent(object sender, MouseEventArgs e) { DraggableBorder draggableBorder = sender as DraggableBorder; Canvas parentCanvas = draggableBorder.Parent as Canvas; double borderLeft = draggableBorder.CurrentLocation.X - this.startPoint.X; double borderTop = draggableBorder.CurrentLocation.Y - this.startPoint.Y; Canvas.SetLeft(draggableBorder, borderLeft); Canvas.SetTop(draggableBorder, borderTop); //计算当前拖动位置应该归属的块 double gridLeft = Canvas.GetLeft(this.hithLightRectangle); double gridTop = Canvas.GetTop(this.hithLightRectangle); if ((borderLeft - gridLeft) >= (GRID_SIZE + GRID_INTERVAL)) { gridLeft += (GRID_SIZE + GRID_INTERVAL); } else if ((borderLeft - gridLeft) <= -(GRID_SIZE + GRID_INTERVAL)) { gridLeft -= (GRID_SIZE + GRID_INTERVAL); } if ((borderTop - gridTop) >= (GRID_SIZE + GRID_INTERVAL)) { gridTop += (GRID_SIZE * 2 + GRID_INTERVAL * 2); } else if ((borderTop - gridTop) <= -(GRID_SIZE + GRID_INTERVAL)) { gridTop -= (GRID_SIZE * 2 + GRID_INTERVAL * 2); } Rect gridRect = new Rect(gridLeft, gridTop, draggableBorder.Width, draggableBorder.Height); this.PutHighLightRectangle(parentCanvas, this.hithLightRectangle, gridRect); //判断是否允许放置在当前位置,不合法则不显示 bool isAllowHighLight = this.canvasBorderList.All(x => !this.IsBorderOverlap(gridRect, x.RectangleGeometry.Rect)) && gridLeft >= 0 && gridLeft + draggableBorder.Width <= this.PreviewCanvas.Width && gridTop >= 0 && gridTop + draggableBorder.Height <= this.PreviewCanvas.Height; if (isAllowHighLight) { if (!parentCanvas.Children.Contains(this.hithLightRectangle)) { parentCanvas.Children.Add(this.hithLightRectangle); } draggableBorder.RectangleGeometry = new RectangleGeometry() { Rect = gridRect }; } else { if (parentCanvas.Children.Contains(this.hithLightRectangle)) { parentCanvas.Children.Remove(this.hithLightRectangle); } } //计算Border中心点位置 Point point = new Point(borderLeft + draggableBorder.Width / 2, borderTop + draggableBorder.Height / 2); DraggableBorder coverBorder = null; if (draggableBorder.ParentControl.Equals(this.PreviewCanvas)) { coverBorder = canvasBorderList.Find(p => p.RectangleGeometry.FillContains(point)); } if (coverBorder != null && !coverBorder.Equals(draggableBorder)) { if (draggableBorder.RenderSize == coverBorder.RenderSize) { //元素大小相同的情况下可以交换位置,否则不可交换 RectangleGeometry tempRect = coverBorder.RectangleGeometry; coverBorder.RectangleGeometry = draggableBorder.RectangleGeometry; draggableBorder.RectangleGeometry = tempRect; this.PutControl(coverBorder); } } }