3

I have a situation where I would like to move a windows form by holding right mouse button on it's client area; the form it's borderless as i've stated.

I would like to move it "natively" (if possible, otherwise other answers are ok too). I mean the way it behaves when you hold left mouse button on the titlebar (with mouse-move and things like that I get a lot of strange behaviours, but maybe it's just me).

I've read around a lot of things and this post looks helpful

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b9985b19-cab5-4fba-9dc5-f323d0d37e2f/

I tried various way to use that and watched through http://msdn.microsoft.com/en-us/library/ff468877%28v=VS.85%29.aspx to find other useful things and WM_NCRBUTTONDOWN came in my mind, however the wndproc doesn't detect it, maybe because it's handled by the form?

Any suggestion are appreciated, thanks

Francesco

Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
  • Try using Spy++ to see what messages are generated when you left and right click. Maybe you can use the method in the link you provided and alter it to get the desired behavior. I think maybe you need a WM_SYSCOMMAND message in concert with SC_MOVE – Chris Dunaway Apr 29 '11 at 16:43
  • I tried now, it doesn't track my messages, nothing appear in messages window :\ – Francesco Belladonna Apr 30 '11 at 00:25
  • Could you please elaborate on the nature of the "strange behaviours" you see when using the .net MoveMove event? I've done precisely what you're talking about via MouseMove and was perfectly satisfied with the results. (i.e. right mouse in the CLIENT area of the form, and have it track with the movement...) – Jason D May 06 '11 at 03:51
  • Also: all windows messages starting with WM_NC are for the NON-CLIENT region of the window (e.g. Title bar) – Jason D May 06 '11 at 03:52
  • @Jason I got something like form not following mouse correctly and leaving a trail until I stop dragging – Francesco Belladonna May 06 '11 at 12:16

2 Answers2

5
public partial class DragForm : Form
{
    // Offset from upper left of form where mouse grabbed
    private Size? _mouseGrabOffset;

    public DragForm()
    {
        InitializeComponent();
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if( e.Button == System.Windows.Forms.MouseButtons.Right )
            _mouseGrabOffset = new Size(e.Location);

        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        _mouseGrabOffset = null;

        base.OnMouseUp(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (_mouseGrabOffset.HasValue)
        {
            this.Location = Cursor.Position - _mouseGrabOffset.Value;
        }

        base.OnMouseMove(e);
    }
}
John Arlen
  • 6,539
  • 2
  • 33
  • 42
1

You need two P/Invoke methods to get this done.

[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);

[DllImport("user32.dll")]
static extern bool ReleaseCapture();

A couple of constants:

const int WmNcLButtonDown = 0xA1;
const int HtCaption= 2;

Handle the MouseDown event on your form, then do this:

private void Form_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ReleaseCapture();
        SendMessage(this.Handle, WmNcLButtonDown, HtCaption, 0);
    }
}

This will send your form the same event it receives when the mouse clicks and holds down the caption area. Move the mouse and the window moves. When you release the mouse button, movement stops. Very easy.