Windows theme affecting ListView header

江枫思渺然 提交于 2020-01-24 02:22:04

问题


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.


回答1:


Looks like this is a known bug for which there is no easy workaround. According to the answer there:

After doing a lot of research with this issue, we find that this is a windows OS bug, which the ComCtl6.0 header control forgets to apply the font information to the drawing DC.

However you could draw the column header yourself. See this article on MSDN for details on how to do it and also look at listView1_DrawColumnHeader.




回答2:


What about disable visual styles ?

with this code you could disable style for one control (just use ListViewConstrol instead of ListView):

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class ListViewControl : ListView {
  [DllImportAttribute("uxtheme.dll")]
  private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);

  protected override void OnHandleCreated(EventArgs e) {
    SetWindowTheme(this.Handle, "", "");
    base.OnHandleCreated(e);
  }
}



回答3:


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);
}



回答4:


As Botz3000 mentioned in his answer that its a well known problem with ListView in Windows XP. Another workaround is to register ListView.DrawColumnHeader event and reset the header font in it. You must set ListView.OwnerDraw property to true. MSDN code is as follows;

// Draws column headers.
private void listView1_DrawColumnHeader(object sender,
    DrawListViewColumnHeaderEventArgs e)
{
    using (StringFormat sf = new StringFormat())
    {
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        switch (e.Header.TextAlign)
        {
            case HorizontalAlignment.Center:
                sf.Alignment = StringAlignment.Center;
                break;
            case HorizontalAlignment.Right:
                sf.Alignment = StringAlignment.Far;
                break;
        }

        // Draw the standard header background.
        e.DrawBackground();

        // Draw the header text.
        using (Font headerFont =
                    new Font("Helvetica", 10, FontStyle.Bold))
        {
            e.Graphics.DrawString(e.Header.Text, headerFont,
                Brushes.Black, e.Bounds, sf);
        }
    }
    return;
}

In this case the header font will always be "Helvetica", 10, FontStyle.Bold and will not be effected by listview font. Check MSDN for further details.




回答5:


Here's how to make the ListView header have the height you want:

C# - Listview colum header height (Windows Form)



来源:https://stackoverflow.com/questions/10103609/windows-theme-affecting-listview-header

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