Windows theme affecting ListView header

c#, listview, visual-c++, windows-xp, winforms

Solution

After exhausting research, I found it out. The thing is that when you call

SetWindowTheme(this.Handle, "", "");

within custom `ListView` class, it prevents visual styles from affecting the appearence of the `ListView` control but not the `ListView` header control (`SysHeader32` window), which is child window of `ListView`. So when calling the `SetWindowTheme` function, we need to provide the handle of the header window instead of the handle of the ListView:

[DllImportAttribute("uxtheme.dll")]
private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);

[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

// Callback method to be used when enumerating windows:
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
    GCHandle gch = GCHandle.FromIntPtr(pointer);
    List<IntPtr> list = gch.Target as List<IntPtr>;
    if (list == null)
        throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
    list.Add(handle);
    return true;
}

// delegate for the EnumChildWindows method:
private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

// get first child:
private static void DisableVisualStylesForFirstChild(IntPtr parent)
{
    List<IntPtr> children = new List<IntPtr>();
    GCHandle listHandle = GCHandle.Alloc(children);
    try
    {
        EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
        EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        if (children.Count > 0)
            SetWindowTheme(children[0], "", "");
    }
    finally
    {
        if (listHandle.IsAllocated)
            listHandle.Free();
    }
}

protected override void OnHandleCreated(EventArgs e)
{
    DisableVisualStylesForFirstChild(this.Handle);
    base.OnHandleCreated(e);
}

Problem

I've created new Windows Forms Application (C#) with one simple form containing ListView. Then I have changed the View Property to Details and increased the size of the font used in this ListView and here's the result: This is how it looks on Windows XP with Windows Classic theme: and here's the result with Windows XP theme: I can prevent the appearance of my application to be affected by Visual Styles either by removing `Application.EnableVisualStyles()` call or by changing the `Application.VisualStyleState`: Although this change makes the ListView to have the desired appearance, it also affects the appearance of other controls. I'd like my ListView to be the only control that is not affected by Visual Styles. I've also found similar questions that try to deal with it: Can you turn off visual styles/theming for just a single windows control? How do I disable visual styles for just one control, and not its children? Unfortunately, none of mentioned solutions works. It looks like the header itself would be made up of some controls that are affected by visual styles even when visual styles for ListView control are disabled. Any C# solution that would prevent visual styles from affecting the appearance of the ListView header would be appreciated.

Original source

Related problems