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

C#编程

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace WeifenLuo.WinFormsUI.Docking
  7. {
  8.     partial class DockPane
  9.     {
  10.         private class SplitterControl : Control, ISplitterDragSource
  11.         {
  12.             DockPane m_pane;
  13.             public SplitterControl(DockPane pane)
  14.             {
  15.                 SetStyle(ControlStyles.Selectable, false);
  16.                 m_pane = pane;
  17.             }
  18.             public DockPane DockPane
  19.             {
  20.                 get { return m_pane; }
  21.             }
  22.             private DockAlignment m_alignment;
  23.             public DockAlignment Alignment
  24.             {
  25.                 get { return m_alignment; }
  26.                 set
  27.                 {
  28.                     m_alignment = value;
  29.                     if (m_alignment == DockAlignment.Left || m_alignment == DockAlignment.Right)
  30.                         Cursor = Cursors.VSplit;
  31.                     else if (m_alignment == DockAlignment.Top || m_alignment == DockAlignment.Bottom)
  32.                         Cursor = Cursors.HSplit;
  33.                     else
  34.                         Cursor = Cursors.Default;
  35.                     if (DockPane.DockState == DockState.Document)
  36.                         Invalidate();
  37.                 }
  38.             }
  39.             protected override void OnPaint(PaintEventArgs e)
  40.             {
  41.                 base.OnPaint(e);
  42.                 if (DockPane.DockState != DockState.Document)
  43.                     return;
  44.                 Graphics g = e.Graphics;
  45.                 Rectangle rect = ClientRectangle;
  46.                 if (Alignment == DockAlignment.Top || Alignment == DockAlignment.Bottom)
  47.                     g.DrawLine(SystemPens.ControlDark, rect.Left, rect.Bottom - 1, rect.Right, rect.Bottom - 1);
  48.                 else if (Alignment == DockAlignment.Left || Alignment == DockAlignment.Right)
  49.                     g.DrawLine(SystemPens.ControlDarkDark, rect.Right - 1, rect.Top, rect.Right - 1, rect.Bottom);
  50.             }
  51.             protected override void OnMouseDown(MouseEventArgs e)
  52.             {
  53.                 base.OnMouseDown(e);
  54.                 if (e.Button != MouseButtons.Left)
  55.                     return;
  56.                 DockPane.DockPanel.BeginDrag(this, Parent.RectangleToScreen(Bounds));
  57.             }
  58.             #region ISplitterDragSource Members
  59.             void ISplitterDragSource.BeginDrag(Rectangle rectSplitter)
  60.             {
  61.             }
  62.             void ISplitterDragSource.EndDrag()
  63.             {
  64.             }
  65.             bool ISplitterDragSource.IsVertical
  66.             {
  67.                 get
  68.                 {
  69.                     NestedDockingStatus status = DockPane.NestedDockingStatus;
  70.                     return (status.DisplayingAlignment == DockAlignment.Left ||
  71.                         status.DisplayingAlignment == DockAlignment.Right);
  72.                 }
  73.             }
  74.             Rectangle ISplitterDragSource.DragLimitBounds
  75.             {
  76.                 get
  77.                 {
  78.                     NestedDockingStatus status = DockPane.NestedDockingStatus;
  79.                     Rectangle rectLimit = Parent.RectangleToScreen(status.LogicalBounds);
  80.                     if (((ISplitterDragSource)this).IsVertical)
  81.                     {
  82.                         rectLimit.X += MeasurePane.MinSize;
  83.                         rectLimit.Width -= 2 * MeasurePane.MinSize;
  84.                     }
  85.                     else
  86.                     {
  87.                         rectLimit.Y += MeasurePane.MinSize;
  88.                         rectLimit.Height -= 2 * MeasurePane.MinSize;
  89.                     }
  90.                     return rectLimit;
  91.                 }
  92.             }
  93.             void ISplitterDragSource.MoveSplitter(int offset)
  94.             {
  95.                 NestedDockingStatus status = DockPane.NestedDockingStatus;
  96.                 double proportion = status.Proportion;
  97.                 if (status.LogicalBounds.Width <= 0 || status.LogicalBounds.Height <= 0)
  98.                     return;
  99.                 else if (status.DisplayingAlignment == DockAlignment.Left)
  100.                     proportion += ((double)offset) / (double)status.LogicalBounds.Width;
  101.                 else if (status.DisplayingAlignment == DockAlignment.Right)
  102.                     proportion -= ((double)offset) / (double)status.LogicalBounds.Width;
  103.                 else if (status.DisplayingAlignment == DockAlignment.Top)
  104.                     proportion += ((double)offset) / (double)status.LogicalBounds.Height;
  105.                 else
  106.                     proportion -= ((double)offset) / (double)status.LogicalBounds.Height;
  107.                 DockPane.SetNestedDockingProportion(proportion);
  108.             }
  109.             #region IDragSource Members
  110.             Control IDragSource.DragControl
  111.             {
  112.                 get { return this; }
  113.             }
  114.             #endregion
  115.             #endregion
  116.         }
  117.         private SplitterControl m_splitter;
  118.         private SplitterControl Splitter
  119.         {
  120.             get { return m_splitter; }
  121.         }
  122.         internal Rectangle SplitterBounds
  123.         {
  124.             set { Splitter.Bounds = value; }
  125.         }
  126.         internal DockAlignment SplitterAlignment
  127.         {
  128.             set { Splitter.Alignment = value; }
  129.         }
  130.     }
  131. }