Change background selection color of ListView?

佐手、 提交于 2019-12-05 07:54:17
Grammarian

ObjectListView -- a wrapper around a WinForm ListView -- has properties to let you control the background and foreground color of the selected rows. It uses the technique that Obalix suggested, but it has already done the hard work for you.

So, with a little effort, you can produce something like this:

The "Feel Good Inc" row show a custom foreground and background for selection.

If you wanted your ListView to have the style of the Windows Explorer ListView (including the nice appearance with rounded edges in Win7/Vista), you could use a little P/Invoke to accomplish that:

[DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList);

// You can subclass ListView and override this method
protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    SetWindowTheme(this.Handle, "explorer", null);
}

Well for WinForms you have to set the OwnerDraw property to true and then use the DrawItem and DrawSubItem events to draw the item manually.

See here for an example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!