DockPanel.SplitterDragHandler.cs
上传用户:szlfmled
上传日期:2020-11-22
资源大小:978k
文件大小:5k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.ComponentModel;
  8. namespace WeifenLuo.WinFormsUI.Docking
  9. {
  10.     partial class DockPanel
  11.     {
  12.         private sealed class SplitterDragHandler : DragHandler
  13.         {
  14.             private class SplitterOutline
  15.             {
  16.                 public SplitterOutline()
  17.                 {
  18.                     m_dragForm = new DragForm();
  19.                     SetDragForm(Rectangle.Empty);
  20.                     DragForm.BackColor = Color.Black;
  21.                     DragForm.Opacity = 0.7;
  22.                     DragForm.Show(false);
  23.                 }
  24.                 DragForm m_dragForm;
  25.                 private DragForm DragForm
  26.                 {
  27.                     get { return m_dragForm; }
  28.                 }
  29.                 public void Show(Rectangle rect)
  30.                 {
  31.                     SetDragForm(rect);
  32.                 }
  33.                 public void Close()
  34.                 {
  35.                     DragForm.Close();
  36.                 }
  37.                 private void SetDragForm(Rectangle rect)
  38.                 {
  39.                     DragForm.Bounds = rect;
  40.                     if (rect == Rectangle.Empty)
  41.                         DragForm.Region = new Region(Rectangle.Empty);
  42.                     else if (DragForm.Region != null)
  43.                         DragForm.Region = null;
  44.                 }
  45.             }
  46.             public SplitterDragHandler(DockPanel dockPanel)
  47.                 : base(dockPanel)
  48.             {
  49.             }
  50.             public new ISplitterDragSource DragSource
  51.             {
  52.                 get { return base.DragSource as ISplitterDragSource; }
  53.                 private set { base.DragSource = value; }
  54.             }
  55.             private SplitterOutline m_outline;
  56.             private SplitterOutline Outline
  57.             {
  58.                 get { return m_outline; }
  59.                 set { m_outline = value; }
  60.             }
  61.             private Rectangle m_rectSplitter;
  62.             private Rectangle RectSplitter
  63.             {
  64.                 get { return m_rectSplitter; }
  65.                 set { m_rectSplitter = value; }
  66.             }
  67.             public void BeginDrag(ISplitterDragSource dragSource, Rectangle rectSplitter)
  68.             {
  69.                 DragSource = dragSource;
  70.                 RectSplitter = rectSplitter;
  71.                 if (!BeginDrag())
  72.                 {
  73.                     DragSource = null;
  74.                     return;
  75.                 }
  76.                 Outline = new SplitterOutline();
  77.                 Outline.Show(rectSplitter);
  78.                 DragSource.BeginDrag(rectSplitter);
  79.             }
  80.             protected override void OnDragging()
  81.             {
  82.                 Outline.Show(GetSplitterOutlineBounds(Control.MousePosition));
  83.             }
  84.             protected override void OnEndDrag(bool abort)
  85.             {
  86.                 DockPanel.SuspendLayout(true);
  87.                 Outline.Close();
  88.                 if (!abort)
  89.                     DragSource.MoveSplitter(GetMovingOffset(Control.MousePosition));
  90.                 DragSource.EndDrag();
  91.                 DockPanel.ResumeLayout(true, true);
  92.             }
  93.             private int GetMovingOffset(Point ptMouse)
  94.             {
  95.                 Rectangle rect = GetSplitterOutlineBounds(ptMouse);
  96.                 if (DragSource.IsVertical)
  97.                     return rect.X - RectSplitter.X;
  98.                 else
  99.                     return rect.Y - RectSplitter.Y;
  100.             }
  101.             private Rectangle GetSplitterOutlineBounds(Point ptMouse)
  102.             {
  103.                 Rectangle rectLimit = DragSource.DragLimitBounds;
  104.                 Rectangle rect = RectSplitter;
  105.                 if (rectLimit.Width <= 0 || rectLimit.Height <= 0)
  106.                     return rect;
  107.                 if (DragSource.IsVertical)
  108.                 {
  109.                     rect.X += ptMouse.X - StartMousePosition.X;
  110.                     rect.Height = rectLimit.Height;
  111.                 }
  112.                 else
  113.                 {
  114.                     rect.Y += ptMouse.Y - StartMousePosition.Y;
  115.                     rect.Width = rectLimit.Width;
  116.                 }
  117.                 if (rect.Left < rectLimit.Left)
  118.                     rect.X = rectLimit.X;
  119.                 if (rect.Top < rectLimit.Top)
  120.                     rect.Y = rectLimit.Y;
  121.                 if (rect.Right > rectLimit.Right)
  122.                     rect.X -= rect.Right - rectLimit.Right;
  123.                 if (rect.Bottom > rectLimit.Bottom)
  124.                     rect.Y -= rect.Bottom - rectLimit.Bottom;
  125.                 return rect;
  126.             }
  127.         }
  128.         private SplitterDragHandler m_splitterDragHandler = null;
  129.         private SplitterDragHandler GetSplitterDragHandler()
  130.         {
  131.             if (m_splitterDragHandler == null)
  132.                 m_splitterDragHandler = new SplitterDragHandler(this);
  133.             return m_splitterDragHandler;
  134.         }
  135.         internal void BeginDrag(ISplitterDragSource dragSource, Rectangle rectSplitter)
  136.         {
  137.             GetSplitterDragHandler().BeginDrag(dragSource, rectSplitter);
  138.         }
  139.     }
  140. }