简体   繁体   English

PictureBox在鼠标滚轮C#上缩放和滚动

[英]PictureBox zoom and scroll on mouse wheel C#

I'm trying to create pictureBox with which I could zoom in/out to cursor, like google maps. 我正在尝试创建pictureBox,可以用它放大/缩小光标,例如Google Maps。

Some code: 一些代码:

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. 现在,我需要鼠标滚轮事件来缩放和滚动到鼠标点,但是我无法弄清楚应该设置scrollBars的值的公式。

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

any help would be appreciated. 任何帮助,将不胜感激。

Regards, Tomas 问候,托马斯

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM