C#:为什么不以鼠标为中心进行缩放?
我正在尝试制作一个可以缩放和平移的画布。平移有效,但缩放鼠标位置不起作用。
编辑:这几乎有效。然而,缩放中心略有偏移...
这是缩放代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于弄清楚了,所以这是解决方案。
I finally figured it out, so here's the solution.
也许您的 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!