/// <summary> 按下中键时可拖动滚动条 /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MindMap_Panel_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Middle || e.Button == MouseButtons.Right) { MoveValue = e.Location; } else if (e.Button == MouseButtons.Left) { MindMapNodeContent.MindMapNodeContentBase ContentControl = ((Control)sender).GetNodeContent(); if (ContentControl == null) { IsMindMapNode = false; #region 空白处按住左键,即将进行矩形选择 if (Control.ModifierKeys == Keys.Control)//按住ctrl就记录已选中节点 { SelectedNodeList = GetSelectedNode(); } else { SelectedNodeList.Clear(); //虽然不按住Control在调用本方法之前所有节点就已经取消选中了,但为了性能还是不要调用GetSelectedNode方法了吧 } ShowOrHideLine(true); //显示矩形的线条 #endregion 空白处按住左键,即将进行矩形选择 } else { // 在节点中按住左键,即将进行拖拽操作 IsMindMapNode = true; } MoveValue = Scroll_panel.PointToClient(Control.MousePosition); } IsDrag = false; }
/// <summary>按住鼠标中间可拖动滚动条 /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MindMap_Panel_MouseMove(object sender, MouseEventArgs e) { #region 计算矩形的尺寸 Point PointTemp1 = Scroll_panel.PointToClient(Control.MousePosition); Point PanelLocation = new Point(0, 0); Size PanelSize = new Size(); if (MoveValue.X < PointTemp1.X) { PanelLocation.X = MoveValue.X; PanelSize.Width = PointTemp1.X - MoveValue.X; } else { PanelLocation.X = PointTemp1.X; PanelSize.Width = MoveValue.X - PointTemp1.X; } if (MoveValue.Y < PointTemp1.Y) { PanelLocation.Y = MoveValue.Y; PanelSize.Height = PointTemp1.Y - MoveValue.Y; } else { PanelLocation.Y = PointTemp1.Y; PanelSize.Height = MoveValue.Y - PointTemp1.Y; } #endregion 计算矩形的尺寸 Rectangle CurrentRectangle = new Rectangle(PanelLocation, PanelSize); //计算出当前在控件中左键画出的矩形 if (CurrentRectangle.Width < 3 || CurrentRectangle.Height < 3) { return; //必须拖动一定距离才触发,否则单击也会触发 } IsDrag = true; if (Control.MouseButtons == MouseButtons.Right) { #region 右键拖动思维导图 Point PointTemp = new Point(); PointTemp.X = MoveValue.X - e.Location.X; PointTemp.Y = MoveValue.Y - e.Location.Y; Point ResultPoint = new Point(Main_Panel.HorizontalScroll.Value + PointTemp.X, Main_Panel.VerticalScroll.Value + PointTemp.Y); Main_Panel.AutoScrollPosition = ResultPoint; RecordScrollPosition(); #endregion 右键拖动思维导图 } else if (Control.MouseButtons == MouseButtons.Left) { if (IsMindMapNode) //是否在节点中进行左键拖拽 { //是:拖拽节点 MindMapNodeContent.MindMapNodeContentBase ContentControl = ((Control)sender).GetNodeContent(); if (ContentControl == null) { return; } ContentControl.DoDragDrop(ContentControl, DragDropEffects.Move); } else { //不是:就拖拽出矩形框框 #region 画出矩形 Selected_Top_panel.Location = PanelLocation; Selected_Top_panel.Width = PanelSize.Width; Selected_Left_panel.Location = PanelLocation; Selected_Left_panel.Height = PanelSize.Height; Selected_Right_panel.Location = new Point(PanelLocation.X + PanelSize.Width, PanelLocation.Y); Selected_Right_panel.Height = PanelSize.Height; Selected_Bottom_panel.Location = new Point(PanelLocation.X, PanelLocation.Y + PanelSize.Height); Selected_Bottom_panel.Width = PanelSize.Width; #endregion 画出矩形 #region 中框住的节点 //获取所有节点实例 List <MindMapNodeContainer> MindMapNodeContainerTemp = mindMapNode.GetChidrenNode(true); MindMapNodeContainerTemp.Add(mindMapNode); foreach (MindMapNodeContainer MindMapNodeContaineritem in MindMapNodeContainerTemp)//遍历所有节点实例 { #region 获取当前节点的坐标和尺寸信息[矩形信息] Point ContentPoint = MindMapNodeContaineritem.NodeContent.PointToScreen(new Point()); //获取节点屏幕坐标 ContentPoint = Scroll_panel.PointToClient(ContentPoint); //获取节点在控件中的位置 Rectangle RectangleTemp = new Rectangle(ContentPoint, MindMapNodeContaineritem.NodeContent.Size); //获取节点的位置和尺寸信息 #endregion 获取当前节点的坐标和尺寸信息[矩形信息] #region 被框住就选中或反选节点 if (CurrentRectangle.IntersectsWith(RectangleTemp)) //左键拖出的矩形是否与当前实例有交集 { if (SelectedNodeList.Contains(MindMapNodeContaineritem)) //如果已经选中了就取消选中 { MindMapNodeContaineritem.NodeContent.Selected = false; } else { MindMapNodeContaineritem.NodeContent.Selected = true; } } else { if (SelectedNodeList.Contains(MindMapNodeContaineritem)) { continue;//在拖动之前就已经选中了就不管 } MindMapNodeContaineritem.NodeContent.Selected = false; } #endregion 被框住就选中或反选节点 } #endregion 中框住的节点 } } }