Windows 窗体 - 在面板中放大和缩小

问题描述 投票:0回答:1

我已经制作了类似绘图应用程序的东西,但我需要实现一个使用鼠标右键进行放大/缩小的功能。我添加了缩放功能,但它在左上角进行缩放。 我尝试在给定坐标处放大,但两次尝试都没有成功。

    float zoom = 1f;

    private void canvasPanel_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.ScaleTransform(zoom, zoom);
        

        DrawShapesFromListOnPaint(e);
    }

      ...

    private void canvasPanel_MouseClick(object sender, MouseEventArgs e) // For Right Click //Zooming
    {
        switch (e.Button)
        {
            case MouseButtons.Right:
                {

                    if (zoomed)
                    {
                        zoom -= 1;
                        zoomed = false;
                    }
                    else
                    {
                        zoom += 1;
                        zoomed = true;


                        zoomPoint = canvasPanel.PointToClient(Cursor.Position);

                    }

                    canvasPanel.Invalidate();
                }


                break;
            case MouseButtons.Left:
                ...
                break;
        }
    }
c# winforms panel
1个回答
0
投票

正如 Hans Passant 所说,您还需要 TranslateTransform() 调用。我写了一个例子,左键放大,右键缩小,请参考:

using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace ZoomingTest
{
  public partial class Form1 : Form
  {
    public Form1() {
      InitializeComponent();
    }

    private Matrix transform = new Matrix();
    private float m_dZoomscale = 1.0f;
    public const float _dScrollValue = 0.1f;

    private void panel1_Paint(object sender, PaintEventArgs e) {
      Graphics g = e.Graphics;
      g.Transform = transform;
      Pen mypen = new Pen(Color.Red, 5);
      Rectangle rect = new Rectangle(10, 10, 30, 30);
      e.Graphics.DrawRectangle(mypen, rect);
    }

    //protected override void OnMouseWheel(MouseEventArgs mea) {
    //  panel1.Focus();
    //  if (panel1.Focused == true && mea.Delta != 0)
    //  {
    //    // Map the Form-centric mouse location to the PictureBox client coordinate system
    //    Point pictureBoxPoint = panel1.PointToClient(this.PointToScreen(mea.Location));
    //    ZoomScroll(pictureBoxPoint, mea.Delta > 0);
    //  }
    //}

    private void ZoomScroll(Point location, bool zoomIn) {
      // Figure out what the new scale will be. Ensure the scale factor remains between
      // 1% and 1000%
      float newScale = Math.Min(Math.Max(m_dZoomscale + (zoomIn ? _dScrollValue : -_dScrollValue), 0.1f), 10);

      if (newScale != m_dZoomscale)
      {
        float adjust = newScale / m_dZoomscale;
        m_dZoomscale = newScale;

        // Translate mouse point to origin
        transform.Translate(-location.X, -location.Y, MatrixOrder.Append);

        // Scale view
        transform.Scale(adjust, adjust, MatrixOrder.Append);

        // Translate origin back to original mouse point.
        transform.Translate(location.X, location.Y, MatrixOrder.Append);

        panel1.Invalidate();
      }
    }

    private void panel1_MouseClick(object sender, MouseEventArgs e) {
      panel1.Focus();

      Point pictureBoxPoint = panel1.PointToClient(this.PointToScreen(e.Location));

      if (panel1.Focused == true && e.Button == MouseButtons.Left)
      {
        // Left click to zoom in
        ZoomScroll(pictureBoxPoint, true);
      }
      else if (panel1.Focused == true && e.Button == MouseButtons.Right)
      {
        // Right click to zoom out
        ZoomScroll(pictureBoxPoint, false);
      }
    }
  }
}

效果如下:

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.