From 4d515d2a9ea868965dd9f2e7055646fb39ecc4e8 Mon Sep 17 00:00:00 2001 From: Jaex Date: Fri, 30 Sep 2016 21:18:30 +0300 Subject: [PATCH] Allow dragging menu using toolstrip grip --- ShareX.HelpersLib/Controls/ToolStripEx.cs | 23 +++++++++++++++++++ ShareX.HelpersLib/Native/NativeConstants.cs | 1 + ShareX.HelpersLib/Native/NativeMethods.cs | 6 +++++ .../Shapes/ShapeManager.cs | 20 ++++++++++------ 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/ShareX.HelpersLib/Controls/ToolStripEx.cs b/ShareX.HelpersLib/Controls/ToolStripEx.cs index f7e8de2eb..b82348333 100644 --- a/ShareX.HelpersLib/Controls/ToolStripEx.cs +++ b/ShareX.HelpersLib/Controls/ToolStripEx.cs @@ -24,12 +24,15 @@ #endregion License Information (GPL v3) using System; +using System.Drawing; using System.Windows.Forms; namespace ShareX.HelpersLib { public class ToolStripEx : ToolStrip { + public event MouseEventHandler GripMouseDown; + private bool clickThrough = false; public bool ClickThrough @@ -53,5 +56,25 @@ protected override void WndProc(ref Message m) m.Result = (IntPtr)NativeMethods.MA_ACTIVATE; } } + + protected override void OnMouseDown(MouseEventArgs mea) + { + if (GripRectangle.Offset(2).Contains(mea.Location)) + { + OnGripMouseDown(mea); + } + else + { + base.OnMouseDown(mea); + } + } + + protected virtual void OnGripMouseDown(MouseEventArgs mea) + { + if (GripMouseDown != null) + { + GripMouseDown(this, mea); + } + } } } \ No newline at end of file diff --git a/ShareX.HelpersLib/Native/NativeConstants.cs b/ShareX.HelpersLib/Native/NativeConstants.cs index 956ec05d7..6cce5804e 100644 --- a/ShareX.HelpersLib/Native/NativeConstants.cs +++ b/ShareX.HelpersLib/Native/NativeConstants.cs @@ -86,5 +86,6 @@ public static partial class NativeMethods public const uint MA_ACTIVATEANDEAT = 2; public const uint MA_NOACTIVATE = 3; public const uint MA_NOACTIVATEANDEAT = 4; + public const uint MOUSE_MOVE = 0xF012; } } \ No newline at end of file diff --git a/ShareX.HelpersLib/Native/NativeMethods.cs b/ShareX.HelpersLib/Native/NativeMethods.cs index 04782c1d8..efd9edb7e 100644 --- a/ShareX.HelpersLib/Native/NativeMethods.cs +++ b/ShareX.HelpersLib/Native/NativeMethods.cs @@ -51,6 +51,9 @@ public static partial class NativeMethods [DllImport("user32.dll")] public static extern IntPtr CopyIcon(IntPtr hIcon); + [DllImport("user32.dll")] + public static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, UIntPtr wParam, IntPtr lParam); + [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DestroyIcon(IntPtr hIcon); @@ -194,6 +197,9 @@ public static partial class NativeMethods [DllImport("user32.dll")] public static extern bool ReleaseCapture(); + [DllImport("user32.dll")] + public static extern bool ReleaseCapture(IntPtr hwnd); + [DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); diff --git a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs index 74d42e4f4..c86a2f609 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs @@ -271,6 +271,12 @@ private void CreateMenu() Text = "ToolStrip" }; + tsMain.GripMouseDown += (sender, e) => + { + NativeMethods.ReleaseCapture(tsMain.Handle); + NativeMethods.DefWindowProc(menuForm.Handle, (uint)WindowsMessages.SYSCOMMAND, (UIntPtr)NativeMethods.MOUSE_MOVE, IntPtr.Zero); + }; + tsMain.SuspendLayout(); menuForm.Controls.Add(tsMain); @@ -496,7 +502,7 @@ private void CreateMenu() AnnotationOptions.BorderColor = dialogColor.NewColor; } - UpdateContextMenu(); + UpdateMenu(); UpdateCurrentShape(); UpdateCursor(); } @@ -572,7 +578,7 @@ private void CreateMenu() AnnotationOptions.FillColor = dialogColor.NewColor; } - UpdateContextMenu(); + UpdateMenu(); UpdateCurrentShape(); } } @@ -632,7 +638,7 @@ private void CreateMenu() if (dialogColor.ShowDialog() == DialogResult.OK) { AnnotationOptions.HighlightColor = dialogColor.NewColor; - UpdateContextMenu(); + UpdateMenu(); UpdateCurrentShape(); } } @@ -780,14 +786,14 @@ private void CreateMenu() menuForm.Show(form); - UpdateContextMenu(); + UpdateMenu(); - CurrentShapeTypeChanged += shapeType => UpdateContextMenu(); + CurrentShapeTypeChanged += shapeType => UpdateMenu(); - CurrentShapeChanged += shape => UpdateContextMenu(); + CurrentShapeChanged += shape => UpdateMenu(); } - private void UpdateContextMenu() + private void UpdateMenu() { if (menuForm == null) return;