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

📄 nicemenu.cs

📁 Csharp编写的关于智能手机(smartphone)闹钟程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
		
		protected override void OnDrawItem(DrawItemEventArgs e) 
		{
			if ( doColorUpdate) 
			{
				DoUpdateMenuColors();
			}
			
			base.OnDrawItem(e);

			Graphics  g      = e.Graphics;
			Rectangle bounds =new Rectangle( new Point (e.Bounds .X ,e.Bounds .Y ),new Size (e.Bounds.Width ,e.Bounds .Height  ));
			bool selected = (e.State & DrawItemState.Selected) > 0;
			bool toplevel = (Parent == Parent.GetMainMenu());
			bool hasicon  = Icon != null;
			bool enabled = Enabled;
			
			DrawBackground(g, bounds, e.State, toplevel, hasicon, enabled);
			if (hasicon)
				DrawIcon(g, Icon, bounds, selected, Enabled, Checked);
			else
			{
				if (Checked)
					DrawCheckmark(g, bounds, selected);
				if (RadioCheck)
					DrawRadioCheckmark(g, bounds, selected);
			}
			
			if (Text == "-") 
			{ DrawSeparator(g, bounds);	} 
			else 
			{ DrawMenuText(g, bounds, Text, shortcuttext, Enabled, toplevel, e.State); }
		}

		/* ******************************
		 *		DrawRadioCheckmark
		 * ******************************/
		private void DrawRadioCheckmark(Graphics g, Rectangle bounds, bool selected) 
		{
			int checkTop = bounds.Top + (itemHeight - BITMAP_SIZE)/2;
			int checkLeft = bounds.Left + ( STRIPE_WIDTH - BITMAP_SIZE)/2;
			ControlPaint.DrawMenuGlyph(g, new Rectangle(checkLeft, checkTop, BITMAP_SIZE, BITMAP_SIZE), MenuGlyph.Bullet);
			g.DrawRectangle(new Pen(framecolor), checkLeft-1, checkTop-1, BITMAP_SIZE+1, BITMAP_SIZE+1);
		}
		private void DrawCheckmark(Graphics g, Rectangle bounds, bool selected) 
		{
			int checkTop = bounds.Top + (itemHeight - BITMAP_SIZE)/2;
			int checkLeft = bounds.Left + ( STRIPE_WIDTH - BITMAP_SIZE)/2;
			ControlPaint.DrawMenuGlyph(g, new Rectangle(checkLeft, checkTop, BITMAP_SIZE, BITMAP_SIZE), MenuGlyph.Checkmark);
			g.DrawRectangle(new Pen(framecolor), checkLeft-1, checkTop-1, BITMAP_SIZE+1, BITMAP_SIZE+1);
		}
		private void DrawIcon(Graphics g, Image icon, Rectangle bounds, bool selected, bool enabled, bool ischecked) 
		{
			int iconTop = bounds.Top + (itemHeight - BITMAP_SIZE)/2;
			int iconLeft = bounds.Left + (STRIPE_WIDTH - BITMAP_SIZE)/2;
			if (enabled) 
			{
				if (selected) 
				{
					ControlPaint.DrawImageDisabled(g, icon, iconLeft + 1, iconTop, Color.Black);
					g.DrawImage(icon, iconLeft, iconTop-1);
				} 
				else 
				{
					g.DrawImage(icon, iconLeft + 1, iconTop);
				}
			} 
			else 
			{
				ControlPaint.DrawImageDisabled(g, icon, iconLeft + 1, iconTop, SystemColors.HighlightText);
			}
		}
	
		private void DrawSeparator(Graphics g, Rectangle bounds) 
		{
			int y = bounds.Y + bounds.Height / 2;
			g.DrawLine(new Pen(SystemColors.ControlDark), bounds.X + iconSize + 7, y, bounds.X + bounds.Width - 2, y);
		}
		
		private void DrawBackground(Graphics g, Rectangle bounds, DrawItemState state, bool toplevel, bool hasicon, bool enabled) 
		{
			bool selected = (state & DrawItemState.Selected) > 0;
			
			if (selected || ((state & DrawItemState.HotLight) > 0)) 
			{
				if (toplevel && selected) 
				{
					bounds.Inflate(-1, 0);
					g.FillRectangle(new SolidBrush(barcolor), bounds);
					ControlPaint.DrawBorder3D(g, bounds.Left, bounds.Top, bounds.Width-1, bounds.Height, Border3DStyle.Flat , Border3DSide.Top | Border3DSide.Left | Border3DSide.Right);
				} 
				else 
				{
					if ( enabled ) 
					{
						g.FillRectangle(new SolidBrush(selectioncolor), bounds);
						g.DrawRectangle(new Pen(framecolor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
					}
					else 
					{
						g.FillRectangle(new SolidBrush(barcolor), bounds);
						bounds.X += STRIPE_WIDTH;
						bounds.Width -= STRIPE_WIDTH;
						g.FillRectangle(new SolidBrush(backcolor), bounds);
					}
				}
			} 
			else 
			{
				if (!toplevel) 
				{
					g.FillRectangle(new SolidBrush(barcolor), bounds);
					bounds.X += STRIPE_WIDTH;
					bounds.Width -= STRIPE_WIDTH;
					g.FillRectangle(new SolidBrush(backcolor), bounds);
				} 
				else 
				{
					g.FillRectangle(SystemBrushes.Control, bounds);
				}
			}
		}

		private void DrawMenuText(Graphics g, Rectangle bounds, string text, string shortcut, bool enabled, bool toplevel, DrawItemState state )	
		{
			StringFormat stringformat = new StringFormat();
			stringformat.HotkeyPrefix = ((state & DrawItemState.NoAccelerator) > 0) ? HotkeyPrefix.Hide : HotkeyPrefix.Show;
		
			if ( toplevel ) 
			{
				int index = text.IndexOf("&");
				if ( index != -1 ) 
				{
					text = text.Remove(index,1);
				}
			}
			
			int textwidth = (int)(g.MeasureString(text, SystemInformation.MenuFont).Width);
			int x = toplevel ? bounds.Left + (bounds.Width - textwidth) / 2: bounds.Left + iconSize + 10;
			int topGap = 4;
			if ( toplevel ) topGap = 2;
			int y = bounds.Top + topGap;
			Brush brush = null;
			
			if (!enabled)
				brush = new SolidBrush(SystemColors.GrayText);
			else 
				brush = new SolidBrush(SystemColors.MenuText);
			
			g.DrawString(text, SystemInformation.MenuFont, brush, x, y, stringformat);

			if ( !toplevel ) 
			{
				stringformat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
				g.DrawString(shortcut, SystemInformation.MenuFont, brush, bounds.Width - 10 , bounds.Top + topGap, stringformat);
			}
		}

		/* ******************************************************
		 *		UpdateMenu(MainMenu yourOldMenu)
		 *		UpdateMenu(ContextMenu yourOldMenu)
		 * ------------------------------------------------------
		 *		this function will update the user old menu
		 * ******************************************************/
		/// <summary>
		/// this function will update your Main Menu.
		/// </summary>
		public void UpdateMenu(MainMenu yourOldMenu, NiceMenuClickEvent yourClickFunction)
		{
			IList myMenuList = new Menu.MenuItemCollection(yourOldMenu);
			BuildMenuTree(myMenuList, yourOldMenu, yourClickFunction);
		}
		/// <summary>
		/// this function will update your Context Menu.
		/// </summary>
		public void UpdateMenu(ContextMenu yourOldMenu, NiceMenuClickEvent yourClickFunction)
		{
			IList myMenuList = new Menu.MenuItemCollection(yourOldMenu);
			BuildMenuTree(myMenuList, yourOldMenu, yourClickFunction);
		}
		/* ***********************************************************
		 *		BuildMenuTree (IList myMenu, MenuItem parentMenu)
		 * -----------------------------------------------------------
		 *		build the new submenu item 
		 * ***********************************************************/
		private void BuildMenuTree(IList myMenu, MenuItem parentMenu, NiceMenuClickEvent yourClickFunction)
		{
			foreach (MenuItem myMenuItem in myMenu) 
			{
				// Declaration
				NiceMenu newSubMenu;

				string IndexImage = "";
				bool AddMenuImage = false;
				
				/* If in the first two characters of the menu item text 
				 * there is a number I set AddMenuImage = true and the 
				 * IndexImage to get the icon in the image list control. */
				if (myMenuItem.Text.Length > 2)
				{
					IndexImage = myMenuItem.Text.Substring(0,2);
					if (Char.IsNumber(IndexImage,1) == true)
					{
						AddMenuImage = true;
						// I have to delete first two characters
						myMenuItem.Text = myMenuItem.Text.Substring(2);
					}
				}
				if (AddMenuImage == true)
					newSubMenu = new NiceMenu(myMenuItem.Text, new EventHandler(yourClickFunction), myMenuItem.Shortcut, MenuImages.Images[Convert.ToInt32(IndexImage)]);
				else
					newSubMenu = new NiceMenu(myMenuItem.Text, new EventHandler(yourClickFunction), myMenuItem.Shortcut);	
				// I add the new menu item to its parent
				parentMenu.MenuItems.Add(newSubMenu);
				// Checked 
				if (myMenuItem.Checked == true) newSubMenu.Checked = true;
				// RadioCheck 
				if (myMenuItem.RadioCheck == true) 
					if (myMenuItem.Checked == true) newSubMenu.RadioCheck = true;
				// DefaultItem 
				if (myMenuItem.DefaultItem == true) newSubMenu.DefaultItem = true;
				// Enabled
				if (myMenuItem.Enabled == false) newSubMenu.Enabled = false;
				// If this menu item contains child menu items
				if (myMenuItem.IsParent == true)
				{
					IList mySubMenu = new Menu.MenuItemCollection(myMenuItem);
					BuildMenuTree(mySubMenu, newSubMenu, yourClickFunction);
				}
			}
		}

		/* ***********************************************************
		 *	BuildMenuTree (IList myMenu, MainMenu parentMenu)
		 *  BuildMenuTree (IList myMenu, ContextMenu parentMenu, NiceMenuClickEvent yourClickFunction)
		 * -----------------------------------------------------------
		 *  build the new main menus and delete the old ones
		 * ***********************************************************/
		private void BuildMenuTree(IList myMenu, MainMenu parentMenu, NiceMenuClickEvent yourClickFunction)
		{
			int numOldMenu = myMenu.Count;

			foreach (MenuItem myMenuItem in myMenu) 
			{
				NiceMenu newMainMenu = new NiceMenu(myMenuItem.Text);
				parentMenu.MenuItems.Add(newMainMenu);
				if (myMenuItem.IsParent == true)
				{
					IList mySubMenu = new Menu.MenuItemCollection(myMenuItem);
					BuildMenuTree(mySubMenu, newMainMenu, yourClickFunction);
				}
			}
			// Now I have to delete the old menus
			for (int i=0;i<numOldMenu;i++) 
			{   parentMenu.MenuItems.RemoveAt(0);   }
		}
		
		/* ****************************************************
		 *	With the context menu I need the yourClickFunction
		 *	because also the main menu shall be clickable !       
		 * ****************************************************/
		private void BuildMenuTree(IList myMenu, ContextMenu parentMenu, NiceMenuClickEvent yourClickFunction)
		{
			int numOldMenu = myMenu.Count;

			foreach (MenuItem myMenuItem in myMenu) 
			{
				NiceMenu newMainMenu = new NiceMenu(myMenuItem.Text, new EventHandler(yourClickFunction), myMenuItem.Shortcut);;
				parentMenu.MenuItems.Add(newMainMenu);
				if (myMenuItem.IsParent == true)
				{
					IList mySubMenu = new Menu.MenuItemCollection(myMenuItem);
					BuildMenuTree(mySubMenu, newMainMenu, yourClickFunction);
				}
			}
			// Now I have to delete the old menus
			for (int i=0;i<numOldMenu;i++) 
			{   parentMenu.MenuItems.RemoveAt(0);   }
		}
	}
}

⌨️ 快捷键说明

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