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

📄 abstractmenueditorcontrol.cs

📁 全功能c#编译器
💻 CS
📖 第 1 页 / 共 2 页
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using ICSharpCode.SharpDevelop.FormDesigner.Hosts;
using ICSharpCode.SharpDevelop.FormDesigner.Services;

using System.Drawing.Design;

namespace ICSharpCode.SharpDevelop.FormDesigner {
	
	public enum ItemSelection {
		Last,
		First,
		Next,
		Prev
	}
	
	public abstract class AbstractMenuEditorControl : Panel
	{
		protected Menu         menu;
		protected SizeF[]      menuItemSize;
		protected RectangleF[] menuItemAreas;
		
		protected RectangleF   typeArea;
		protected SizeF        typeSize;
		protected string       typeAreaText = "Type here";
		
		protected IDesignerHost host;
		protected ItemEditor    itemEditor = null;
		
		protected Control       rootControl;
		protected SubMenuEditorControl subMenuEditor = null;
		
		protected int           oldSelectedItem = -1;
		protected int           selectedItem    = -1;
		protected int           mouseOverItem   = -1;
		protected bool          isMouseOverLeftSpacing = false;
		
		protected bool          isHorizontal = true;
		protected bool          canEditMenuItems = true;
		protected bool          drawMenuGlyphs   = false;
		protected Size borderSize = new Size(0, 0);
		
		public static bool MenuEditorFocused = false;
		public bool iveTheFocus;
		
		public int SelectedItem {
			get {
				return this.selectedItem;
			}
			set {
				this.oldSelectedItem = selectedItem;
				if (selectedItem != value) {
					selectedItem = value;
					Refresh();
				}
				ISelectionService selectionService = (ISelectionService)host.GetService(typeof(ISelectionService));
				if (menu != null && selectedItem >= 0 && selectedItem < menu.MenuItems.Count) {
					selectionService.SetSelectedComponents(new object[] { menu.MenuItems[selectedItem] });
				} else {
					selectionService.SetSelectedComponents(null);
				}
			}
		}
		
		protected override void OnGotFocus(EventArgs e)
		{
			base.OnGotFocus(e);
			MenuEditorFocused = true;
			iveTheFocus = true;
			Refresh();
		}
		
		protected override void OnLostFocus(EventArgs e)
		{
			base.OnLostFocus(e);
			iveTheFocus       = false;
//			SelectedItem      = -1;
			Refresh();
		}
		
		protected override Size DefaultSize {
			get {
				return new Size(320, System.Windows.Forms.SystemInformation.MenuHeight + 2);
			}
		}
		
		protected AbstractMenuEditorControl(IDesignerHost host, Menu menu)
		{
			this.host = host;
			this.menu = menu;
			rootControl = (Control)host.RootComponent;
			rootControl.Resize += new EventHandler(SetSize);
			SetSize(null, null);
			this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
		}
		
		public virtual void SetSize(object sender, EventArgs e)
		{
			int boundsLeftRightOffset = 0;
			int boundsTopOffset = 0;
			
			if (rootControl is Form) {
				Form f = (Form)rootControl;
				switch (f.FormBorderStyle) {
					case FormBorderStyle.Fixed3D:
						boundsLeftRightOffset = SystemInformation.Border3DSize.Width + SystemInformation.FixedFrameBorderSize.Width;
						break;
					
					case FormBorderStyle.FixedDialog:
					case FormBorderStyle.FixedSingle:
					case FormBorderStyle.FixedToolWindow:
						boundsLeftRightOffset = SystemInformation.FixedFrameBorderSize.Width;
						break;
					
					case FormBorderStyle.Sizable:
					case FormBorderStyle.SizableToolWindow:
						boundsLeftRightOffset = SystemInformation.FrameBorderSize.Width;
						break;
				}
				
				switch (f.FormBorderStyle) {
					case FormBorderStyle.Fixed3D:
					case FormBorderStyle.FixedDialog:
					case FormBorderStyle.FixedSingle:
						boundsTopOffset       = SystemInformation.FixedFrameBorderSize.Height + SystemInformation.CaptionHeight;
						break;
					
					case FormBorderStyle.Sizable:
						boundsTopOffset       = SystemInformation.FrameBorderSize.Height      + SystemInformation.CaptionHeight;
						break;
					
					case FormBorderStyle.SizableToolWindow:
						boundsTopOffset       = SystemInformation.FrameBorderSize.Height      + SystemInformation.ToolWindowCaptionHeight;
						break;
					case FormBorderStyle.FixedToolWindow:
						boundsTopOffset       = SystemInformation.FixedFrameBorderSize.Height + SystemInformation.ToolWindowCaptionHeight;
						break;
				}
			}
			
			SetBounds(rootControl.Bounds.Left  + boundsLeftRightOffset, 
			          rootControl.Bounds.Top   + boundsTopOffset, 
			          rootControl.Bounds.Width - boundsLeftRightOffset * 2,  
			          System.Windows.Forms.SystemInformation.MenuHeight + 2);
			Show();
		}
		
		protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
		{
			Graphics g = pevent.Graphics;
			g.FillRectangle(new SolidBrush(SystemColors.Menu), 
			                pevent.ClipRectangle.X, 
			                pevent.ClipRectangle.Y, 
			                pevent.ClipRectangle.Width, 
			                pevent.ClipRectangle.Height);
		}
		
		int GetLeftSpacing()
		{
			return drawMenuGlyphs ? SystemInformation.MenuHeight : 0;
		}
		int GetRightSpacing()
		{
			return drawMenuGlyphs ? SystemInformation.MenuHeight : 0;
		}
		int GetLeftRightSpacing()
		{
			return GetLeftSpacing() + GetRightSpacing();
		}
		
		protected virtual void MeasureMenuItems()
		{
			Graphics g = null;
			try {
				g = base.CreateGraphics();
			} catch (Exception e) {
				Console.WriteLine("Got exception : " + e.ToString());
				return;
			}

			Font f = System.Windows.Forms.SystemInformation.MenuFont;
			
			menuItemAreas = new RectangleF[menu.MenuItems.Count];
			
			float curX = 0;
			float curY = 0;
			float maxWidth = 0;
			menuItemSize = new SizeF[menu.MenuItems.Count];
			for (int i = 0; i < menu.MenuItems.Count; ++i) {
				menuItemSize[i] = g.MeasureString(menu.MenuItems[i].Text + "min", f);
				menuItemSize[i].Height += 2;
				maxWidth = Math.Max(maxWidth, menuItemSize[i].Width + GetLeftRightSpacing());
			}
			
			typeSize = g.MeasureString(typeAreaText, f);
			typeSize.Height += 2;
			maxWidth = Math.Max(maxWidth, typeSize.Width  + GetLeftRightSpacing());
			
			for (int i = 0; i < menu.MenuItems.Count; ++i) {
				menuItemAreas[i] = new RectangleF(curX + this.borderSize.Width, 
				                                  curY + this.borderSize.Height, 
				                                  isHorizontal ? menuItemSize[i].Width : maxWidth,
				                                  SystemInformation.MenuHeight);
				
				if (this.isHorizontal) {
					curX += menuItemAreas[i].Width + 4;
				} else {
					curY += menuItemAreas[i].Height;
				}
			}
			
			typeArea = new RectangleF(curX + this.borderSize.Width, 
			                          curY + this.borderSize.Height, 
			                          isHorizontal ? typeSize.Width : maxWidth, 
			                          SystemInformation.MenuHeight);
			
			g.Dispose();
		}
		
		protected void DrawTypeHere(Graphics g, bool isSelected, bool isMouseOver)
		{
			int yOffset = (int)((typeArea.Height - typeSize.Height) / 2);
			
			if (isSelected && iveTheFocus) {
				DrawSelcectionRectangle(g, typeArea, isMouseOver, false, true);
			} else {
				g.FillRectangle(new SolidBrush(Color.White),
				                (int)typeArea.X + 2,
				                (int)typeArea.Y + yOffset,
				                (int)typeArea.Width - 4,
				                (int)typeSize.Height);
				
				g.DrawRectangle(new Pen(Color.Gray), 
				                (int)typeArea.X + 2,
				                (int)typeArea.Y + yOffset,
				                (int)typeArea.Width - 4,
				                (int)typeSize.Height);
			}
			
			
			g.DrawString(typeAreaText,
			             SystemInformation.MenuFont,
			             SystemBrushes.InactiveBorder,
			             (int)typeArea.X + this.GetLeftSpacing(),
			             (int)typeArea.Y + yOffset);
		}
		
		protected void DrawSelcectionRectangle(Graphics g, RectangleF drawArea, bool isMouseOver, bool mouseOverLeft, bool alwaysSelectAll)
		{
			g.FillRectangle(SystemBrushes.Highlight,
			                (int)drawArea.X + 2,
			                (int)drawArea.Y + 2,
			                (int)drawArea.Width - 3,
			                (int)drawArea.Height - 3);
			
			if (isMouseOver) {
				if (mouseOverLeft) {
					g.DrawRectangle(new Pen(Color.White),
					                (int)drawArea.X + 2,
					                (int)drawArea.Y + 2,
					                (int)this.GetLeftSpacing() - 3,
					                (int)drawArea.Height - 3);
				} else {
					if (this.drawMenuGlyphs && !alwaysSelectAll) {
						g.DrawRectangle(new Pen(Color.White),
							                (int)drawArea.X + this.GetLeftSpacing(),
							                (int)drawArea.Y + 2,
							                (int)drawArea.Width - 1 -  this.GetLeftSpacing() - 1,
							                (int)drawArea.Height - 3);
					} else {
						g.DrawRectangle(new Pen(Color.White),
							                (int)drawArea.X + 2,
							                (int)drawArea.Y + 2,
							                (int)drawArea.Width - 3,
							                (int)drawArea.Height - 3);
					}
				}
			} 
			
			g.DrawRectangle(new Pen(new HatchBrush(HatchStyle.Percent50, SystemColors.Highlight, SystemColors.Menu)),
			                (int)drawArea.X,
			                (int)drawArea.Y,
			                (int)drawArea.Width,
			                (int)drawArea.Height);
		}
		
		protected void DrawItem(Graphics g, int i, bool isSelected, bool isMouseOver)
		{
			Brush drawBrush = SystemBrushes.ControlText;
			if (isSelected && iveTheFocus) {
				DrawSelcectionRectangle(g, menuItemAreas[i], isMouseOver, isMouseOverLeftSpacing, menu.MenuItems[i].MenuItems.Count > 0);
				drawBrush = SystemBrushes.HighlightText;
			}
			
			int yOffset = (int)((menuItemAreas[i].Height - menuItemSize[i].Height) / 2);
			g.DrawString(menu.MenuItems[i].Text,
			             SystemInformation.MenuFont,
			             drawBrush,
			             (int)menuItemAreas[i].X + this.GetLeftSpacing(),
			             (int)menuItemAreas[i].Y + yOffset + 1);
			
			if (drawMenuGlyphs) {
				int r = SystemInformation.MenuHeight;
				if (menu.MenuItems[i].Checked) {
					ControlPaint.DrawMenuGlyph(g, (int)menuItemAreas[i].X,
					                              (int)menuItemAreas[i].Y, 
					                              r, r, MenuGlyph.Checkmark);
				}

⌨️ 快捷键说明

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