.net自带的Menu菜单实在不好看。
MagicLibrary此控件功能很强大,贴个下拉菜单的此控件用法。当然,此控件不仅仅只有这个用法,还有很多功能。比如说像VS.net这样的窗口控制、TabControl控件等等。
现举个例,用MagicLibrary做下拉菜单:
部份效果截图:
(如图一)
(如图二)
源码下载:/Files/HD/MagicLibrary_Demo/MenuDemo.rar
首先创建两个窗体Form1和Form2,再在主窗体Form1中加入ImageList控件。把所要的小图标加入到控件中。
以下为例子源码部份:(Form1窗体源码)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
protected System.Windows.Forms.ImageList _images;
private Crownwood.Magic.Menus.MenuControl _topMenu = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.SetupMenu();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected void SetupMenu()
{
this._topMenu = new Crownwood.Magic.Menus.MenuControl();
this._topMenu.MdiContainer = this;
//建立菜单
Crownwood.Magic.Menus.MenuCommand top1 = new Crownwood.Magic.Menus.MenuCommand( "&File" );
//加入子菜单
this.CreateFileMenu( top1 );
Crownwood.Magic.Menus.MenuCommand top2 = new Crownwood.Magic.Menus.MenuCommand( "&View" );
//加入子菜单
this.CreateViewMenu( top2 );
this._topMenu.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top1,top2 } );
//控件置顶
_topMenu.Dock = DockStyle.Top;
//菜单样式
this._topMenu.Style = Crownwood.Magic.Common.VisualStyle.IDE;
//载入控件
Controls.Add( _topMenu );
}
/// <summary>
/// File子菜单
/// </summary>
/// <param name="mc"></param>
protected void CreateFileMenu( Crownwood.Magic.Menus.MenuCommand mc )
{
Crownwood.Magic.Menus.MenuCommand top1_0 = new Crownwood.Magic.Menus.MenuCommand( "&New window",this._images,0,new EventHandler(OnNewWindowSelected) );
Crownwood.Magic.Menus.MenuCommand top1_1 = new Crownwood.Magic.Menus.MenuCommand( "&Open",new EventHandler(OnOpenSelected) );
Crownwood.Magic.Menus.MenuCommand top1_2 = new Crownwood.Magic.Menus.MenuCommand( "&Save",this._images,1,new EventHandler(OnSaveSelected) );
Crownwood.Magic.Menus.MenuCommand top1_3 = new Crownwood.Magic.Menus.MenuCommand( "&Exit",new EventHandler(OnExitSelected) );
mc.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top1_0,top1_1,top1_2,top1_3 } );
}
/// <summary>
/// View菜单
/// </summary>
/// <param name="mc"></param>
protected void CreateViewMenu( Crownwood.Magic.Menus.MenuCommand mc )
{
//下拉主菜单
Crownwood.Magic.Menus.MenuCommand top2_1 = new Crownwood.Magic.Menus.MenuCommand( "&Search",this._images,2 );
Crownwood.Magic.Menus.MenuCommand top2_2 = new Crownwood.Magic.Menus.MenuCommand( "&Replace" );
Crownwood.Magic.Menus.MenuCommand top2_3 = new Crownwood.Magic.Menus.MenuCommand( "&Cut" );
Crownwood.Magic.Menus.MenuCommand top2_4 = new Crownwood.Magic.Menus.MenuCommand( "&Repair" );
Crownwood.Magic.Menus.MenuCommand top2_5 = new Crownwood.Magic.Menus.MenuCommand( "&Edit" );
//下拉子菜单
Crownwood.Magic.Menus.MenuCommand top2_1_1 = new Crownwood.Magic.Menus.MenuCommand( "&Select search" );
Crownwood.Magic.Menus.MenuCommand top2_1_2 = new Crownwood.Magic.Menus.MenuCommand( "&Check search" );
top2_1.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top2_1_1,top2_1_2 } );
//下拉主菜单属性
top2_2.Enabled = false;
top2_4.Infrequent = true;
top2_5.Infrequent = true;
//下拉子菜单属性
top2_1_1.Checked = true;
//下拉菜单左导航
mc.MenuCommands.ExtraText = "View edit";
mc.MenuCommands.ExtraTextColor = Color.White;
mc.MenuCommands.ExtraBackColor = Color.DarkBlue;
mc.MenuCommands.ExtraFont = new Font("Times New Roman", 12f, FontStyle.Bold | FontStyle.Italic);
mc.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top2_1,top2_2,top2_3,top2_4,top2_5 } );
}
/// <summary>
/// 添加New window选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnNewWindowSelected( object sender, EventArgs e )
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
Form2 f2 = this.ActiveMdiChild as Form2;
if ( f2 != null )
{
f2.Focus();
}
else
{
f2 = new Form2();
f2.MdiParent = this;
f2.Show();
}
}
/// <summary>
/// 添加Open选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnOpenSelected( object sender, EventArgs e )
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog( this );
}
/// <summary>
/// 添加Save选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnSaveSelected( object sender, EventArgs e )
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog();
sfd.ShowDialog( this );
}
/// <summary>
/// 添加Exit选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnExitSelected( object sender, EventArgs e )
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
DialogResult dr = MessageBox.Show( "Are you sure exit application now?","Warning!",MessageBoxButtons.YesNo,MessageBoxIcon.Warning );
if ( dr == DialogResult.Yes )
{
Application.Exit();
}
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
protected System.Windows.Forms.ImageList _images;
private Crownwood.Magic.Menus.MenuControl _topMenu = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.SetupMenu();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected void SetupMenu()
{
this._topMenu = new Crownwood.Magic.Menus.MenuControl();
this._topMenu.MdiContainer = this;
//建立菜单
Crownwood.Magic.Menus.MenuCommand top1 = new Crownwood.Magic.Menus.MenuCommand( "&File" );
//加入子菜单
this.CreateFileMenu( top1 );
Crownwood.Magic.Menus.MenuCommand top2 = new Crownwood.Magic.Menus.MenuCommand( "&View" );
//加入子菜单
this.CreateViewMenu( top2 );
this._topMenu.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top1,top2 } );
//控件置顶
_topMenu.Dock = DockStyle.Top;
//菜单样式
this._topMenu.Style = Crownwood.Magic.Common.VisualStyle.IDE;
//载入控件
Controls.Add( _topMenu );
}
/// <summary>
/// File子菜单
/// </summary>
/// <param name="mc"></param>
protected void CreateFileMenu( Crownwood.Magic.Menus.MenuCommand mc )
{
Crownwood.Magic.Menus.MenuCommand top1_0 = new Crownwood.Magic.Menus.MenuCommand( "&New window",this._images,0,new EventHandler(OnNewWindowSelected) );
Crownwood.Magic.Menus.MenuCommand top1_1 = new Crownwood.Magic.Menus.MenuCommand( "&Open",new EventHandler(OnOpenSelected) );
Crownwood.Magic.Menus.MenuCommand top1_2 = new Crownwood.Magic.Menus.MenuCommand( "&Save",this._images,1,new EventHandler(OnSaveSelected) );
Crownwood.Magic.Menus.MenuCommand top1_3 = new Crownwood.Magic.Menus.MenuCommand( "&Exit",new EventHandler(OnExitSelected) );
mc.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top1_0,top1_1,top1_2,top1_3 } );
}
/// <summary>
/// View菜单
/// </summary>
/// <param name="mc"></param>
protected void CreateViewMenu( Crownwood.Magic.Menus.MenuCommand mc )
{
//下拉主菜单
Crownwood.Magic.Menus.MenuCommand top2_1 = new Crownwood.Magic.Menus.MenuCommand( "&Search",this._images,2 );
Crownwood.Magic.Menus.MenuCommand top2_2 = new Crownwood.Magic.Menus.MenuCommand( "&Replace" );
Crownwood.Magic.Menus.MenuCommand top2_3 = new Crownwood.Magic.Menus.MenuCommand( "&Cut" );
Crownwood.Magic.Menus.MenuCommand top2_4 = new Crownwood.Magic.Menus.MenuCommand( "&Repair" );
Crownwood.Magic.Menus.MenuCommand top2_5 = new Crownwood.Magic.Menus.MenuCommand( "&Edit" );
//下拉子菜单
Crownwood.Magic.Menus.MenuCommand top2_1_1 = new Crownwood.Magic.Menus.MenuCommand( "&Select search" );
Crownwood.Magic.Menus.MenuCommand top2_1_2 = new Crownwood.Magic.Menus.MenuCommand( "&Check search" );
top2_1.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top2_1_1,top2_1_2 } );
//下拉主菜单属性
top2_2.Enabled = false;
top2_4.Infrequent = true;
top2_5.Infrequent = true;
//下拉子菜单属性
top2_1_1.Checked = true;
//下拉菜单左导航
mc.MenuCommands.ExtraText = "View edit";
mc.MenuCommands.ExtraTextColor = Color.White;
mc.MenuCommands.ExtraBackColor = Color.DarkBlue;
mc.MenuCommands.ExtraFont = new Font("Times New Roman", 12f, FontStyle.Bold | FontStyle.Italic);
mc.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top2_1,top2_2,top2_3,top2_4,top2_5 } );
}
/// <summary>
/// 添加New window选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnNewWindowSelected( object sender, EventArgs e )
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
Form2 f2 = this.ActiveMdiChild as Form2;
if ( f2 != null )
{
f2.Focus();
}
else
{
f2 = new Form2();
f2.MdiParent = this;
f2.Show();
}
}
/// <summary>
/// 添加Open选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnOpenSelected( object sender, EventArgs e )
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog( this );
}
/// <summary>
/// 添加Save选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnSaveSelected( object sender, EventArgs e )
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog();
sfd.ShowDialog( this );
}
/// <summary>
/// 添加Exit选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnExitSelected( object sender, EventArgs e )
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
DialogResult dr = MessageBox.Show( "Are you sure exit application now?","Warning!",MessageBoxButtons.YesNo,MessageBoxIcon.Warning );
if ( dr == DialogResult.Yes )
{
Application.Exit();
}
}
}
}