UenoLucas
UenoLucas

Reputation: 1

How make scroll follow mouse location when pictureBox is scale inside a panel windows forms c#

I have a pictureBox in a Panel and I've been have problems to program the panel's scroll follow the mouse position in pictureBox. Observation: I set the picturebox in the middle of panel, but if picturebox1.Width > panel1.Width i set the picturebox to location (0,0). Here is my code:

        public float scaleModification = 1f;


        private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (ModifierKeys.HasFlag(Keys.Control))
            {
                setPictureToLocationZero();
                if (e.Delta > 0)
                {
                    scaleModification *= 1.1f;
                    pictureBox1.Scale(new SizeF(1.1f, 1.1f));
                }
                else
                {
                    scaleModification *= 0.9f;
                    pictureBox1.Scale(new SizeF(0.9f, 0.9f));
                }
                setPanelScroll(new PointF(e.X, e.Y));
            }
        }
        private void setPanelScroll(PointF mousePos)
        {
            //panel1.ScrollControlIntoView(pictureBox1);
            var p = panel1.AutoScrollPosition;
            p.X = -p.X;
            p.Y = -p.Y;
            p.X += (int)((mousePos.X * scaleModification) - mousePos.X) ;
            p.Y += (int)((mousePos.Y * scaleModification) - mousePos.Y);
            panel1.AutoScrollPosition = new Point(p.X,p.Y);
        }
        private void pictureBox1_SizeChanged(object sender, EventArgs e)
        {
            if (pictureBox1.Width > panel1.Width)
            {
                setPictureToLocationZero();
            }
            else
            {
                setPictureToMiddle();
            }
        }
        private void setPictureToLocationZero()
        {
            pictureBox1.Location = new Point(0, 0);
        }


        private void setPictureToMiddle()
        {
            int x = (panel1.Width - pictureBox1.Width) / 2;
            int y = (panel1.Height - pictureBox1.Height) / 2;
            pictureBox1.Location = new Point(x,y);
        }

I expect when the mouse wheel scroll zoom is activate the panel's scrool follow the cursor location in picturebox. I will appreciate a lot any help!

Upvotes: 0

Views: 78

Answers (1)

Wenbin Geng
Wenbin Geng

Reputation: 3688

Your code is not triggering the mouse wheel event. You need to trigger the scroll wheel event to resize it to achieve your desired effect.

The code to trigger the mouse wheel event is as follows:

public Form1()
    {
        InitializeComponent();
        this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
    }
[DllImport("user32.dll")]
public static extern int WindowFromPoint(int xPoint, int yPoint);
 
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
    System.Drawing.Point p = PointToScreen(e.Location);
    if (WindowFromPoint(p.X, p.Y) == pictureBox1.Handle.ToInt32())
    {
        if (e.Delta == 120)
        {
            MessageBox.Show("Bigger");
        }
        else if (e.Delta < 0)
        {
            MessageBox.Show("Smaller");
        }
    }
}

Upvotes: 0

Related Questions