1、建立坐标
2、坐标系的放大缩小
3、标定刻度
4、实时显示鼠标在坐标系上的坐标
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace coordinates
{public partial class Form1 : Form{private PictureBox pictureBox;private float zoomFactor = 1.2f; //初始放缩因子private float zoomLevel = 1.0f; //每次滚动缩放变化的步长private Point offset; // 视图偏移量public Form1(){InitializeComponent();// 初始化 PictureBoxpictureBox = new PictureBox{Dock = DockStyle.Fill, //填满窗体SizeMode = PictureBoxSizeMode.AutoSize};this.Controls.Add(pictureBox);// 订阅 Paint 事件pictureBox1.Paint += PictureBox_Paint;// 为pictureBox1的MouseWheel事件添加处理程序this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);//订阅鼠标移动事件this.pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);}private string fullname = null;//图像文件完整路径public Form1(string a){InitializeComponent1();fullname = a;}private int originalWidth;private int originalHeight;private void InitializeComponent1(){// 初始化 PictureBox 的原始大小originalWidth = pictureBox1.Width;originalHeight = pictureBox1.Height;// 其他初始化代码}//鼠标滚轮事件private void pictureBox1_MouseWheel(object sender, MouseEventArgs e){// 根据鼠标滚轮的滚动方向调整缩放级别if (e.Delta > 0){zoomLevel *= 1.1f; // 放大}else{zoomLevel /= 1.1f; // 缩小}// 确保缩放级别在合理范围内(放大到最大,缩小到最小)zoomLevel = Math.Max(0.1f, Math.Min(1.0f, zoomLevel));// 重新绘制 PictureBoxpictureBox1.Invalidate();}//画板制作--网格坐标private void PictureBox_Paint(object sender, PaintEventArgs e){// 网络坐标Graphics g = e.Graphics;Pen gridPen = new Pen(Color.LightGray, 1); // 网格线的颜色和宽度Font font = new Font("Arial", 8);SolidBrush brush = new SolidBrush(Color.Black); //实例画刷// 获取 PictureBox 的尺寸int width = pictureBox1.Width;int height = pictureBox1.Height;// 根据放缩级别调整网格线的绘制间隔int gridSize = (int)(15 / zoomLevel); // zoomLevel步长// ———————————绘制网格坐标线————————————//水平线for (int i = width / 2; i <= width; i += gridSize){g.DrawLine(gridPen, i, 0, i, height);}for (int i = width / 2 - gridSize; i >= 0; i -= gridSize){g.DrawLine(gridPen, i, 0, i, height);}// 垂直线for (int i = height / 2; i <= height; i += gridSize){g.DrawLine(gridPen, 0, i, width, i);}for (int i = height / 2 - gridSize; i >= 0; i -= gridSize){g.DrawLine(gridPen, 0, i, width, i);}// 绘制坐标轴Pen axisPen = new Pen(Color.Black, 2);axisPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; // 设置为虚线g.DrawLine(axisPen, 0, height / 2, width, height / 2); // X轴g.DrawLine(axisPen, width / 2, 0, width / 2, height); // Y轴// 绘制顶部刻度线// 假设你想让刻度值从 0 开始,每条刻度线增加 1int scaleValue = 0;for (int i = width / 2; i <= width; i += gridSize){g.DrawLine(axisPen, i, 0, i, 5); // 顶部刻度线g.DrawString(scaleValue.ToString(), font, brush, i - 5, 10); // 顶部刻度值//scaleValue++;scaleValue += 5;}scaleValue =0;for (int i = width / 2 ; i >= 0; i -= gridSize){g.DrawLine(axisPen, i, 0, i, 5); // 顶部刻度线g.DrawString(scaleValue.ToString(), font, brush, i - 5, 10); // 顶部刻度值scaleValue -= 5;}// 绘制左侧刻度线// 假设你想让刻度值从 0 开始,每条刻度线增加 1int leftScaleValue = 0;for (int i = height / 2; i <= height; i += gridSize){g.DrawLine(axisPen, 0, i, 5, i); // 左侧刻度线g.DrawString(leftScaleValue.ToString(), font, brush, 10, i - 5); // 左侧刻度值leftScaleValue -= 5;}leftScaleValue = 5;for (int i = height / 2 - gridSize; i >= 0; i -= gridSize){g.DrawLine(axisPen, 0, i, 5, i); // 左侧刻度线g.DrawString(leftScaleValue.ToString(), font, brush, 10, i - 5); // 左侧刻度值leftScaleValue += 5;}//刻度线边缘线Pen sige_Pen = new Pen(Color.Black, 2);sige_Pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; // 设置为实线g.DrawLine(sige_Pen, 0, 0, width,0); g.DrawLine(sige_Pen, 0, 0, 0, height);g.DrawLine(sige_Pen, width, 0, width, height);g.DrawLine(sige_Pen, 0, height, width, height);}private void pictureBox1_Click(object sender, EventArgs e){}private void pictureBox1_MouseDown(object sender, MouseEventArgs e){}//鼠标移动事件private void pictureBox1_MouseMove(object sender, MouseEventArgs e){// 计算鼠标相对于中心点的偏移量(原点)int centerX = pictureBox1.Width / 2; int centerY = pictureBox1.Height / 2;// 获取相对中心点的偏移量并根据缩放级别进行调整float gridX = ((e.X - centerX) * zoomLevel / 15)*5;float gridY = ((centerY - e.Y) * zoomLevel / 15)*5;// 显示当前的网络坐标textBox1.Text = $"X: {gridX:F2}, Y: {gridY:F2}";}private void pictureBox1_MouseDown_1(object sender, MouseEventArgs e){}}
}