Well i have a windows forms application in which i add a couple of listViews in order to hold some data for the user and it looks like this
As you see my form backcolor is black so the list view's grid lines and header white color makes an annoying contrast so after an hour searching without a luck i decided to ask here.
[Question] : How could i edit the colors of the Header & Grid Lines of the list view to match my needs ?
It looks like that there is not anyone who's interested in customizing a ListView
to support Grid Line Color
. I've tried this one and want to share here. It's not really good by a little flicker (not really much) when you scroll the ListView items. However it's acceptable. I think I lack some knowledge of win32
here to make it more perfect:
public class CustomListView : ListView {
bool scrollDown;
int lastScroll;
public Color GridLinesColor {get;set;}
[DllImport("user32")]
private static extern int GetScrollPos(IntPtr hwnd, int nBar);
public CustomListView(){
GridLinesColor = Color.Red;
DoubleBuffered = true;
base.GridLines = false;//We should prevent the default drawing of gridlines.
}
public new bool GridLines {get;set;}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x20a){//WM_MOUSEWHEEL = 0x20a
scrollDown = (m.WParam.ToInt64() >> 16) < 0;
}
if (m.Msg == 0x115){//WM_VSCROLL = 0x115
int n = (m.WParam.ToInt32() >> 16);
scrollDown = n > lastScroll;
lastScroll = n;
}
base.WndProc(ref m);
if (m.Msg == 0xf && GridLines && Items.Count > 0&&View==View.Details)//WM_PAINT = 0xf
{
using (Graphics g = CreateGraphics())
{
using(Pen p = new Pen(GridLinesColor)){
int w = -GetScrollPos(Handle, 0);
for (int i = 0; i < Columns.Count; i++)
{
w += Columns[i].Width;
g.DrawLine(p, new Point(w, 0), new Point(w, ClientSize.Height));
}
int a = Items[0].Bounds.Bottom - 1;
int b = Height - Items[0].Bounds.Y;
int c = Items[0].Bounds.Height;
for (int i = scrollDown ? a + (b/c) * c : a ; scrollDown ? i >= a : i < b ; i += scrollDown ? -c : c)
{
g.DrawLine(p, new Point(0, i), new Point(ClientSize.Width, i));
}
}
}
}
}
}
UPDATE: Thanks to suggestion of Cody Gray, I added code to handle horizontal scrolling. I use GetScrollPos
for simplicity because as recommended by MSDN documentation page, we should use GetScrollInfo
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With