Menu in Winforms resembling folder explorer menu
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 whic开发者_StackOverflow中文版h appear & disappear by clicking on the + & - symbols.
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.
You need to P/Invoke to call SetWindowTheme
passing the window handle of the tree and use "explorer" as the theme.
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);
}
}
Note that this trick also works exactly the same way for the ListView
control.
You can have multiple 'root' nodes in a WinForms treeview:
treeView.Nodes.Add("Root 1");
treeView.Nodes.Add("Root 2");
Instead of text above, they could be full nodes w/ children.
精彩评论