Hi Peter,
The default font color of the group headers in a listview is black. There's
no property or method in the ListView or ListViewGroup classes for us to
change the font color of the group headers. Handling the DrawItem event of
the listview is no use, because we could only redraw items but not group
headers by this means.
However, we could call the SendMessage Win32 API to send a
LVM_SETGROUPMETRICS message the listview control to sepcify what color the
group header is. The following is a sample.
using System.Runtime.InteropServices;
public class ListViewAPI
{
#region Constants for Listview-Messages
public const int LVM_FIRST = 4096;
public const int LVM_SETGROUPMETRICS = (LVM_FIRST + 155);
#endregion
#region Constants for LVGROUPMETRICS.mask
public const int LVGMF_NONE = 0;
public const int LVGMF_BORDERSIZE = 1;
public const int LVGMF_BORDERCOLOR = 2;
public const int LVGMF_TEXTCOLOR = 4;
#endregion
[StructLayout(LayoutKind.Sequential)]
public struct LVGROUPMETRICS
{
public int cbSize;
public int mask;
public int Left;
public int Top;
public int Right;
public int Bottom;
public int crLeft;
public int crTop;
public int crRight;
public int crBottom;
public int crHeader;
public int crFooter;
}
[DllImport("User32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int
wParam, ref LVGROUPMETRICS lParam);
public static void SetGroupHeaderColor(IntPtr handle, int color)
{
ListViewAPI.LVGROUPMETRICS groupMetrics;
groupMetrics = new ListViewAPI.LVGROUPMETRICS();
groupMetrics.cbSize =
Marshal.SizeOf(typeof(ListViewAPI.LVGROUPMETRICS));
// set the mask to LVGMF_TEXTCOLOR to enable the new header
color to take effect
groupMetrics.mask = ListViewAPI.LVGMF_TEXTCOLOR;
groupMetrics.crHeader = color;
int ptrRetVal = (int)SendMessage(handle,
ListViewAPI.LVM_SETGROUPMETRICS, 0, ref groupMetrics);
}
}
private void Form1_Load(object sender, EventArgs e)
{
ListViewAPI.SetGroupHeaderColor(this.listView1.Handle,0x0000ff00);
}
Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Peter Larsen [] - 25 Sep 2006 08:05 GMT
Hi Linda,
This works just fine.
Thank you.
BR
Peter