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