111,094
社区成员




用以下代码画网格,可是显示出的线段不能对齐,甚至横线在竖线右部的外边缘还冒出了一段,不知何故。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
Pen bluePen = new Pen(Color.Blue, 3);
for (int i = 0; i < this.pictureBox1.Width; i++)
{
;
g.DrawLine(bluePen, new Point(i, 0), new Point(i, this.pictureBox1.Height));
i += 60;
}
//水平
for (int j = 0; j < this.pictureBox1.Height; j++)
{
g.DrawLine(bluePen, new Point(0, j), new Point(this.pictureBox1.Width, j));
j += (this.pictureBox1.Height - 6) / 10;
}
}
}
}
计算i与j的值 其实很简单,你需要先确定行列数
dx = Width/ 列数
dy = Height/行数
竖线在最底部也冒出头了
如果要正方形网格,设置pictureBox的Width与Height相等,另外还要计算i与j的值,这个办法是不是比较麻烦?
以第一行为例进行分析,横线最右边的点的坐标为:
new Point(0, this.pictureBox1.Height)
最右边竖线与第一行横线交点的坐标是:
new Point(this.pictureBox1.Width,this.pictureBox1.Height-1)
是不是这样?我也是糊涂了。
点 new Point(i, this.pictureBox1.Height) 在 点 new Point(this.pictureBox1.Width, j) 的右边,这就是横线出头的原因