Home > Net >  Retrieve border color of WinForms ListBox/ListView rendered with OS visual styles
Retrieve border color of WinForms ListBox/ListView rendered with OS visual styles

Time:10-07

If we create a new WinForms .NET project and place a ListBox or ListView control on it, by default they are rendered using the OS visual styles:

enter image description here

I need to draw a 1-pixel border similar to what the system provides for ListBox/ListView in my custom control. Is there a way to retrieve the color of that system border for ListBox/ListView using a system call?

CodePudding user response:

A quick answer for C#/WinForms:

var rend = new VisualStyleRenderer("ListBox", 0, 0);
Color borderColor = rend.GetColor(ColorProperty.BorderColor);

You can also retrieve the same value with Windows API using the [GetThemeColor}(https://docs.microsoft.com/en-us/windows/win32/api/uxtheme/nf-uxtheme-getthemecolor) function with the property identifier 3801:

enter image description here

However, in the general case the border may consist of different color values and/or be blended with the parent's background, so that the retrieved color value may be not exactly what you expect.

  • Related