C# Graphics 问题
答案:1 悬赏:40 手机版
解决时间 2021-03-31 17:59
- 提问者网友:愿为果
- 2021-03-31 14:09
C# Graphics 问题
最佳答案
- 五星知识达人网友:轻熟杀无赦
- 2021-03-31 14:29
问题应该出在你的e上面,这个e里面有你datagridview的单元格信息,包括行索引,列索引等等,如果你想将重绘做成一个函数,那么这个DataGridViewCellPaintingEventArgs变量e应该是做为参数来自拟的重绘事件,比如datagridview_CellPainting等等,有了这个信息,你才能取得e.CellBounds等信息...
另外使用完画刷(brush),画笔后一定要释放回收,损耗太大了...
这一段是我的重绘,拆分单元格
/// ---------------------------------------------------------------------------------------
///
/// 重绘dataGridView函数
///
///
/// 重绘单元格内绘制的文字内容 :ArrayList[]
///
///
/// 系统变量 :DataGridViewCellPaintingEventArgs
///
/// ---------------------------------------------------------------------------------------
private void ReDrawDatagridView(List arrayList, DataGridViewCellPaintingEventArgs e)
{
//使用刷子
using (
Brush gridBrush = new SolidBrush(dgvOperation.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
//使用画笔
using (Pen gridLinePen = new Pen(gridBrush))
{
// 擦除原单元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
//绘制线条,这些线条是单元格相互间隔的区分线条
for (int i = 0; i < this.dgvOperation.Rows.Count; i++)
{
if (e.RowIndex == i)
{
for (int j = 0; j < (int)accountCountArray[i]; j++)
{
//下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - ((e.CellBounds.Height / (int)accountCountArray[i]) * j),
e.CellBounds.Right - 1,
e.CellBounds.Bottom - ((e.CellBounds.Height / (int)accountCountArray[i]) * j));
//重绘金额栏的情况
if (e.ColumnIndex == 6 || e.ColumnIndex == 7)
{
float moneyWeight = 96 / (72 / e.CellStyle.Font.Size);
//画金额
e.Graphics.DrawString(arrayList[i][j].ToString(), e.CellStyle.Font, Brushes.Black,
e.CellBounds.Right - (arrayList[i][j].ToString().Length) * (int)(moneyWeight / 2)
- (int)(moneyWeight / 2), e.CellBounds.Y + 20 * j + 5, StringFormat.GenericDefault);
}
else
{
//画文字
e.Graphics.DrawString(arrayList[i][j].ToString(), e.CellStyle.Font, Brushes.Black,
e.CellBounds.X, e.CellBounds.Y + 20 * j + 5, StringFormat.GenericDefault);
}
}
//底
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right, e.CellBounds.Bottom - 1);
}
}
//右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Handled = true;
}
}
}
另外使用完画刷(brush),画笔后一定要释放回收,损耗太大了...
这一段是我的重绘,拆分单元格
/// ---------------------------------------------------------------------------------------
///
/// 重绘dataGridView函数
///
///
/// 重绘单元格内绘制的文字内容 :ArrayList[]
///
///
/// 系统变量 :DataGridViewCellPaintingEventArgs
///
/// ---------------------------------------------------------------------------------------
private void ReDrawDatagridView(List arrayList, DataGridViewCellPaintingEventArgs e)
{
//使用刷子
using (
Brush gridBrush = new SolidBrush(dgvOperation.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
//使用画笔
using (Pen gridLinePen = new Pen(gridBrush))
{
// 擦除原单元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
//绘制线条,这些线条是单元格相互间隔的区分线条
for (int i = 0; i < this.dgvOperation.Rows.Count; i++)
{
if (e.RowIndex == i)
{
for (int j = 0; j < (int)accountCountArray[i]; j++)
{
//下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - ((e.CellBounds.Height / (int)accountCountArray[i]) * j),
e.CellBounds.Right - 1,
e.CellBounds.Bottom - ((e.CellBounds.Height / (int)accountCountArray[i]) * j));
//重绘金额栏的情况
if (e.ColumnIndex == 6 || e.ColumnIndex == 7)
{
float moneyWeight = 96 / (72 / e.CellStyle.Font.Size);
//画金额
e.Graphics.DrawString(arrayList[i][j].ToString(), e.CellStyle.Font, Brushes.Black,
e.CellBounds.Right - (arrayList[i][j].ToString().Length) * (int)(moneyWeight / 2)
- (int)(moneyWeight / 2), e.CellBounds.Y + 20 * j + 5, StringFormat.GenericDefault);
}
else
{
//画文字
e.Graphics.DrawString(arrayList[i][j].ToString(), e.CellStyle.Font, Brushes.Black,
e.CellBounds.X, e.CellBounds.Y + 20 * j + 5, StringFormat.GenericDefault);
}
}
//底
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right, e.CellBounds.Bottom - 1);
}
}
//右侧的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Handled = true;
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯