Domanda

Ho una situazione in cui mi piacerebbe spostare un modulo finestre tenendo premuto il tasto destro del mouse su di essa la zona del cliente; la forma che di margini, come ho già detto.

Vorrei spostarlo "nativo" (se possibile, altrimenti le altre risposte sono ok troppo). Mi riferisco al modo in cui si comporta quando si tiene il pulsante sinistro del mouse sulla barra del titolo (con il mouse-move e cose del genere che ho un sacco di comportamenti strani, ma forse è solo a me).

Ho letto in giro un sacco di cose e questo post sembra utile

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

Ho provato vario modo per utilizzare che a guardare attraverso http://msdn.microsoft.com/en-us/library/ff468877%28v=VS.85%29.aspx per trovare altre cose utili e WM_NCRBUTTONDOWN è venuto nella mia mente, ma il doesn WndProc' t rilevarlo, forse perché è gestita dal modulo?

Ogni suggerimento sono apprezzati, grazie

Francesco

È stato utile?

Soluzione

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top