[Solved] How to make a picturebox dragable?


Yes, it is.

Assume a Picturebox named “pbxBigCat” (load it with a pPicture …)

Add this lines to your form:

    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

And then write an eventhandler for the pbxBigCat MouseDown event:

    private void pbxBigCat_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(pbxBigCat.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }

If you test it you will se it works. For more Information (like saving positions etc.) I refere to Make a borderless form movable?

Another possibility to do this (now we have a Label called label1)

public partial class Form1 : Form
{
    private bool mouseDown;
    private Point lastLocation;

    public Form1()
    {
        InitializeComponent();
    }

    private void pbxBigCat_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            mouseDown = true;
            lastLocation = e.Location;
        }
    }

    private void pbxBigCat_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseDown)
        {
            pbxBigCat.Location = new Point((pbxBigCat.Location.X - lastLocation.X + e.X), (pbxBigCat.Location.Y - lastLocation.Y) + e.Y);
            label1.Text = pbxBigCat.Location.X.ToString() + "https://stackoverflow.com/" + pbxBigCat.Location.Y.ToString();
        }
    }

    private void pbxBigCat_MouseUp(object sender, MouseEventArgs e)
    {
        mouseDown = false;
    }
}

Everything drawn from the example on the mentioned SO article.

6

solved How to make a picturebox dragable?