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 (!Win32Helper.IsRunningOnMono)
|
---|
49 | if (!NativeMethods.DragDetect(DragControl.Handle, StartMousePosition))
|
---|
50 | return false;
|
---|
51 |
|
---|
52 | DragControl.FindForm().Capture = true;
|
---|
53 | AssignHandle(DragControl.FindForm().Handle);
|
---|
54 | Application.AddMessageFilter(this);
|
---|
55 | return true;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected abstract void OnDragging();
|
---|
60 |
|
---|
61 | protected abstract void OnEndDrag(bool abort);
|
---|
62 |
|
---|
63 | private void EndDrag(bool abort)
|
---|
64 | {
|
---|
65 | ReleaseHandle();
|
---|
66 | Application.RemoveMessageFilter(this);
|
---|
67 | DragControl.FindForm().Capture = false;
|
---|
68 |
|
---|
69 | OnEndDrag(abort);
|
---|
70 | }
|
---|
71 |
|
---|
72 | bool IMessageFilter.PreFilterMessage(ref Message m)
|
---|
73 | {
|
---|
74 | if (m.Msg == (int)Win32.Msgs.WM_MOUSEMOVE)
|
---|
75 | OnDragging();
|
---|
76 | else if (m.Msg == (int)Win32.Msgs.WM_LBUTTONUP)
|
---|
77 | EndDrag(false);
|
---|
78 | else if (m.Msg == (int)Win32.Msgs.WM_CAPTURECHANGED)
|
---|
79 | EndDrag(true);
|
---|
80 | else if (m.Msg == (int)Win32.Msgs.WM_KEYDOWN && (int)m.WParam == (int)Keys.Escape)
|
---|
81 | EndDrag(true);
|
---|
82 |
|
---|
83 | return OnPreFilterMessage(ref m);
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected virtual bool OnPreFilterMessage(ref Message m)
|
---|
87 | {
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected sealed override void WndProc(ref Message m)
|
---|
92 | {
|
---|
93 | if (m.Msg == (int)Win32.Msgs.WM_CANCELMODE || m.Msg == (int)Win32.Msgs.WM_CAPTURECHANGED)
|
---|
94 | EndDrag(true);
|
---|
95 |
|
---|
96 | base.WndProc(ref m);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | private abstract class DragHandler : DragHandlerBase
|
---|
101 | {
|
---|
102 | private DockPanel m_dockPanel;
|
---|
103 |
|
---|
104 | protected DragHandler(DockPanel dockPanel)
|
---|
105 | {
|
---|
106 | m_dockPanel = dockPanel;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public DockPanel DockPanel
|
---|
110 | {
|
---|
111 | get { return m_dockPanel; }
|
---|
112 | }
|
---|
113 |
|
---|
114 | private IDragSource m_dragSource;
|
---|
115 | protected IDragSource DragSource
|
---|
116 | {
|
---|
117 | get { return m_dragSource; }
|
---|
118 | set { m_dragSource = value; }
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected sealed override Control DragControl
|
---|
122 | {
|
---|
123 | get { return DragSource == null ? null : DragSource.DragControl; }
|
---|
124 | }
|
---|
125 |
|
---|
126 | protected sealed override bool OnPreFilterMessage(ref Message m)
|
---|
127 | {
|
---|
128 | if ((m.Msg == (int)Win32.Msgs.WM_KEYDOWN || m.Msg == (int)Win32.Msgs.WM_KEYUP) &&
|
---|
129 | ((int)m.WParam == (int)Keys.ControlKey || (int)m.WParam == (int)Keys.ShiftKey))
|
---|
130 | OnDragging();
|
---|
131 |
|
---|
132 | return base.OnPreFilterMessage(ref m);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|