当用户制作新网格时,我想要显示如下的波浪线:
我该怎么做? Currenlty,我通过调用DrawLine
来绘制它,但它需要很长时间才能完成它。
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics l = e.Graphics;
Pen p = new Pen(Color.Red, 1);
l.DrawLine(p ,10 ,5,5,0);
l.DrawLine(p, 10, 5, 15, 0);
l.DrawLine(p, 20, 5, 15, 0);
l.DrawLine(p, 20, 5, 25, 0);
l.DrawLine(p, 30, 5, 25, 0);
l.Dispose();
}
答案 0 :(得分:1)
为DataGridView.CellPainting
事件添加处理程序。
dataGridView1.CellPainting += dataGridView1_CellPainting;
这里实施。注意:这些行位于“白色框”之外。细胞。
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
var dg = (DataGridView) sender;
if (e.ColumnIndex == -1 || e.RowIndex != (dg.RowCount - 1))
return;
using (var p = new Pen(Color.Red, 1))
{
var cellBounds = e.CellBounds;
const int size = 2;
var pts = new List<Point>();
var h = false;
for (int i = cellBounds.Left; i <= cellBounds.Right; i += size,h = !h)
{
pts.Add(
new Point
{
X = i,
Y = h ? cellBounds.Bottom : cellBounds.Bottom + size
});
}
e.Graphics.DrawLines(p, pts.ToArray());
}
}
输出:
我希望它有所帮助。