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

📄 popupmenu.cs

📁 c#编写的仿OUTLOOK工具条的Winform菜单
💻 CS
📖 第 1 页 / 共 5 页
字号:
						// Fill the entire drawing area with white
						using (SolidBrush whiteBrush = new SolidBrush(SystemColors.ControlLightLight))
							g.FillRectangle(whiteBrush, new Rectangle(drawRect.Left + 1, drawRect.Top,
																	  drawRect.Width - 1, drawRect.Height));

						Rectangle imageCol = new Rectangle(drawRect.Left, drawRect.Top,
														   imageColWidth, drawRect.Height);

						// Draw the image column background
						g.FillRectangle(SystemBrushes.Control, imageCol);
						break;
					case VisualStyle.Plain:
							g.FillRectangle(SystemBrushes.Control, new Rectangle(drawRect.Left, drawRect.Top,
																				 drawRect.Width, drawRect.Height));
						break;
					}

				}

				// Always draw the expansion bitmap
				g.DrawImage(_menuImages.Images[(int)ImageIndex.Expansion], xPos, yPos);
			}
			else
			{
				// Is this item a separator?
				if (dc.Separator)
				{
					if (dc.VerticalSeparator)
					{
						switch(_style)
						{
						case VisualStyle.IDE:
							// Draw the separator as a single line
							using (Pen separatorPen = new Pen(SystemColors.ControlDark))
								g.DrawLine(separatorPen, drawRect.Left, drawRect.Top, drawRect.Left, drawRect.Bottom);
							break;
						case VisualStyle.Plain:
							Color baseColor = SystemColors.Control;
							ButtonBorderStyle bsInset = ButtonBorderStyle.Inset;
							ButtonBorderStyle bsNone = ButtonBorderStyle.Inset;
							
							Rectangle sepRect = new Rectangle(drawRect.Left + 1, drawRect.Top, 2, drawRect.Height);
							
							// Draw the separator as two lines using Inset style
							ControlPaint.DrawBorder(g, sepRect, 
													baseColor, 1, bsInset, baseColor, 0, bsNone,
													baseColor, 1, bsInset, baseColor, 0, bsNone);
							break;
						}
					}
					else
					{
						switch(_style)
						{
						case VisualStyle.IDE:
							// Draw the image column background
							Rectangle imageCol = new Rectangle(drawRect.Left, drawRect.Top,
															   imageColWidth,
															   drawRect.Height);

							g.FillRectangle(new SolidBrush(ColorUtil.VSNetBackgroundColor), drawRect);
							g.FillRectangle(new SolidBrush(ColorUtil.VSNetControlColor), imageCol);
                             
							// Draw a separator
							using (Pen separatorPen = new Pen(Color.FromArgb(75, SystemColors.MenuText)))
							{
								// Draw the separator as a single line
								g.DrawLine(separatorPen, drawRect.Left + imageColWidth + textGapLeft, drawRect.Top + 1,
										   drawRect.Right-4, drawRect.Top + 1); 
										   
							}
							break;
						case VisualStyle.Plain:
							Color baseColor = SystemColors.Control;
							ButtonBorderStyle bsInset = ButtonBorderStyle.Inset;
							ButtonBorderStyle bsNone = ButtonBorderStyle.Inset;
							
							Rectangle sepRect = new Rectangle(drawRect.Left + 2, drawRect.Top + 1, 
															  drawRect.Width - 4, 2);
							
							// Draw the separator as two lines using Inset style
							ControlPaint.DrawBorder(g, sepRect, 
													baseColor, 0, bsNone, baseColor, 1, bsInset,
													baseColor, 0, bsNone, baseColor, 1, bsInset);
							break;
						}
					}
				}
				else
				{
					// Should the command be drawn selected?
					if (hotCommand && mc.ComboBox == null )
					{
						switch(_style)
						{
						case VisualStyle.IDE:
							Rectangle selectArea = new Rectangle(drawRect.Left + 1, drawRect.Top,
																 drawRect.Width - 3, drawRect.Height - 1);

							using (Pen selectPen = new Pen(ColorUtil.VSNetBorderColor))
							{
								// Draw the selection area white, because we are going to use an alpha brush
								using (SolidBrush whiteBrush = new SolidBrush(Color.White))
									g.FillRectangle(whiteBrush, selectArea);

								using (SolidBrush selectBrush = new SolidBrush(Color.FromArgb(70, ColorUtil.VSNetBorderColor)))
								{
									// Draw the selection area
									g.FillRectangle(selectBrush, selectArea);

									// Draw a border around the selection area
									g.DrawRectangle(selectPen, selectArea);
								}
							}
							break;
						case VisualStyle.Plain:
							using (SolidBrush selectBrush = new SolidBrush(ColorUtil.VSNetBorderColor))
								g.FillRectangle(selectBrush, drawRect);
							break;
						}
					}
					else
					{
						switch(_style)
						{
						case VisualStyle.IDE:
							// Fill the entire drawing area with white
							using (SolidBrush whiteBrush = new SolidBrush(ColorUtil.VSNetBackgroundColor))
								g.FillRectangle(whiteBrush, new Rectangle(drawRect.Left + 1, drawRect.Top,
																		  drawRect.Width - 1, drawRect.Height));

							Rectangle imageCol = new Rectangle(drawRect.Left, drawRect.Top,
															   imageColWidth, drawRect.Height);

							// Draw the image column background
							g.FillRectangle(new SolidBrush(ColorUtil.VSNetControlColor), imageCol);

                            // If this is a combobox item, make sure to position the combobox a couple
                            // of pixel after the icon area 
							if ( mc.ComboBox != null )
							{
								// Combobox will paint itself
								mc.ComboBox.Left = drawRect.Left+imageColWidth + 2;
								mc.ComboBox.Top = drawRect.Top + (drawRect.Height - mc.ComboBox.Height)/2;
				
							}

							break;
						case VisualStyle.Plain:
							g.FillRectangle(SystemBrushes.Control, new Rectangle(drawRect.Left, drawRect.Top,
																				 drawRect.Width, drawRect.Height));
							break;
						}
					}

					int leftPos = drawRect.Left + imageColWidth + textGapLeft;

					// Calculate text drawing rectangle
					Rectangle strRect = new Rectangle(leftPos, drawRect.Top, shortCutX - leftPos, drawRect.Height);

					// Left align the text drawing on a single line centered vertically
					// and process the & character to be shown as an underscore on next character
					StringFormat format = new StringFormat();
					format.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.NoWrap;
					format.Alignment = StringAlignment.Near;
					format.LineAlignment = StringAlignment.Center;
					format.HotkeyPrefix = HotkeyPrefix.Show;

					SolidBrush textBrush;

					// Create brush depending on enabled state
					if (mc.Enabled)
					{
						if (!hotCommand || (_style == VisualStyle.IDE))
							textBrush = new SolidBrush(SystemColors.MenuText);
						else
							textBrush = new SolidBrush(SystemColors.HighlightText);
					}
					else 
						textBrush = new SolidBrush(SystemColors.GrayText);

					// Helper values used when drawing grayed text in plain style
					Rectangle rectDownRight = strRect;
					rectDownRight.Offset(1,1);

					if (mc.Enabled || (_style == VisualStyle.IDE))
						g.DrawString(mc.Text, _textFont, textBrush, strRect, format);
					else
					{
						// Draw grayed text by drawing white string offset down and right
						using (SolidBrush whiteBrush = new SolidBrush(SystemColors.HighlightText))
							g.DrawString(mc.Text, _textFont, whiteBrush, rectDownRight, format);

						// And then draw in corret color offset up and left
						g.DrawString(mc.Text, _textFont, textBrush, strRect, format);
					}

					if (mc.Shortcut != Shortcut.None)
					{
						// Right align the shortcut drawing
						format.Alignment = StringAlignment.Far;

						if (mc.Enabled || (_style == VisualStyle.IDE))
						{
							// Draw the shortcut text 
							g.DrawString(GetShortcutText(mc.Shortcut), _textFont, textBrush, strRect, format);
						}
						else
						{
							// Draw grayed text by drawing white string offset down and right
							using (SolidBrush whiteBrush = new SolidBrush(SystemColors.HighlightText))
								g.DrawString(GetShortcutText(mc.Shortcut), _textFont, whiteBrush, rectDownRight, format);

							// And then draw in corret color offset up and left
							g.DrawString(GetShortcutText(mc.Shortcut), _textFont, textBrush, strRect, format);
						}
					}

					// The image offset from top of cell is half the space left after
					// subtracting the height of the image from the cell height
					int imageTop = drawRect.Top + (drawRect.Height - _imageHeight) / 2;

					Image image = null;

					// Should a check mark be drawn?
					if (mc.Checked)
					{
						switch(_style)
						{
						case VisualStyle.IDE:
							Pen boxPen;

							if (mc.Enabled)
								boxPen = new Pen(ColorUtil.VSNetBorderColor);
							else
								boxPen = new Pen(SystemColors.GrayText);

							// Draw the box around the checkmark area
							g.DrawRectangle(boxPen, new Rectangle(imageLeft - 1, imageTop - 1, 
																  _imageHeight + 2, _imageWidth + 2));

							boxPen.Dispose();
							break;
						case VisualStyle.Plain:
							break;
						}

						// Grab either tick or radio button image
						if (mc.RadioCheck)
						{
							if (hotCommand && (_style == VisualStyle.Plain))
								image = _menuImages.Images[(int)ImageIndex.RadioSelected];
							else
								image = _menuImages.Images[(int)ImageIndex.Radio];
						}
						else
						{
							if (hotCommand && (_style == VisualStyle.Plain))
								image = _menuImages.Images[(int)ImageIndex.CheckSelected];
							else
								image = _menuImages.Images[(int)ImageIndex.Check];
						}
					}
					else
					{
						try
						{
							// Is there an image available to be drawn?
							if ((mc.ImageList != null) && (mc.ImageIndex >= 0))
								image = mc.ImageList.Images[mc.ImageIndex];
							else if ( mc.Image != null)
								image = mc.Image;
						}
						catch(Exception)
						{
							// User supplied ImageList/ImageIndex are invalid, use an error image instead
							image = _menuImages.Images[(int)ImageIndex.ImageError];
						}
					}

					// Is there an image to be drawn?
					if (image != null)
					{
						if (mc.Enabled)
						{
							if ((hotCommand) && (!mc.Checked) && (_style == VisualStyle.IDE))
							{
								// Draw a disabled icon offset down and right
								ControlPaint.DrawImageDisabled(g, image, imageLeft + 1, imageTop + 1, 
															   SystemColors.HighlightText);

								// Draw an enabled icon offset up and left
								g.DrawImage(image, imageLeft - 1, imageTop - 1);
							}
							else
							{
								// Draw an enabled icon
								g.DrawImage(image, imageLeft, imageTop);
							}
						}
						else
						{
							// Draw a image disabled
							ControlPaint.DrawImageDisabled(g, image, imageLeft, imageTop, 
														   SystemColors.HighlightText);
						}
					}

					// Does the menu have a submenu defined?
					if (dc.SubMenu)
					{
						// Is the item enabled?
						if (mc.Enabled)
						{
							int subMenuIndex = (int)ImageIndex.SubMenu;

							if (hotCommand && (_style == VisualStyle.Plain))
								subMenuIndex = (int)ImageIndex.SubMenuSelected;

							// Draw the submenu arrow 
							g.DrawImage(_menuImages.Images[subMenuIndex], subMenuX, imageTop);
						}
						else
						{
							// Draw a image disabled
							ControlPaint.DrawImageDisabled(g, _menuImages.Images[(int)ImageIndex.SubMenu], 
														   subMenuX, imageTop, SystemColors.HighlightText);
						}
					}
				}
			}
		}

		protected void DrawAllCommands(Graphics g)
		{
			for(int i=0; i<_drawCommands.Count; i++)
			{
				// Grab some commonly used values				
				DrawCommand dc = _drawCommands[i] as DrawCommand;

				// Draw this command only
				DrawSingleCommand(g, dc, (i == _trackItem));
			}
		}

		protected string GetShortcutText(Shortcut shortcut)
		{
			// Get the key code
			char keycode = (char)((int)shortcut & 0x0000FFFF);

			// The type converter does not work for numeric values as it returns
			// Alt+D0 instad of Alt+0. So check for numeric keys and construct the
			// return string ourself.
			if ((keycode >= '0') && (keycode <= '9'))
			{
				string display = "";

				// Get the modifier
				int modifier = (int)((int)shortcut & 0xFFFF0000);
		
				if ((modifier & 0x00010000) != 0)
					display += "Shift+";

				if ((modifier & 0x00020000) != 0)
					display += "Ctrl+";
				
				if ((modifier & 0x00040000) != 0)
					display += "Alt+";

				display += keycode;

				return display;
			}

			return TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString((Keys)shortcut);
		}

		protected bool ProcessKeyUp()
		{
			int newItem = _trackItem;
			int startItem = newItem;

			for(int i=0; i<_drawCommands.Count; i++)
			{
				// Move to previous item
				newItem--;

				// Have we looped all the way around all the choices
				if (newItem == startItem)
					return false;

				// Check limits
				if (newItem < 0)
					newItem = _drawCommands.Count - 1;

				DrawCommand dc = _drawCommands[newItem] as DrawCommand;

				// Can we select this item?
				if (!dc.Separator && dc.Enabled)
				{
					// If a change has occured
					if (newItem != _trackItem)
					{
						// Modify the display of the two items 
						SwitchSelection(_trackItem, newItem, false, false);

						return true;
					}
				}
			}

			return false;
		}

		protected bool

⌨️ 快捷键说明

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