views:

403

answers:

2

Hello !

I have a project in C#, WindowsForms and I created a panel that contains a pictureBox that is much bigger than his parent.

I turned panel.AutoScroll to true and what I want to do is dragging this pictureBox in panel instead of catching a scroll and moving it.

I.e. when I grab an image and move cursor to left and down I would like to get the same behavior as I will do it with panel's scrolls.

How to do it ?

+2  A: 

Ok, I got it. ;-) If anyone else has the same problem, here is solution:

    protected Point clickPosition;
    protected Point scrollPosition; 

    private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        this.clickPosition.X = e.X;
        this.clickPosition.Y = e.Y;
    }

    private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            scrollPosition.X = scrollPosition.X + clickPosition.X - e.X;
            scrollPosition.Y = scrollPosition.Y + clickPosition.Y - e.Y;
            this.panel.AutoScrollPosition = scrollPosition;
        }
    }
hsz
A: 

a smaller variant of the hsz solution :)

    protected Point clickPosition;
    protected Point scrollPosition;

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        this.clickPosition = e.Location;            
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.SuspendLayout();
            this.scrollPosition += (Size)clickPosition - (Size)e.Location;
            this.panel1.AutoScrollPosition = scrollPosition;                    
            this.ResumeLayout(false);
        }
    }
serhio