我搜索了更改我们使用的ListView的标题颜色:
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
e.DrawText();
}
我们使用相同的事件来更改ListView的标题样式:
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
e.DrawBackground();
using (Font headerFont =
new Font("Microsoft Sans Serif", 9, FontStyle.Bold)) //Font size!!!!
{
e.Graphics.DrawString(e.Header.Text, headerFont,
Brushes.Black, e.Bounds, sf);
}
}
}
现在我的问题是我想要更改标题颜色和标题样式。所以我这样写:
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
e.DrawText();
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
e.DrawBackground();
using (Font headerFont =
new Font("Microsoft Sans Serif", 9, FontStyle.Bold)) //Font size!!!!
{
e.Graphics.DrawString(e.Header.Text, headerFont,
Brushes.Black, e.Bounds, sf);
}
}
}
但是如果我执行此代码,Header将更改为Bold,但Header颜色不会更改。要更改它们(即标题颜色和标题样式)我缺少什么?我不明白。
答案 0 :(得分:5)
尝试此操作,即跳过对e.DrawText()
和e.DrawBackground()
的调用:
private void list_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
using (var sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
using (var headerFont = new Font("Microsoft Sans Serif", 9, FontStyle.Bold))
{
e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
e.Graphics.DrawString(e.Header.Text, headerFont,
Brushes.Black, e.Bounds, sf);
}
}
}