Windows主题的ListView影响头 [英] Windows theme affecting ListView header

查看:193
本文介绍了Windows主题的ListView影响头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建新的Windows窗体包含ListView的一个简单的表格应用程序(C#)。然后,我已经改变了查看物业为详细信息并增加在这ListView的使用,这里的字体大小是结果:

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:

这是它的外观在Windows XP与Windows经典主题:结果

This is how it looks on Windows XP with Windows Classic theme:

和这里是与Windows XP主题的结果:结果

and here's the result with Windows XP theme:

我可以prevent我的应用程序的外观由视觉样式通过删除<一个受到影响href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.application.enablevisualstyles.aspx\"相对=nofollow> Application.EnableVisualStyles() 致电或通过改变
<一href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.visualstyles.visualstylestate.aspx\"相对=nofollow> Application.VisualStyleState
结果
虽然这种变化使在ListView具有所需的外观,它也影响其他控件的外观。我想我的ListView为未受到视觉样式的唯一控制

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?

不幸的是,没有提及解决方案的作品。 它看起来像自己会作出一些由视觉样式甚至当视觉样式的ListView控件被禁用受影响的控件的标题。

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.

的任何C#解决方案,prevent视觉样式从影响的ListView头的外观将AP preciated。

推荐答案

用尽研究之后,我发现它。问题是,当你调用

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

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

自定义在的ListView 类,它prevents从影响的ListView 控制appearence视觉样式,但不是的ListView 头控制( SysHeader32 窗口),这是的ListView 所以,当调用 SetWindowTheme 函数,我们需要提供标题窗口,而不是ListView控件的句柄的句柄:

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

这篇关于Windows主题的ListView影响头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆