-4

I am an enthusiastic programmer and new to stack overflow. I am trying to build a prototype of an OS(Operating System) in C#...Just a test.

Just like we see that we can drag things in the desktop and put it anywhere I am creating a desktop for my OS.

So how should I make an icon(a picturebox) drag-able and how would I save its position so that next time I open my desktop I see it in the same place? I would love the dragging without any freezes or those sneaky bugs. I would love if, it is as close and smooth as the ones in Windows(dragging items(icons) in desktop)..

Thanks...

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
s1nisteR
  • 1
  • 4

1 Answers1

0

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() + "/" + pbxBigCat.Location.Y.ToString();
        }
    }

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

Everything drawn from the example on the mentioned SO article.

Community
  • 1
  • 1
nabuchodonossor
  • 2,095
  • 20
  • 18