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

📄 menuitemex.cs

📁 类似outlook风格的程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
			int extraHeight = 2;
			e.ItemHeight  = SystemInformation.MenuHeight + extraHeight;
			if ( topLevel )
				e.ItemWidth  = textwidth - 5; 
			else
				e.ItemWidth   =  textwidth + 45;

			// save menu item heihgt for later use
			itemHeight = e.ItemHeight;
			
		}

		protected override void OnDrawItem(DrawItemEventArgs e)
		{
			
			base.OnDrawItem(e);

			if ( doColorUpdate) 
			{
				DoUpdateMenuColors();
			}

			Graphics g = e.Graphics;
			Rectangle bounds = e.Bounds;
			bool selected = (e.State & DrawItemState.Selected) > 0;
			bool toplevel = (Parent == Parent.GetMainMenu());
			bool hasIcon  = Icon != null;
			bool enabled = Enabled;

			// Try to  speed up drawing top level a little bit
			if ( toplevel )
			{
				DrawBackground(g, bounds, e.State, toplevel, hasIcon, enabled);
				DrawMenuText(g, bounds, Text, shortcuttext, Enabled, toplevel, e.State);
				return;
			}
			
			DrawBackground(g, bounds, e.State, toplevel, hasIcon, enabled);
			if (hasIcon)
				DrawIcon(g, Icon, bounds, selected, Enabled, Checked);
			else if ( imageList != null && imageIndex != -1 )
			{
				DrawIcon(g, imageList.Images[imageIndex], bounds, selected, Enabled, Checked);
			}

			else
				if (Checked && !hasIcon)
				DrawMenuGlyph(g, bounds, selected, true);
			else if ( RadioCheck )
				DrawMenuGlyph(g, bounds, selected, false);
			
			if (Text == "-") 
			{
				DrawSeparator(g, bounds);
			} 
			else 
			{
				DrawMenuText(g, bounds, Text, shortcuttext, Enabled, toplevel, e.State);
			}
		}

		public void DrawMenuGlyph(Graphics g, Rectangle bounds, bool selected,  bool bCheckMark)
		{
			int checkTop = bounds.Top + (itemHeight - BITMAP_SIZE)/2;
			int checkLeft = bounds.Left + ( STRIPE_WIDTH - BITMAP_SIZE)/2;
			
			if ( Enabled ) 
			{
				g.FillRectangle(new SolidBrush(selected ? darkSelectionColor:selectionColor), 
					bounds.Left+1, bounds.Top+1, STRIPE_WIDTH-3, bounds.Height-3);
				menuImages.Draw(g, checkLeft, checkTop, bCheckMark?0:1);
				g.DrawRectangle(new Pen(borderColor), bounds.Left+1, bounds.Top+1, STRIPE_WIDTH-3, bounds.Height-3);
			}
			else
			{
				int imageIndex = bCheckMark?0:1;
				ControlPaint.DrawImageDisabled(g, menuImages.Images[imageIndex], checkLeft, checkTop, Color.Black);
			}
		}

		
		public void DrawIcon(Graphics g, Image icon, Rectangle bounds, bool selected, bool enabled, bool isChecked)
		{
			// make icon transparent
			Color transparentColor  = Color.FromArgb(0, 128, 128);
			Bitmap tempIcon = (Bitmap)icon;
			tempIcon.MakeTransparent(transparentColor);

			int iconTop = bounds.Top + (itemHeight - BITMAP_SIZE)/2;
			int iconLeft = bounds.Left + ( STRIPE_WIDTH - BITMAP_SIZE)/2;
			if (enabled) 
			{
				if (selected) 
				{
					if ( isChecked ) 
					{
						DrawCheckedRectangle(g, bounds);
						g.DrawImage(icon, iconLeft + 1, iconTop);
					}
					else 
					{
						ControlPaint.DrawImageDisabled(g, icon, iconLeft + 1, iconTop, Color.Black);
						g.DrawImage(icon, iconLeft, iconTop-1);
					}
				} 
				else 
				{
					if ( isChecked ) 
						DrawCheckedRectangle(g, bounds);
					g.DrawImage(icon, iconLeft + 1, iconTop);
				}
			} 
			else 
			{
				ControlPaint.DrawImageDisabled(g, icon, iconLeft + 1, iconTop, SystemColors.HighlightText);
			}
		}


		private void DrawCheckedRectangle(Graphics g, Rectangle bounds)
		{
			int checkTop = bounds.Top + (itemHeight - BITMAP_SIZE)/2;
			int checkLeft = bounds.Left + ( STRIPE_WIDTH - BITMAP_SIZE)/2;
			g.FillRectangle(new SolidBrush(selectionColor), bounds.Left+1, bounds.Top+1, STRIPE_WIDTH-3, bounds.Height-3);
			g.DrawRectangle(new Pen(borderColor), bounds.Left+1, bounds.Top+1, STRIPE_WIDTH-3, bounds.Height-3);
		}

	
		public 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);
		}
		
		public 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) 
				{   // draw toplevel, selected menuitem
					bounds.Inflate(-1, 0);
					g.FillRectangle(new SolidBrush(stripeColor), bounds);
					
					if ( ColorUtil.UsingCustomColor )
					{
						GDIUtil.Draw3DRect(g, bounds, ColorUtil.VSNetBorderColor, ColorUtil.VSNetControlColor);
					}
					else 
					{
						ControlPaint.DrawBorder3D(g, bounds.Left, bounds.Top, bounds.Width, 
							bounds.Height, Border3DStyle.Flat, Border3DSide.Top | Border3DSide.Left | Border3DSide.Right);
					}
				} 
				else 
				{   // draw menuitem hotlighted
					if ( enabled ) 
					{   
						g.FillRectangle(new SolidBrush(selectionColor), bounds);
						g.DrawRectangle(new Pen(borderColor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
					}
					else 
					{
						// Check if menu item was selected by using the mouse or the keyboard
						RECT rc = new RECT();
						IntPtr parentHandle = Parent.Handle;
						uint index = (uint)Index;
						bool success = WindowsAPI.GetMenuItemRect(IntPtr.Zero, parentHandle, index, ref rc);
						Rectangle menuRect = new Rectangle(rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
						Point mp = Control.MousePosition;
						
						if ( !menuRect.Contains(mp) ) 
						{
							// Menu was selected by using keyboard
							g.FillRectangle(new SolidBrush(bgColor), bounds);
							g.DrawRectangle(new Pen(borderColor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
						}
						else 
						{
							// Menu was selected by using mouse
							g.FillRectangle(new SolidBrush(stripeColor), bounds);
							bounds.X += STRIPE_WIDTH;
							bounds.Width -= STRIPE_WIDTH;
							g.FillRectangle(new SolidBrush(bgColor), bounds);
						}
					}
				}
			} 
			else 
			{
				if (!toplevel) 
				{   // draw menuitem, unselected
					g.FillRectangle(new SolidBrush(stripeColor), bounds);
					bounds.X += STRIPE_WIDTH;
					bounds.Width -= STRIPE_WIDTH;
					g.FillRectangle(new SolidBrush(bgColor), bounds);
				} 
				else 
				{
					// draw toplevel, unselected menuitem
					g.FillRectangle(SystemBrushes.Control, bounds);
				}
			}
		}

		public void DrawMenuText(Graphics g, Rectangle bounds, string text, string shortcut, bool enabled, bool toplevel, DrawItemState state )
		{
			StringFormat stringformat = new StringFormat();
			stringformat.HotkeyPrefix = HotkeyPrefix.Show;
		
			// if 3D background happens to be black, as it is the case when
			// using a high contrast color theme, then make sure text is white
			bool highContrast = false;
			bool whiteHighContrast = false;
			if ( SystemColors.Control.ToArgb() == Color.FromArgb(255,0,0,0).ToArgb() ) highContrast = true;
			if ( SystemColors.Control.ToArgb() == Color.FromArgb(255,255,255,255).ToArgb() ) whiteHighContrast = true;

			// if menu is a top level, extract the ampersand that indicates the shortcut character
			// so that the menu text is centered
			string textTopMenu = text;
			if ( toplevel ) 
			{
				int index = text.IndexOf("&");
				if ( index != -1 ) 
				{
					// remove it
					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(Color.FromArgb(120, SystemColors.MenuText));
			else if ( highContrast ) 
				brush = new SolidBrush(Color.FromArgb(255, SystemColors.MenuText));
			else 
				brush = new SolidBrush(Color.Black);

			if ( whiteHighContrast && ( (state & DrawItemState.HotLight) > 0 
				|| ( (state & DrawItemState.Selected) > 0 && !toplevel )) )
				brush = new SolidBrush(Color.FromArgb(255, Color.White));
			
			if ( toplevel ) text = textTopMenu;
			g.DrawString(text, SystemInformation.MenuFont, brush, x, y, stringformat);
					
			// don't draw the shortcut for top level menus
			// in case there was actually one
			if ( !toplevel ) 
			{
				// draw shortcut right aligned
				stringformat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
				g.DrawString(shortcut, SystemInformation.MenuFont, brush, bounds.Width - 10 , bounds.Top + topGap, stringformat);
			}
		}


	}

}

⌨️ 快捷键说明

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