winform的picturebox中绘制出的坐标轴怎么将放大缩小中心固定到坐标原点而不是控件左上角
时间: 2024-05-01 21:16:48 浏览: 137
要将放大缩小中心固定到坐标原点而不是控件左上角,可以通过以下步骤实现:
1. 在PictureBox的Paint事件中绘制坐标轴时,将绘制坐标轴的起点设为PictureBox的中心点,而不是左上角。
2. 在放大和缩小的事件中,计算出缩放后中心点的坐标,并将其设置为PictureBox的中心点。
代码示例:
```
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// 获取PictureBox的中心点
Point center = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2);
// 绘制坐标轴
Pen pen = new Pen(Color.Black);
e.Graphics.DrawLine(pen, center.X, 0, center.X, pictureBox1.Height);
e.Graphics.DrawLine(pen, 0, center.Y, pictureBox1.Width, center.Y);
}
private void btnZoomIn_Click(object sender, EventArgs e)
{
// 计算缩放后中心点的坐标
Point center = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2);
int newX = (int)(center.X / 1.2);
int newY = (int)(center.Y / 1.2);
// 设置PictureBox的中心点
pictureBox1.Location = new Point(center.X - newX, center.Y - newY);
pictureBox1.Size = new Size((int)(pictureBox1.Width / 1.2), (int)(pictureBox1.Height / 1.2));
}
private void btnZoomOut_Click(object sender, EventArgs e)
{
// 计算缩放后中心点的坐标
Point center = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2);
int newX = (int)(center.X * 1.2);
int newY = (int)(center.Y * 1.2);
// 设置PictureBox的中心点
pictureBox1.Location = new Point(center.X - newX, center.Y - newY);
pictureBox1.Size = new Size((int)(pictureBox1.Width * 1.2), (int)(pictureBox1.Height * 1.2));
}
```
阅读全文