如何为 .NET TreeView 获得 Windows 本机外观?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5131534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Windows native look for the .NET TreeView?
提问by asmo
When using the TreeView component in .NET, I get the look of the left tree. How can I get the look of the right tree (Windows Native Look) for my .NET TreeView?
在 .NET 中使用 TreeView 组件时,我看到了左侧树的外观。如何为我的 .NET TreeView 获得正确的树(Windows Native Look)的外观?
What I especially want to get is the "triangle" node handles and the blue "bubble" selection square.
我特别想得到的是“三角形”节点句柄和蓝色的“气泡”选择方块。
回答by David Heffernan
You need to P/Invoke to call SetWindowTheme
passing the window handle of the tree and use "explorer" as the theme.
您需要P/Invoke 调用SetWindowTheme
传递树的窗口句柄并使用“资源管理器”作为主题。
Paste the following code into a new class in your project, compile, and use this custom control instead of the built-in TreeView
control.
将以下代码粘贴到项目中的新类中,编译并使用此自定义控件而不是内置TreeView
控件。
C#:
C#:
public class NativeTreeView : System.Windows.Forms.TreeView
{
[DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
private extern static int SetWindowTheme(IntPtr hWnd, string pszSubAppName,
string pszSubIdList);
protected override void CreateHandle()
{
base.CreateHandle();
SetWindowTheme(this.Handle, "explorer", null);
}
}
VB.NET:
VB.NET:
Public Class NativeTreeView : Inherits TreeView
Private Declare Unicode Function SetWindowTheme Lib "uxtheme.dll"
(hWnd As IntPtr, pszSubAppName As String, pszSubIdList As String) As Integer
Protected Overrides Sub CreateHandle()
MyBase.CreateHandle()
SetWindowTheme(Me.Handle, "Explorer", Nothing)
End Sub
End Class
Note that this trick also works exactly the same way for the ListView
control.
请注意,此技巧对ListView
控件的工作方式也完全相同。