Winforms中的菜单类似于文件夹资源管理器菜单 [英] Menu in Winforms resembling folder explorer menu
问题描述
如何在winforms中创建菜单,该菜单与我们浏览任何文件夹时在Windows资源管理器中的左手框架上显示的菜单完全相似.菜单包含出现的树节点和根节点&通过单击+&消失-符号.
How to create a menu in winforms which totally resembles the menu which appears on Left Hand Frame in Windows Explorer when we Explore any folder. The menu contains tree nodes and root nodes which appear & disappear by clicking on the + & - symbols.
推荐答案
好吧,这不是菜单,而是树视图.您可以使用WinForms树形视图,但是开箱即用的外观与资源管理器树形视图并不完全一样.您需要应用资源管理器窗口主题.
Well, that's not a menu, it's a tree view. You can use the WinForms tree view, but out of the box it won't look exactly like the Explorer tree view. You need to apply the Explorer window theme.
您需要P/Invoke才能调用 SetWindowTheme
树的窗口句柄,并以资源管理器"为主题.
You need to P/Invoke to call SetWindowTheme
passing the window handle of the tree and use "explorer" as the theme.
将以下代码粘贴到项目中的新类中,进行编译,并使用此自定义控件代替内置的TreeView
控件.
Paste the following code into a new class in your project, compile, and use this custom control instead of the built-in TreeView
control.
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);
}
}
请注意,此技巧对ListView
控件也完全相同.
Note that this trick also works exactly the same way for the ListView
control.
这篇关于Winforms中的菜单类似于文件夹资源管理器菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!