Change font style and color for column header in ListView
I was looking for what we use to change the title color for the ListView:
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
e.DrawText();
}
And we use the same event to change the header style for the 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);
}
}
}
Now my problem is that I want to change the color of the title as well as the style of the title. So I wrote it like this:
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);
}
}
}
But if I execute this code, the title changes to Bold, but the title color does not change. To change them (i.e., title and title style), what am I missing? I do not understand.
+3
source to share
1 answer
Try this, for example, skip the call e.DrawText()
and 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);
}
}
}
+5
source to share