winform 画坐标轴
时间: 2023-09-14 10:10:23 浏览: 74
你可以使用WinForm来画坐标轴。下面是一个简单的示例代码,演示了如何使用C#和WinForm来画一个简单的坐标轴:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WinFormCoordinateAxis
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black);
// 绘制x轴
g.DrawLine(pen, 50, 200, 400, 200);
// 绘制y轴
g.DrawLine(pen, 50, 200, 50, 50);
// 绘制x轴刻度
for (int i = 0; i <= 10; i++)
{
int x = 50 + i * 35;
g.DrawLine(pen, x, 200, x, 205);
g.DrawString(i.ToString(), this.Font, Brushes.Black, x - 5, 207);
}
// 绘制y轴刻度
for (int i = 0; i <= 10; i++)
{
int y = 200 - i * 35;
g.DrawLine(pen, 45, y, 50, y);
g.DrawString(i.ToString(), this.Font, Brushes.Black, 30, y - 7);
}
}
}
}
```
在WinForm的设计视图中,将`Form1`的`Paint`事件与`Form1_Paint`方法关联。这样当窗体重绘时,就会调用该方法进行绘制。
这段代码会绘制一个大小为400x200像素的坐标轴,x轴上有10个刻度,y轴上也有10个刻度。你可以根据需要调整坐标轴的大小和刻度个数。