【问题标题】:.NET ListView and Windows 7.NET ListView 和 Windows 7
【发布时间】:2010-12-28 12:22:01
【问题描述】:

也许我错过了什么,但是... Windows 7 中的 ListView 控件在所选项目周围显示一个突出显示,看起来像一个 3D 蓝色半透明矩形(我不是在谈论选择矩形,而是在实际所选项目周围的矩形)。将鼠标悬停在项目上时,它甚至会显示一个较浅的矩形。

但是,当我在 WinForms 中使用 ListView 时(即使是双缓冲的),所选项目只有纯蓝色背景(并且没有悬停背景),这看起来比资源管理器中的列表更不专业。

有谁知道我应该调用什么秘密 API 函数来使 .NET ListView 看起来与操作系统的其余部分保持一致?

例如,这是我用 C++ 编写的应用程序之一,使用 Windows 7 中的标准 ListView 控件:(注意突出显示和悬停矩形)

这里是用 WinForms 用 C# 重写的那个应用程序:(注意粗略的高亮和没有悬停)

【问题讨论】:

    标签: winforms listview windows-7


    【解决方案1】:

    好的,我完全明白了,这可能会帮助其他被此问题困扰的人。

    我首先注意到 C++Builder 中的 ListView 控件在 Windows 7 下看起来“正确”,所以我查看了 VCL 的源代码,看看他们正在做什么样的魔法来使 ListView 看起来像Windows 资源管理器中的列表控件。我偶然发现了一行看起来很有希望的代码:

    SetWindowTheme(Handle, 'explorer', nil);
    

    根据 SDK 文档,此函数“导致窗口使用与其类通常使用的不同的视觉样式信息集。”

    所以,我尝试在我的 WinForms ListView 控件上调用此函数:

    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
    public static extern int SetWindowTheme(IntPtr hWnd, String pszSubAppName, String pszSubIdList);
    
    
    SetWindowTheme(myListView.Handle, "explorer", null);
    

    ...而且,老天,它奏效了! ListView 终于看起来像是属于操作系统的其余部分了!谢谢,Borland Inprise Embarcadero!你真的很擅长某事!

    【讨论】:

      【解决方案2】:

      编辑:现在它也对我有用,确切的签名是:

       <DllImport("uxtheme.dll",
        BestFitMapping:=False,
        CharSet:=CharSet.Unicode,
        EntryPoint:="#136",
        CallingConvention:=CallingConvention.Winapi)>
        Private Shared Function SetWindowsTheme(ByVal handle As IntPtr, ByVal app As String, ByVal id As String) As Integer
              ' Leave function empty - DLLImport attribute forwards calls to the right function 
          End Function
      
      
      Public Shared Sub MakeControlLookBeautiful(ByVal c As Windows.Forms.Control)
          SetWindowsTheme(c.Handle, "explorer", Nothing)
      End Sub
      

      :)

      【讨论】:

        【解决方案3】:
        Imports System.Runtime.InteropServices
        
        Public Class Form1
            <DllImport("uxtheme", CharSet:=CharSet.Unicode)> _
            Public Shared Function SetWindowTheme(ByVal hWnd As IntPtr, ByVal textSubAppName As String, ByVal textSubIdList As String) As Integer
            End Function
        
            Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                SetWindowTheme(lst.Handle, "explorer", Nothing)
            End Sub
        End Class
        

        上面的代码将像冠军一样工作......

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-09
          • 2023-04-09
          • 2013-11-11
          • 1970-01-01
          • 1970-01-01
          • 2011-04-25
          • 2010-12-22
          • 1970-01-01
          相关资源
          最近更新 更多