PictureBox 在鼠标滚轮上缩放和滚动 C# [英] PictureBox zoom and scroll on mouse wheel C#

查看:53
本文介绍了PictureBox 在鼠标滚轮上缩放和滚动 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建可以放大/缩小光标的图片框,例如谷歌地图.

I'm trying to create pictureBox with which I could zoom in/out to cursor, like google maps.

一些代码:

int viewRectWidth;
int viewRectHeight;
public float zoomshift = 0.05f;
int xForScroll;
int yForScroll;
float zoom = 1.0f;
public float Zoom
{
  get { return zoom; }
  set
  {
    if (value < 0.001f) value = 0.001f;
    zoom = value;
    displayScrollbar();
    setScrollbarValues();
    Invalidate();
  }
}
Size canvasSize = new Size(60, 40);
public Size CanvasSize
{
  get { return canvasSize; }
  set
  {
    canvasSize = value;
    displayScrollbar();
    setScrollbarValues();
    Invalidate();
  }
}
Bitmap image;
public Bitmap Image
{
  get { return image; }
  set
  {
    image = value;
    displayScrollbar();
    setScrollbarValues();
    Invalidate();
  }
}
InterpolationMode interMode = InterpolationMode.HighQualityBilinear;
public InterpolationMode InterpolationMode
{
  get { return interMode; }
  set { interMode = value; }
}
public ZoomablePictureBox()
{
  InitializeComponent();
  this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | 
                ControlStyles.ResizeRedraw | ControlStyles.UserPaint | 
                ControlStyles.DoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
  base.OnPaint(e);
  if(image != null)
  {
    Rectangle srcRect, distRect;
    Point pt = new Point((int)(hScrollBar1.Value / zoom), (int)(vScrollBar1.Value / zoom));
    if (canvasSize.Width * zoom < viewRectWidth) 
      srcRect = new Rectangle(0, 0, canvasSize.Width, canvasSize.Height);
    else srcRect = new Rectangle(pt, new Size((int)(viewRectWidth / zoom), (int)(viewRectHeight / zoom)));
    distRect = new Rectangle((int)(-srcRect.Width / 2), -srcRect.Height / 2, srcRect.Width, srcRect.Height);

    Matrix mx = new Matrix();
    mx.Scale(zoom, zoom);
    mx.Translate(viewRectWidth / 2.0f, viewRectHeight / 2.0f, MatrixOrder.Append);

    Graphics g = e.Graphics;
    g.InterpolationMode = interMode;
    g.Transform = mx;
    g.DrawImage(image, distRect, srcRect, GraphicsUnit.Pixel);
  }
}

现在我需要鼠标滚轮事件来缩放和滚动到鼠标点,但我无法弄清楚应该设置滚动条的值的公式.

Now I need mouse wheel event to zoom and scroll to mouse point, and I just can't figure out formula to what values I should set scrollBars.

private void onMouseWheel(object sender, MouseEventArgs e)
{
  if (ModifierKeys == Keys.Control)
  {
    this.Zoom += e.Delta / 120 * this.zoomshift;
    vScrollBar1.Value = ?;
    hScrollBar1.Value = ?;
  }
}

任何帮助将不胜感激.

问候,托马斯

推荐答案

如果你想让滚动条保持在相同的相对位置,我认为以下应该可行:

If you want to keep the scrollbars at the same relative position, I think the following should work:

float oldvMax = vScrollBar1.Maximum;
int oldvValue = vScrollBar1.Value;
float oldhMax = hScrollBar1.Maximum;
int oldhValue = hScrollBar1.Value;
this.Zoom += e.Delta / 120 * this.zoomshift;
vScrollBar1.Value = (int)((oldvValue / oldvMax) * vScrollBar1.Maximum);
hScrollBar1.Value = (int)((oldhValue / oldhMax) * hScrollBar1.Maximum);

这篇关于PictureBox 在鼠标滚轮上缩放和滚动 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆