.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 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { 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; } #endregion /**//// <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(); } } } }
(二)(TabControl控件)
MagicLibrary.dll控件功能中昨天写了个Menu的实现,全部以代码方式实现的一个例子。
在MagicLibrary.dll中,还一TabControl控件。
今天以拖拉方式(工具方法拖出)实现TabControl,其中加有Menu例子。
例子源码下载:/Files/HD/MagicLibrary_Demo/TabControlDemo.rar
截图如下:
例图一:
例图二:加入TabPage
例图三:关才已选择的TabPage
例图四:在已选择的TabControl中的RichTextBox控件右键菜单
例图五:TabControl控件的PositionTop属性设为flase
操作示例:
(界面设计)
项目中加入MagicLibrary.dll(包括解决方案资源管理器、工具箱中)。
项目中包括两个Form,分别为:Form1、Form2,Form1为起始Form。
打开Form1,在窗口设计器中从工具箱中拖入刚加入的控件:MenuControl,把MenuControl的Dock属性设为:Top,Name属性为:_topMenu,PositionTop属性设为:true,ShowArrows属性设为:true,ShowClose属性设为:true,ShrinkPagesToFit属性设为:false。接着再从工具箱中拖入:TabControl,把TabControl控件的Dock属性设为:Fill,Name属性为:_tabControl。
打开Form2,在窗口设计器中从工具箱中拖入控件:RichTextBox,把RichTextBox的Dock属性设为:Fill,Name属性设为:rtxContent
一:Form1
(代码实现)
打开Form1的代码设计器,在Form1的构造函数中加入:this.SetupMenu();
见源码:
操作示例:(界面设计)项目中加入MagicLibrary.dll(包括解决方案资源管理器、工具箱中)。
项目中包括两个Form,分别为:Form1、Form2,Form1为起始Form。打开Form1,在窗口设计器中从工具箱中拖入刚
加入的控件:MenuControl,把MenuControl的Dock属性设为:Top,Name属性为:_topMenu,PositionTop属性设为:true,
ShowArrows属性设为:true,ShowClose属性设为:true,ShrinkPagesToFit属性设为:false。
接着再从工具箱中拖入:TabControl,把TabControl控件的Dock属性设为:Fill,Name属性为:_tabControl。
打开Form2,在窗口设计器中从工具箱中拖入控件:RichTextBox,把RichTextBox的Dock属性设为:
Fill,Name属性设为:rtxContent 一:Form1 (代码实现)打开Form1的代码设计器,在Form1的构造函数中加入:this.SetupMenu();
见源码: public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); //加入菜单 this.SetupMenu(); }
接着复制这段代码到设计器中: /**//// <summary> /// 加入下拉菜单 /// </summary>
protected void SetupMenu() { Crownwood.Magic.Menus.MenuCommand top1 = new Crownwood.Magic.Menus.MenuCommand( "&PageControls" );
Crownwood.Magic.Menus.MenuCommand top1_1 = new Crownwood.Magic.Menus.MenuCommand( "&AddPage",new EventHandler( OnAddPageSelected ) );
Crownwood.Magic.Menus.MenuCommand top1_2 = new Crownwood.Magic.Menus.MenuCommand( "&RemovePage",new EventHandler( OnColseSelected ) );
Crownwood.Magic.Menus.MenuCommand top1_3 = new Crownwood.Magic.Menus.MenuCommand( "&Exit",new EventHandler( OnExitSelected ) );
top1.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top1_1,top1_2,top1_3 } );
Crownwood.Magic.Menus.MenuCommand top2 = new Crownwood.Magic.Menus.MenuCommand( "&TabControl" );
Crownwood.Magic.Menus.MenuCommand top2_1 = new Crownwood.Magic.Menus.MenuCommand( "&Top",new EventHandler( OnTopSelected ) );
Crownwood.Magic.Menus.MenuCommand top2_2 = new Crownwood.Magic.Menus.MenuCommand( "&Buttom",new EventHandler( OnButtomSelected ) );
top2.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top2_1,top2_2 } );
this._topMenu.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ top1,top2 } );
this._tabControl.MouseUp += new MouseEventHandler( OnMouseUpTabPage ); } /**//// <summary> /// 加入页面 /// </summary>
protected void AddPage() { Form2 f2 = new Form2(); System.Windows.Forms.Control ctlForm = f2;
Crownwood.Magic.Controls.TabPage tp = new Crownwood.Magic.Controls.TabPage( "Page"+this._tabControl.TabPages.Count.ToString(),ctlForm );
this._tabControl.TabPages.Add( tp ).Selected = true; }
protected void OnExitSelected( object sender , EventArgs e )
{ DialogResult dr = MessageBox.Show( "Are you sure exit application?","Warning",MessageBoxButtons.YesNo,MessageBoxIcon.Warning );
if ( dr == DialogResult.Yes ) { Application.Exit(); } } protected void OnTopSelected( object sender,EventArgs e ) { this._tabControl.PositionTop = true; }
protected void OnButtomSelected( object sender , EventArgs e ) { this._tabControl.PositionTop = false; }
protected void OnAddPageSelected( object sender , EventArgs e ) { this.AddPage(); }
protected void OnMouseUpTabPage( object sender , System.Windows.Forms.MouseEventArgs e )
{ if ( this._tabControl.TabPages.Count>0 && e.Button == MouseButtons.Right && this._tabControl.SelectedTab.Selected )
{ Crownwood.Magic.Menus.MenuControl muMenu = new Crownwood.Magic.Menus.MenuControl();
Crownwood.Magic.Menus.MenuCommand menu1 = new Crownwood.Magic.Menus.MenuCommand( "&SavePage" );
Crownwood.Magic.Menus.MenuCommand menu2 = new Crownwood.Magic.Menus.MenuCommand( "&Close",new EventHandler( OnColseSelected ) );
Crownwood.Magic.Menus.PopupMenu pm = new Crownwood.Magic.Menus.PopupMenu();
pm.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ menu1,menu2 } );
pm.TrackPopup( this._tabControl.PointToScreen( new Point( e.X,e.Y ) ) ); } }
protected void OnColseSelected( object sender , EventArgs e ) { if ( this._tabControl.TabPages.Count > 0 )
{ this._tabControl.TabPages.Remove( this._tabControl.SelectedTab ); } } (添加事件)打开Form1窗口设计器,选中TabControl控件,向控件加入MouseUp事件:
/**//// <summary> /// 单击TabControl关闭按钮时关闭已选择的TabPage /// </summary> /// <param name="sender"></param> ///
<param name="e"></param> private void _tabControl_ClosePressed(object sender, System.EventArgs e) { if ( this._tabControl.TabPages.Count > 0 )
{ this._tabControl.TabPages.Remove( this._tabControl.SelectedTab ); } } 二:Form2 打开Form2窗口设计器,选中RichTextBox,为RichTextBox控件添加MouseUp事件:
private void rtxContent_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if ( e.Button == MouseButtons.Right )
{ System.Windows.Forms.RichTextBox rt = ( RichTextBox )sender; Crownwood.Magic.Menus.MenuControl muMenu = new Crownwood.Magic.Menus.MenuControl();
Crownwood.Magic.Menus.MenuCommand menu1 = new Crownwood.Magic.Menus.MenuCommand( "&Copy" );
Crownwood.Magic.Menus.MenuCommand menu2 = new Crownwood.Magic.Menus.MenuCommand( "&Cut" );
Crownwood.Magic.Menus.PopupMenu pm = new Crownwood.Magic.Menus.PopupMenu();
pm.MenuCommands.AddRange( new Crownwood.Magic.Menus.MenuCommand[]{ menu1,menu2 } ); pm.TrackPopup( rt.PointToScreen( new Point( e.X,e.Y ) ) ); } }