Allow dragging menu using toolstrip grip

This commit is contained in:
Jaex 2016-09-30 21:18:30 +03:00
parent ca1d2b6e38
commit 4d515d2a9e
4 changed files with 43 additions and 7 deletions

View file

@ -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);
}
}
}
}

View file

@ -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;
}
}

View file

@ -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);

View file

@ -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;