.net自带的Menu菜单实在不好看。
MagicLibrary此控件功能很强大,贴个下拉菜单的此控件用法。当然,此控件不仅仅只有这个用法,还有很多功能。比如说像VS.net这样的窗口控制、TabControl控件等等。
.net自带的Menu菜单实在不好看。
MagicLibrary此控件功能很强大,贴个下拉菜单的此控件用法。当然,此控件不仅仅只有这个用法,还有很多功能。比如说像VS.net这样的窗口控制、TabControl控件等等。
现举个例,用MagicLibrary做下拉菜单:
部份效果截图:
(如图一)
![](/images/cnblogs_com/hd/DemoOpus/Image_1.jpg)
(如图二)
![](/images/cnblogs_com/hd/DemoOpus/Image_2.jpg)
源码下载:/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;
![](/Images/OutliningIndicators/None.gif)
namespace WindowsApplication1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.ComponentModel.IContainer components;
protected System.Windows.Forms.ImageList _images;
private Crownwood.Magic.Menus.MenuControl _topMenu = null;
![](/Images/OutliningIndicators/InBlock.gif)
public Form1()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
![](/Images/OutliningIndicators/InBlock.gif)
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.SetupMenu();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this._images = new System.Windows.Forms.ImageList(this.components);
//
// _images
//
this._images.ImageSize = new System.Drawing.Size(16, 16);
this._images.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("_images.ImageStream")));
this._images.TransparentColor = System.Drawing.Color.Transparent;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 333);
this.IsMdiContainer = true;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void SetupMenu()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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 );
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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 );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// File子菜单
/// </summary>
/// <param name="mc"></param>
protected void CreateFileMenu( Crownwood.Magic.Menus.MenuCommand mc )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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) );
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
mc.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]
{ top1_0,top1_1,top1_2,top1_3 } );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// View菜单
/// </summary>
/// <param name="mc"></param>
protected void CreateViewMenu( Crownwood.Magic.Menus.MenuCommand mc )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//下拉主菜单
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" );
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
top2_1.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]
{ top2_1_1,top2_1_2 } );
![](/Images/OutliningIndicators/InBlock.gif)
//下拉主菜单属性
top2_2.Enabled = false;
top2_4.Infrequent = true;
top2_5.Infrequent = true;
![](/Images/OutliningIndicators/InBlock.gif)
//下拉子菜单属性
top2_1_1.Checked = true;
![](/Images/OutliningIndicators/InBlock.gif)
//下拉菜单左导航
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);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
mc.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]
{ top2_1,top2_2,top2_3,top2_4,top2_5 } );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 添加New window选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnNewWindowSelected( object sender, EventArgs e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
Form2 f2 = this.ActiveMdiChild as Form2;
if ( f2 != null )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
f2.Focus();
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
f2 = new Form2();
f2.MdiParent = this;
f2.Show();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 添加Open选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnOpenSelected( object sender, EventArgs e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog( this );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 添加Save选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnSaveSelected( object sender, EventArgs e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Crownwood.Magic.Menus.MenuCommand mc = (Crownwood.Magic.Menus.MenuCommand)sender;
System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog();
sfd.ShowDialog( this );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 添加Exit选项目事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnExitSelected( object sender, EventArgs e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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 )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Exit();
}
}
}
}
![](/Images/OutliningIndicators/None.gif)