C#:为什么不以鼠标为中心进行缩放?

发布于 2024-10-04 01:00:48 字数 2893 浏览 4 评论 0原文

我正在尝试制作一个可以缩放和平移的画布。平移有效,但缩放鼠标位置不起作用。

编辑:这几乎有效。然而,缩放中心略有偏移...

这是缩放代码:

  public partial class Form1 : Form
    {

        PointF mouseDown;

        float newX;
        float newY;
        float zoomFactor = 1.0F;



        public Form1()
        {
            InitializeComponent();

            mouseDown = new PointF(0F, 0F);

            this.panel1.Paint += new PaintEventHandler(panel1_Paint);
            this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(panel1_MouseDown);
            this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

            base.OnPaint(e);

            Graphics dc = e.Graphics;

            dc.SmoothingMode = SmoothingMode.AntiAlias;

            dc.TranslateTransform(newX, newY);
            dc.ScaleTransform(zoomFactor, zoomFactor);

            Color lineColor = Color.FromArgb(200, 200, 200);
            Pen linePen = new Pen(lineColor,1*zoomFactor);

            dc.DrawLine(linePen, 100, 100, 200, 200);

            textBox1.Text = newX.ToString();
            textBox2.Text = newY.ToString();
        }



        private void panel1_MouseDown(object sender, EventArgs e)
        {
            MouseEventArgs mouse = e as MouseEventArgs;

            if (mouse.Button == MouseButtons.Right)
            {
                mouseDown = mouse.Location;

                mouseDown.X = mouseDown.X - newX;
                mouseDown.Y = mouseDown.Y - newY;

            }

        }

        private void panel1_MouseMove(object sender, EventArgs e)
        {
            MouseEventArgs mouse = e as MouseEventArgs;

            if (mouse.Button == MouseButtons.Right)
            {
                Point mousePosNow = mouse.Location;

                float deltaX = mousePosNow.X - mouseDown.X;
                float deltaY = mousePosNow.Y - mouseDown.Y;

                newX = deltaX;
                newY = deltaY;

                panel1.Invalidate();

            }
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {

            MouseEventArgs mouse = e as MouseEventArgs;

            if (e.Delta > 0)
            {
                if (zoomFactor + 0.2 < 10)
                {
                    zoomFactor += 0.2F;
                }
            }

            else if (e.Delta < 0)
            {
                if (zoomFactor - 0.2 > 0.2)
                {
                    zoomFactor -= 0.2F;
                }
            }

            float x = (mouse.Location.X - newX) * zoomFactor;
            float y = (mouse.Location.Y - newY) * zoomFactor;

            newX = mouse.Location.X - x;
            newY = mouse.Location.Y - y;

            panel1.Invalidate();

        }
    }

I'm trying to make a canvas where you can zoom and pan. The panning works, but zooming at the mouse position won't work.

EDIT: This almost works. The center for zoom is slightly offset however...

This is the code for the zooming:

  public partial class Form1 : Form
    {

        PointF mouseDown;

        float newX;
        float newY;
        float zoomFactor = 1.0F;



        public Form1()
        {
            InitializeComponent();

            mouseDown = new PointF(0F, 0F);

            this.panel1.Paint += new PaintEventHandler(panel1_Paint);
            this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(panel1_MouseDown);
            this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

            base.OnPaint(e);

            Graphics dc = e.Graphics;

            dc.SmoothingMode = SmoothingMode.AntiAlias;

            dc.TranslateTransform(newX, newY);
            dc.ScaleTransform(zoomFactor, zoomFactor);

            Color lineColor = Color.FromArgb(200, 200, 200);
            Pen linePen = new Pen(lineColor,1*zoomFactor);

            dc.DrawLine(linePen, 100, 100, 200, 200);

            textBox1.Text = newX.ToString();
            textBox2.Text = newY.ToString();
        }



        private void panel1_MouseDown(object sender, EventArgs e)
        {
            MouseEventArgs mouse = e as MouseEventArgs;

            if (mouse.Button == MouseButtons.Right)
            {
                mouseDown = mouse.Location;

                mouseDown.X = mouseDown.X - newX;
                mouseDown.Y = mouseDown.Y - newY;

            }

        }

        private void panel1_MouseMove(object sender, EventArgs e)
        {
            MouseEventArgs mouse = e as MouseEventArgs;

            if (mouse.Button == MouseButtons.Right)
            {
                Point mousePosNow = mouse.Location;

                float deltaX = mousePosNow.X - mouseDown.X;
                float deltaY = mousePosNow.Y - mouseDown.Y;

                newX = deltaX;
                newY = deltaY;

                panel1.Invalidate();

            }
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {

            MouseEventArgs mouse = e as MouseEventArgs;

            if (e.Delta > 0)
            {
                if (zoomFactor + 0.2 < 10)
                {
                    zoomFactor += 0.2F;
                }
            }

            else if (e.Delta < 0)
            {
                if (zoomFactor - 0.2 > 0.2)
                {
                    zoomFactor -= 0.2F;
                }
            }

            float x = (mouse.Location.X - newX) * zoomFactor;
            float y = (mouse.Location.Y - newY) * zoomFactor;

            newX = mouse.Location.X - x;
            newY = mouse.Location.Y - y;

            panel1.Invalidate();

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2024-10-11 01:00:48

我终于弄清楚了,所以这是解决方案。

public partial class Form1 : Form
{

    PointF mouseDown;

    float newX;
    float newY;
    float zoomFactor = 1.0F;


    public Form1()
    {
        InitializeComponent();

        mouseDown = new PointF(0F, 0F);

        this.panel1.Paint += new PaintEventHandler(panel1_Paint);
        this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(panel1_MouseDown);
        this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);

    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

        base.OnPaint(e);

        Graphics dc = e.Graphics;

        dc.SmoothingMode = SmoothingMode.AntiAlias;

        dc.TranslateTransform(newX, newY);

        dc.ScaleTransform(zoomFactor, zoomFactor, MatrixOrder.Prepend);

        Color lineColor = Color.FromArgb(200, 200, 200);
        Pen linePen = new Pen(lineColor,1*zoomFactor);

        dc.DrawLine(linePen, 100, 100, 200, 100);
        dc.DrawLine(linePen, 200, 100, 200, 200);
        dc.DrawLine(linePen, 200, 200, 100, 200);
        dc.DrawLine(linePen, 100, 200, 100, 100);
        dc.DrawLine(linePen, 100, 100, 200, 200);
        dc.DrawLine(linePen, 200, 100, 100, 200);

    }

    private void panel1_MouseDown(object sender, EventArgs e)
    {
        MouseEventArgs mouse = e as MouseEventArgs;

        if (mouse.Button == MouseButtons.Right)
        {
            mouseDown = mouse.Location;

            mouseDown.X = mouseDown.X - newX;
            mouseDown.Y = mouseDown.Y - newY;

        }

    }

    private void panel1_MouseMove(object sender, EventArgs e)
    {
        MouseEventArgs mouse = e as MouseEventArgs;

        if (mouse.Button == MouseButtons.Right)
        {
            PointF mousePosNow = mouse.Location;

            float deltaX = mousePosNow.X - mouseDown.X;
            float deltaY = mousePosNow.Y - mouseDown.Y;

            newX = deltaX;
            newY = deltaY;

            panel1.Invalidate();

        }


    }

    protected override void OnMouseWheel(MouseEventArgs e)
    {

        MouseEventArgs mouse = e as MouseEventArgs;

        PointF mP = mouse.Location;

        if (e.Delta > 0)
        {
            if (zoomFactor >= 1 && zoomFactor <= 10)
            {
                zoomFactor += 1F;

                newX = newX - ((mP.X - newX) / (zoomFactor - 1));
                newY = newY - ((mP.Y - newY) / (zoomFactor - 1));
            }
            else if (zoomFactor == 0.5)
            {
                zoomFactor = zoomFactor * 2;
                newX = newX - ((mP.X - newX) );
                newY = newY - ((mP.Y - newY) );
            }
            else if (zoomFactor < 0.5)
            {
                zoomFactor = zoomFactor * 2;
                newX = newX - ((mP.X - newX) );
                newY = newY - ((mP.Y - newY) );
            }
        }

        else if (e.Delta < 0)
        {
            if (zoomFactor >2)
            {
                zoomFactor -= 1F;
                newX = newX + (((mP.X - newX)) / (zoomFactor+1 ));
                newY = newY + (((mP.Y - newY)) / (zoomFactor+1));
            }
            else if (zoomFactor == 2) {
                zoomFactor -= 1F;

                newX = newX + ((mP.X - newX)/2);
                newY = newY + ((mP.Y - newY)/2);
            }else if(zoomFactor <= 1 && zoomFactor > 0.2)
            {
                zoomFactor = zoomFactor / 2;

                newX = newX + ((mP.X - newX) / 2);
                newY = newY + ((mP.Y - newY) / 2);

            }


        }

        panel1.Invalidate();

    }
}

I finally figured it out, so here's the solution.

public partial class Form1 : Form
{

    PointF mouseDown;

    float newX;
    float newY;
    float zoomFactor = 1.0F;


    public Form1()
    {
        InitializeComponent();

        mouseDown = new PointF(0F, 0F);

        this.panel1.Paint += new PaintEventHandler(panel1_Paint);
        this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(panel1_MouseDown);
        this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);

    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

        base.OnPaint(e);

        Graphics dc = e.Graphics;

        dc.SmoothingMode = SmoothingMode.AntiAlias;

        dc.TranslateTransform(newX, newY);

        dc.ScaleTransform(zoomFactor, zoomFactor, MatrixOrder.Prepend);

        Color lineColor = Color.FromArgb(200, 200, 200);
        Pen linePen = new Pen(lineColor,1*zoomFactor);

        dc.DrawLine(linePen, 100, 100, 200, 100);
        dc.DrawLine(linePen, 200, 100, 200, 200);
        dc.DrawLine(linePen, 200, 200, 100, 200);
        dc.DrawLine(linePen, 100, 200, 100, 100);
        dc.DrawLine(linePen, 100, 100, 200, 200);
        dc.DrawLine(linePen, 200, 100, 100, 200);

    }

    private void panel1_MouseDown(object sender, EventArgs e)
    {
        MouseEventArgs mouse = e as MouseEventArgs;

        if (mouse.Button == MouseButtons.Right)
        {
            mouseDown = mouse.Location;

            mouseDown.X = mouseDown.X - newX;
            mouseDown.Y = mouseDown.Y - newY;

        }

    }

    private void panel1_MouseMove(object sender, EventArgs e)
    {
        MouseEventArgs mouse = e as MouseEventArgs;

        if (mouse.Button == MouseButtons.Right)
        {
            PointF mousePosNow = mouse.Location;

            float deltaX = mousePosNow.X - mouseDown.X;
            float deltaY = mousePosNow.Y - mouseDown.Y;

            newX = deltaX;
            newY = deltaY;

            panel1.Invalidate();

        }


    }

    protected override void OnMouseWheel(MouseEventArgs e)
    {

        MouseEventArgs mouse = e as MouseEventArgs;

        PointF mP = mouse.Location;

        if (e.Delta > 0)
        {
            if (zoomFactor >= 1 && zoomFactor <= 10)
            {
                zoomFactor += 1F;

                newX = newX - ((mP.X - newX) / (zoomFactor - 1));
                newY = newY - ((mP.Y - newY) / (zoomFactor - 1));
            }
            else if (zoomFactor == 0.5)
            {
                zoomFactor = zoomFactor * 2;
                newX = newX - ((mP.X - newX) );
                newY = newY - ((mP.Y - newY) );
            }
            else if (zoomFactor < 0.5)
            {
                zoomFactor = zoomFactor * 2;
                newX = newX - ((mP.X - newX) );
                newY = newY - ((mP.Y - newY) );
            }
        }

        else if (e.Delta < 0)
        {
            if (zoomFactor >2)
            {
                zoomFactor -= 1F;
                newX = newX + (((mP.X - newX)) / (zoomFactor+1 ));
                newY = newY + (((mP.Y - newY)) / (zoomFactor+1));
            }
            else if (zoomFactor == 2) {
                zoomFactor -= 1F;

                newX = newX + ((mP.X - newX)/2);
                newY = newY + ((mP.Y - newY)/2);
            }else if(zoomFactor <= 1 && zoomFactor > 0.2)
            {
                zoomFactor = zoomFactor / 2;

                newX = newX + ((mP.X - newX) / 2);
                newY = newY + ((mP.Y - newY) / 2);

            }


        }

        panel1.Invalidate();

    }
}
梦幻的味道 2024-10-11 01:00:48

也许您的 MousePosition 与 PanelPosition 不同?我的意思是 on 是相对于控件的,而 1 是相对于屏幕的?

Control.PointToScreen 是您的朋友!

Maybe your MousePosition is different from PanelPosition? I mean on is relative to the control and one relative to the screen?

Control.PointToScreen is your friend!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文