[2134] | 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 |
|
---|
| 9 | namespace WeifenLuo.WinFormsUI.Docking
|
---|
| 10 | {
|
---|
| 11 | partial class DockPanel
|
---|
| 12 | {
|
---|
| 13 | /// <summary>
|
---|
| 14 | /// DragHandlerBase is the base class for drag handlers. The derived class should:
|
---|
| 15 | /// 1. Define its public method BeginDrag. From within this public BeginDrag method,
|
---|
| 16 | /// DragHandlerBase.BeginDrag should be called to initialize the mouse capture
|
---|
| 17 | /// and message filtering.
|
---|
| 18 | /// 2. Override the OnDragging and OnEndDrag methods.
|
---|
| 19 | /// </summary>
|
---|
| 20 | private abstract class DragHandlerBase : NativeWindow, IMessageFilter
|
---|
| 21 | {
|
---|
| 22 | protected DragHandlerBase()
|
---|
| 23 | {
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | protected abstract Control DragControl
|
---|
| 27 | {
|
---|
| 28 | get;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | private Point m_startMousePosition = Point.Empty;
|
---|
| 32 | protected Point StartMousePosition
|
---|
| 33 | {
|
---|
| 34 | get { return m_startMousePosition; }
|
---|
| 35 | private set { m_startMousePosition = value; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | protected bool BeginDrag()
|
---|
| 39 | {
|
---|
| 40 | // Avoid re-entrance;
|
---|
| 41 | lock (this)
|
---|
| 42 | {
|
---|
| 43 | if (DragControl == null)
|
---|
| 44 | return false;
|
---|
| 45 |
|
---|
| 46 | StartMousePosition = Control.MousePosition;
|
---|
| 47 |
|
---|
| 48 | if (!NativeMethods.DragDetect(DragControl.Handle, StartMousePosition))
|
---|
| 49 | return false;
|
---|
| 50 |
|
---|
| 51 | DragControl.FindForm().Capture = true;
|
---|
| 52 | AssignHandle(DragControl.FindForm().Handle);
|
---|
| 53 | Application.AddMessageFilter(this);
|
---|
| 54 | return true;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | protected abstract void OnDragging();
|
---|
| 59 |
|
---|
| 60 | protected abstract void OnEndDrag(bool abort);
|
---|
| 61 |
|
---|
| 62 | private void EndDrag(bool abort)
|
---|
| 63 | {
|
---|
| 64 | ReleaseHandle();
|
---|
| 65 | Application.RemoveMessageFilter(this);
|
---|
| 66 | DragControl.FindForm().Capture = false;
|
---|
| 67 |
|
---|
| 68 | OnEndDrag(abort);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | bool IMessageFilter.PreFilterMessage(ref Message m)
|
---|
| 72 | {
|
---|
| 73 | if (m.Msg == (int)Win32.Msgs.WM_MOUSEMOVE)
|
---|
| 74 | OnDragging();
|
---|
| 75 | else if (m.Msg == (int)Win32.Msgs.WM_LBUTTONUP)
|
---|
| 76 | EndDrag(false);
|
---|
| 77 | else if (m.Msg == (int)Win32.Msgs.WM_CAPTURECHANGED)
|
---|
| 78 | EndDrag(true);
|
---|
| 79 | else if (m.Msg == (int)Win32.Msgs.WM_KEYDOWN && (int)m.WParam == (int)Keys.Escape)
|
---|
| 80 | EndDrag(true);
|
---|
| 81 |
|
---|
| 82 | return OnPreFilterMessage(ref m);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | protected virtual bool OnPreFilterMessage(ref Message m)
|
---|
| 86 | {
|
---|
| 87 | return false;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | protected sealed override void WndProc(ref Message m)
|
---|
| 91 | {
|
---|
| 92 | if (m.Msg == (int)Win32.Msgs.WM_CANCELMODE || m.Msg == (int)Win32.Msgs.WM_CAPTURECHANGED)
|
---|
| 93 | EndDrag(true);
|
---|
| 94 |
|
---|
| 95 | base.WndProc(ref m);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private abstract class DragHandler : DragHandlerBase
|
---|
| 100 | {
|
---|
| 101 | private DockPanel m_dockPanel;
|
---|
| 102 |
|
---|
| 103 | protected DragHandler(DockPanel dockPanel)
|
---|
| 104 | {
|
---|
| 105 | m_dockPanel = dockPanel;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public DockPanel DockPanel
|
---|
| 109 | {
|
---|
| 110 | get { return m_dockPanel; }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private IDragSource m_dragSource;
|
---|
| 114 | protected IDragSource DragSource
|
---|
| 115 | {
|
---|
| 116 | get { return m_dragSource; }
|
---|
| 117 | set { m_dragSource = value; }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | protected sealed override Control DragControl
|
---|
| 121 | {
|
---|
| 122 | get { return DragSource == null ? null : DragSource.DragControl; }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | protected sealed override bool OnPreFilterMessage(ref Message m)
|
---|
| 126 | {
|
---|
| 127 | if ((m.Msg == (int)Win32.Msgs.WM_KEYDOWN || m.Msg == (int)Win32.Msgs.WM_KEYUP) &&
|
---|
| 128 | ((int)m.WParam == (int)Keys.ControlKey || (int)m.WParam == (int)Keys.ShiftKey))
|
---|
| 129 | OnDragging();
|
---|
| 130 |
|
---|
| 131 | return base.OnPreFilterMessage(ref m);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|