Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

Path Finder

Rate me:
Please Sign up or sign in to vote.
4.10/5 (29 votes)
4 May 200419 min read 103K   1.7K   59  
Playing with various Artifical Intelligence theories.
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;
		}
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions