C#中如何在ListView控件第一列中绘制矩形
2个回答
展开全部
private void CustomForm_Load(object sender, EventArgs e)
{
for (int i = 0; i < 16; i++)
{
ListViewItem item = new ListViewItem();
item.Name = item.Text = (i * 4).ToString();
item.SubItems.AddRange(new string[] {
(i * 4 + 1).ToString(), (i * 4 + 2).ToString(),
(i * 4 + 3).ToString()});
this.listView1.Items.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
Random ColorNo = new Random(0);
for (int i = 0; i < this.listView1.Items.Count; i++)
{
Point leftop = this.listView1.Items[i].SubItems[0].Bounds.Location;//获取左上角点
int height = this.listView1.Items[i].SubItems[0].Bounds.Height;//获取表格高度
int width = this.listView1.Columns[0].Width;//取列度为表格的宽度
using (Graphics g = this.listView1.CreateGraphics())
{
g.DrawRectangle(new Pen(Color.FromArgb(ColorNo.Next(0, 255), ColorNo.Next(0, 255), ColorNo.Next(0, 255))),
new Rectangle(leftop, new Size(width, height)));
}
}
}
第一个方法是填充模拟数据。第二个才是绘制边框的,另如果你想填充背景色你只要把g.DrawRectangle方法改成FillRectangle就可以了
{
for (int i = 0; i < 16; i++)
{
ListViewItem item = new ListViewItem();
item.Name = item.Text = (i * 4).ToString();
item.SubItems.AddRange(new string[] {
(i * 4 + 1).ToString(), (i * 4 + 2).ToString(),
(i * 4 + 3).ToString()});
this.listView1.Items.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
Random ColorNo = new Random(0);
for (int i = 0; i < this.listView1.Items.Count; i++)
{
Point leftop = this.listView1.Items[i].SubItems[0].Bounds.Location;//获取左上角点
int height = this.listView1.Items[i].SubItems[0].Bounds.Height;//获取表格高度
int width = this.listView1.Columns[0].Width;//取列度为表格的宽度
using (Graphics g = this.listView1.CreateGraphics())
{
g.DrawRectangle(new Pen(Color.FromArgb(ColorNo.Next(0, 255), ColorNo.Next(0, 255), ColorNo.Next(0, 255))),
new Rectangle(leftop, new Size(width, height)));
}
}
}
第一个方法是填充模拟数据。第二个才是绘制边框的,另如果你想填充背景色你只要把g.DrawRectangle方法改成FillRectangle就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询