Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.WinFormsUI/2.3.1/WinFormsUI-2.3.1/Docking/DockPanel.DragHandler.cs @ 5304

Last change on this file since 5304 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.5 KB
Line 
1using System.Drawing;
2using System.Windows.Forms;
3
4namespace 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}
Note: See TracBrowser for help on using the repository browser.