⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitmapmenuitem.cs

📁 包括Pheromones Algorythm、Memory Algorythm和Hill Climbing Algorythm I
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Text;
using System.IO;

/// MenuItem Extensions ripped from a Dr Gui VB article
namespace MenuItemExtension
{
	/// <summary>
	/// Summary description for BitmapMenuItem.
	/// </summary>
	public class BitmapMenuItem : MenuItem
	{
		private Font fFont;
		private Image bImage = null;

		public BitmapMenuItem( string text, EventHandler handler, string bitmap ) : base( text, handler )
		{
			bImage = new Bitmap( bitmap );
			fFont = SystemInformation.MenuFont;
			OwnerDraw = true;
		}

		public BitmapMenuItem( string text, EventHandler handler, Image bitmap ) : base( text, handler )
		{
			bImage = new Bitmap( bitmap );
			fFont = SystemInformation.MenuFont;
			OwnerDraw = true;
		}

		public BitmapMenuItem( string text, EventHandler handler, Stream bitmap ) : base( text, handler )
		{
			bImage = new Bitmap( bitmap );
			fFont = SystemInformation.MenuFont;
			OwnerDraw = true;
		}

		/// <summary>
		///  override the on measure item 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnMeasureItem(MeasureItemEventArgs e)
		{
			base.OnMeasureItem (e);
			float[] tabStopDistance = { 0 };

			StringFormat sfString = new StringFormat();

			sfString.HotkeyPrefix = HotkeyPrefix.Show;
			sfString.SetTabStops( 50, tabStopDistance );

			if( bImage.Height > fFont.Height )
				e.ItemHeight = bImage.Height + 6;
			else
				e.ItemHeight = fFont.Height + 6;

			e.ItemWidth = ( int )e.Graphics.MeasureString( AppendShortCut(), fFont, 1000, sfString ).Width + bImage.Width + 5;

			sfString.Dispose();
		}


		protected override void OnDrawItem(DrawItemEventArgs e)
		{
			base.OnDrawItem (e);
			float[] tabStopDistance = {0};

			if( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected )
			{
				e.Graphics.FillRectangle( SystemBrushes.Highlight, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height );
			}
			else
				e.Graphics.FillRectangle( SystemBrushes.Control, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height );

			if( bImage != null )
			{
				e.Graphics.DrawRectangle( SystemPens.Control, e.Bounds );
				e.Graphics.DrawImage( bImage, e.Bounds.Left + 3, e.Bounds.Top + 3 );
			}

			SolidBrush brush = new SolidBrush( SystemColors.WindowText );
			StringFormat sfString = new StringFormat();
			sfString.HotkeyPrefix = HotkeyPrefix.Show;
			sfString.SetTabStops( 50, tabStopDistance );
			
			e.Graphics.DrawString( AppendShortCut(), fFont, brush, e.Bounds.Left + bImage.Width + 5, e.Bounds.Top + 2, sfString );

			brush.Dispose();
			sfString.Dispose();
		}

		private string AppendShortCut()
		{
			string strString = Text;

			if( ShowShortcut && Shortcut != Shortcut.None )
			{
				strString += Shortcut.ToString();
			} 

			return strString;
		}
	}

	public class IconMenuItem : MenuItem
	{
		private Font fFont;
		private Icon iImage = null;

		public IconMenuItem( string text, EventHandler handler, string icon ) : base( text, handler )
		{
			iImage = new Icon( icon );
			fFont = SystemInformation.MenuFont;
			OwnerDraw = true;
		}

		public IconMenuItem( string text, EventHandler handler, Icon icon ) : base( text, handler )
		{
			Size iconSize = new Size( 16, 16 );
			iImage = new Icon( icon, iconSize );
			fFont = SystemInformation.MenuFont;
			OwnerDraw = true;
		}

		public IconMenuItem( string text, EventHandler handler, Icon icon, Size size ) : base( text, handler )
		{
			iImage = new Icon( icon, size );
			fFont = SystemInformation.MenuFont;
			OwnerDraw = true;
		}

		public IconMenuItem( string text, EventHandler handler, Stream icon ) : base( text, handler )
		{
			iImage = new Icon( icon );
			fFont = SystemInformation.MenuFont;
			OwnerDraw = true;
		}

		/// <summary>
		///  override the on measure item 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnMeasureItem(MeasureItemEventArgs e)
		{
			base.OnMeasureItem (e);
			float[] tabStopDistance = { 0 };

			StringFormat sfString = new StringFormat();

			sfString.HotkeyPrefix = HotkeyPrefix.Show;
			sfString.SetTabStops( 50, tabStopDistance );

			if( iImage.Height > fFont.Height )
				e.ItemHeight = iImage.Height + 6;
			else
				e.ItemHeight = fFont.Height + 6;

			e.ItemWidth = ( int )e.Graphics.MeasureString( AppendShortCut(), fFont, 1000, sfString ).Width + iImage.Width + 5;

			sfString.Dispose();
		}


		protected override void OnDrawItem(DrawItemEventArgs e)
		{
			base.OnDrawItem (e);
			float[] tabStopDistance = {0};

			if( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected )
			{
				e.Graphics.FillRectangle( SystemBrushes.Highlight, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height );
			}
			else
				e.Graphics.FillRectangle( SystemBrushes.Control, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height );

			if( iImage != null )
			{
				e.Graphics.DrawRectangle( SystemPens.Control, e.Bounds );
				e.Graphics.DrawIcon( iImage, e.Bounds.Left + 3, e.Bounds.Top + 3 );
			}

			SolidBrush brush = new SolidBrush( SystemColors.WindowText );
			StringFormat sfString = new StringFormat();
			sfString.HotkeyPrefix = HotkeyPrefix.Show;
			sfString.SetTabStops( 50, tabStopDistance );
			
			e.Graphics.DrawString( AppendShortCut(), fFont, brush, e.Bounds.Left + iImage.Width + 5, e.Bounds.Top + 2, sfString );

			brush.Dispose();
			sfString.Dispose();
		}

		private string AppendShortCut()
		{
			string strString = Text;

			if( ShowShortcut && Shortcut != Shortcut.None )
			{
				strString += Shortcut.ToString();
			} 

			return strString;
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -