在 ListView 中更改列 Header 的字体样式和颜色
Changing Font Style and Color for Column Header in ListView
我搜索了为我们使用的 ListView 更改 Header 颜色的内容:
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
e.DrawText();
}
并且我们使用相同的事件来更改 Header 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);
}
}
}
现在我的问题是我想同时更改 Header 颜色和 Header 样式。所以我这样写:
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 会变成粗体,但是 Header 颜色不会改变。要同时更改它们(即 Header 颜色和 Header 样式),我缺少什么?没看懂。
试试这个,即跳过对 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);
}
}
}
我搜索了为我们使用的 ListView 更改 Header 颜色的内容:
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
e.DrawText();
}
并且我们使用相同的事件来更改 Header 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);
}
}
}
现在我的问题是我想同时更改 Header 颜色和 Header 样式。所以我这样写:
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 会变成粗体,但是 Header 颜色不会改变。要同时更改它们(即 Header 颜色和 Header 样式),我缺少什么?没看懂。
试试这个,即跳过对 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);
}
}
}