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

📄 toolbarex.cs

📁 c#编写的仿OUTLOOK工具条的Winform菜单
💻 CS
📖 第 1 页 / 共 3 页
字号:
				if ( disabled ) 
					ControlPaint.DrawImageDisabled(g, image, point.X, point.Y, ColorUtil.VSNetSelectionColor);
				else if ( hot && !selected && !item.Checked )
				{
					ControlPaint.DrawImageDisabled(g, image, point.X+1, point.Y, ColorUtil.VSNetSelectionColor);
					g.DrawImage(image, point.X, point.Y-1);
				}
				else 
				{
					if ( item.Checked  )
					{
						if  ( !selected ) point.Y -= 1;
					}
					g.DrawImage(image, point.X, point.Y);
				}
			}

			// Draw Text 
			if ( hasText )
			{
				string currentText = item.Text;
				int amperSandIndex = currentText.IndexOf('&');
				if ( barType == BarType.MenuBar && amperSandIndex != -1 )
					currentText = item.Text.Remove(amperSandIndex, 1);
				Size textSize = TextUtil.GetTextSize(g, currentText, SystemInformation.MenuFont);
				Point pos;
				if ( barType == BarType.MenuBar )	
				{
					int offset = rc.left + ((rc.right - rc.left) - textSize.Width)/2;
					pos = new Point(offset, rc.top + ((rc.bottom - rc.top - textSize.Height) / 2));
				}
				else
				{
					pos = new Point(rc.left, rc.top + ((rc.bottom - rc.top - textSize.Height) / 2));
					if ( image != null )
					{
						pos.X = rc.left + MARGIN + image.Size.Width + MARGIN;
					}
				}

				StringFormat stringFormat = new StringFormat();
				stringFormat.HotkeyPrefix = HotkeyPrefix.Show;
				g.DrawString(item.Text, SystemInformation.MenuFont, Brushes.Black, pos, stringFormat); 
			}
		
			m.Result = (IntPtr) CustomDrawReturnFlags.CDRF_SKIPDEFAULT;
		
		}

		protected void DrawArrowGlyph(Graphics g, Rectangle rectangle)
		{
			// Draw arrow glyph
			Point[] pts = new Point[3];
			int leftEdge = rectangle.Left + (rectangle.Width-DROWPDOWN_ARROW_WIDTH+1);
			int middle = rectangle.Top + rectangle.Height/2-1;
			pts[0] = new Point(leftEdge + 4, middle);
			pts[1] = new Point(leftEdge + 9,  middle);
			pts[2] = new Point(leftEdge + 6, middle+3);
			g.FillPolygon(Brushes.Black, pts);

		}

		public override bool PreProcessMessage(ref Message message)
		{
			if (message.Msg == (int)Msg.WM_KEYDOWN || message.Msg == (int)Msg.WM_SYSKEYDOWN)
			{
				// Check for shortcuts in ToolBarItems in this toolbar
				Keys keys = (Keys)(int) message.WParam  | ModifierKeys;
				ToolBarItem shortcutHit = items[keys];
				if (shortcutHit != null && shortcutHit.Enabled )
				{
					shortcutHit.RaiseClick();
					return true;
				}

				// Check for shortcuts in the menuitems of the popup menu
				// currently being displayed
				if ( barType == BarType.MenuBar )
				{
					MenuItem hitItem = null;
					foreach ( ToolBarItem tbi in items ) 
					{
						hitItem = FindShortcutItem(tbi.MenuItems, keys);
						if ( hitItem != null)
							break;
					}
					if ( hitItem != null )
						hitItem.PerformClick();
				}
  
				//  Check if we have a mnemonic
				bool alt = ((keys & Keys.Alt) != 0);
				if ( alt )
				{
					Keys keyCode = keys & Keys.KeyCode;
					char key = (char)(int)keyCode;
					if ((Char.IsDigit(key) || (Char.IsLetter(key))))
					{
						ToolBarItem mnemonicsHit = items[key];        
						if ( mnemonicsHit != null ) 
						{
							if ( barType == BarType.MenuBar )
								TrackDropDown(mnemonicsHit.Index);
							else
								mnemonicsHit.RaiseClick();
							return true;
						}
					}
				}
			}
			
			return false;
		}


		private MenuItem FindShortcutItem(MenuItem[] menuItems, Keys keys)
		{
			MenuItem resultItem = null;
			foreach (MenuItem item in menuItems )
			{
				if ( ((int)item.Shortcut == (int)keys) && (item.Enabled) && (item.Visible) )
					return item;
				else
				{
					resultItem =  FindShortcutItem(item.MenuItems, keys);
					if ( resultItem != null )
						break;
				}
			}
			return resultItem;
		}

		private MenuItem FindShortcutItem(Menu.MenuItemCollection collection, Keys keys)
		{
			int count = collection.Count;
			foreach (MenuItem item in collection )
			{
				if ( ((int)item.Shortcut == (int)keys) && (item.Enabled) && (item.Visible) )
					return item;
				else
					return FindShortcutItem(item.MenuItems, keys);
			}
			return null;
		}

		protected override void OnFontChanged(EventArgs e) 
		{
			base.OnFontChanged(e);
			UpdateItems();
		}

		void Items_Changed(Object s, EventArgs e)
		{
			UpdateItems();
		}

		void Item_Changed(Object s, EventArgs e)
		{
			ToolBarItem[] handledItems = this.handledItems;
			foreach (ToolBarItem item in handledItems)
			{
				if ( item == s )
				{
					if (item == null) return;
					int index = items.IndexOf(item);
					if (index == -1) return;
					UpdateItem(index);
				}
			}
		}
	
		void BeginUpdate()
		{
			WindowsAPI.SendMessage(Handle, (int)Msg.WM_SETREDRAW, 0, 0);	
		}
	
		void EndUpdate()
		{
			WindowsAPI.SendMessage(Handle, (int)Msg.WM_SETREDRAW, 1, 0);
		}


		public void UpdateToolBarItems()
		{	
			
			UpdateImageList();	
			for (int i = 0; i < items.Count; i++)
			{
				// The toolbar handle is going to be destroy to correctly update the toolbar 
				// itself. We need to detach the toolbar as the parent of the comboboxes --otherwise the comboboxes
				// does not behave appropiately after we pull the plug on the parent
				// The combobox will again parented to the toolbar once the handle is recreated and when the
				// RealizeItem routine gets the information for the toolbaritems
				if ( items[i].Style == ToolBarItemStyle.ComboBox )
				{
					WindowsAPI.SetParent(items[i].ComboBox.Handle, IntPtr.Zero);
					items[i].ComboBox.Parent = null;
				}

			}
			UpdateItems();
		}

		void UpdateItems()
		{
			Detach();
			Attach();
			if (IsHandleCreated) RecreateHandle();
		}
	
		TBBUTTONINFO GetButtonInfo(int index)
		{
			ToolBarItem item = items[index];
			TBBUTTONINFO tbi = new TBBUTTONINFO();
			tbi.cbSize = Marshal.SizeOf(typeof(TBBUTTONINFO));
			tbi.dwMask = (int)(ToolBarButtonInfoFlags.TBIF_IMAGE | ToolBarButtonInfoFlags.TBIF_STATE | 
				 ToolBarButtonInfoFlags.TBIF_STYLE | ToolBarButtonInfoFlags.TBIF_COMMAND | ToolBarButtonInfoFlags.TBIF_SIZE);
			tbi.idCommand = index;			
			tbi.iImage = (int)ToolBarButtonInfoFlags.I_IMAGECALLBACK;
			tbi.fsState = 0;
			tbi.cx = 0;
			tbi.lParam = IntPtr.Zero;
			tbi.pszText = IntPtr.Zero;
			tbi.cchText = 0;
			
			if ( item.Style == ToolBarItemStyle.ComboBox )
			{
				tbi.fsStyle = (int)(ToolBarButtonStyles.TBSTYLE_BUTTON) ;
				tbi.cx = (short)item.ComboBox.Width;
				WindowsAPI.SetParent(item.ComboBox.Handle, Handle);
			}
			else if ( item.Text != null && item.Text != string.Empty )
			{
				tbi.fsStyle = (int)(ToolBarButtonStyles.TBSTYLE_BUTTON);
				tbi.cx = MARGIN;
				if ( item.Image != null )
					tbi.cx += (short)(item.Image.Size.Width + MARGIN);
				if ( item.Style == ToolBarItemStyle.DropDownButton )
					tbi.cx += DROWPDOWN_ARROW_WIDTH;
				Graphics g = CreateGraphics();
				string currentText = item.Text;
				int amperSandIndex = currentText.IndexOf('&');
				if ( amperSandIndex != -1 )
					currentText = item.Text.Remove(amperSandIndex, 1);
				Size size = TextUtil.GetTextSize(g, currentText, SystemInformation.MenuFont);
                g.Dispose();
				if ( barType == BarType.MenuBar)
				{
					tbi.cx += (short)(size.Width + 2*MENUTEXT_MARGIN);
				}
				else
					tbi.cx += (short)(size.Width + 2*MARGIN);
				
				tbi.dwMask |= (int)ToolBarButtonInfoFlags.TBIF_TEXT;
				tbi.pszText = Marshal.StringToHGlobalAuto(item.Text + "\0");
				tbi.cchText = item.Text.Length;

				if (  IsCommonCtrl6() && barType != BarType.MenuBar )
				{
					// If we let the operating system do the drawing
					// the DROWPDOWN_ARROW_WIDTH is slightly bigger than
					// the value we are using add some padding to compensate
					tbi.cx += 6;
				}

			}
			else 
			{
				tbi.fsStyle = (int)(ToolBarButtonStyles.TBSTYLE_BUTTON | ToolBarButtonStyles.TBSTYLE_AUTOSIZE );
				tbi.cx = 0;
			}

			if (!item.Visible)
				tbi.fsState |= (int)ToolBarButtonStates.TBSTATE_HIDDEN;
						
			if (item.Style == ToolBarItemStyle.Separator)
			{
				tbi.fsStyle |= (int)ToolBarButtonStyles.TBSTYLE_SEP;
			}			
			else
			{
				if (item.Enabled)
					tbi.fsState |= (int)ToolBarButtonStates.TBSTATE_ENABLED;

				if ( item.Style == ToolBarItemStyle.DropDownButton )
					tbi.fsStyle |= (int)ToolBarButtonStyles.TBSTYLE_DROPDOWN;

				if (item.Style == ToolBarItemStyle.PushButton)
					if (item.Checked) 
						tbi.fsState |= (int)ToolBarButtonStates.TBSTATE_CHECKED;
			}

			if (item.Style == ToolBarItemStyle.Separator )
				tbi.iImage = (int)ToolBarButtonInfoFlags.I_IMAGENONE;
			else if (item.Image != null)
				tbi.iImage = index;

			return tbi;				
		}

		void RealizeItems()
		{
			
			UpdateImageList();
						
			for (int i = 0; i < items.Count; i++)
			{
				items[i].Index = i;
				items[i].ToolBar = this;
				TBBUTTON button = new TBBUTTON();
				button.idCommand = i;
				WindowsAPI.SendMessage(Handle, (int)ToolBarMessages.TB_INSERTBUTTON, i, ref button);
	
				TBBUTTONINFO tbi = GetButtonInfo(i);
				WindowsAPI.SendMessage(Handle, (int)ToolBarMessages.TB_SETBUTTONINFOW, i, ref tbi);
				if ( items[i].Style == ToolBarItemStyle.ComboBox ) UpdateComboBoxPosition(i);
			}
						
            UpdateSize();
		}

		void UpdateComboBoxPosition(int index)
		{
			RECT rect = new RECT();
			WindowsAPI.SendMessage(Handle, (int)ToolBarMessages.TB_GETRECT, index, ref rect);
			int rectHeight = (rect.bottom-rect.top);
			int cbHeight = Items[index].ComboBox.Bounds.Height;
			int topOffset = rect.top+(rectHeight-cbHeight)/2;
			Items[index].ComboBox.Bounds = new Rectangle(rect.left+1, topOffset, 
					(rect.right-rect.left)-2, cbHeight);
			
		}


		void UpdateImageList()
		{
			Size size = new Size(16, SystemInformation.MenuFont.Height);
			for (int i = 0; i < items.Count; i++)
			{
				Image image = items[i].Image;
				if (image != null)
				{
					if (image.Width > size.Width) size.Width = image.Width;
					if (image.Height > size.Height) size.Height = image.Height;
				}
			}
	
			imageList = new ImageList();
			imageList.ImageSize = size;
			imageList.ColorDepth = ColorDepth.Depth32Bit;		

			for (int i = 0; i < items.Count; i++)
			{
				// Take combobox size into consideration too
				if ( items[i].Style == ToolBarItemStyle.ComboBox )
				{
					// update combobox to use the current system menu font
					items[i].ComboBox.Font = SystemInformation.MenuFont;
					ComboBoxBase cbb = (ComboBoxBase)items[i].ComboBox;
					int decrease = 2;
					if ( SystemInformation.MenuFont.Height >= 20)
						decrease = 5;
					cbb.SetFontHeight(SystemInformation.MenuFont.Height-decrease);
					
				}
				
				Image image = items[i].Image;
				imageList.Images.Add((image != null) ? image : new Bitmap(size.Width, size.Height));
			}

			IntPtr handle = (BarType == BarType.MenuBar) ? IntPtr.Zero : imageList.Handle;
			WindowsAPI.SendMessage(Handle, (int)ToolBarMessages.TB_SETIMAGELIST, 0, handle);
		}


		void UpdateSize()
		{
			Size size = new Size(0, 0);
			for (int i = 0; i < items.Count; i++)
			{
				RECT rect = new RECT();
				WindowsAPI.SendMessage(Handle, (int)ToolBarMessages.TB_GETRECT, i, ref rect);
				int height = rect.bottom - rect.top;
				if (height > size.Height) size.Height = height;
				size.Width += rect.right - rect.left;
			}
			Size = size;
		}

		public int GetIdealSize()
		{
			Size size = new Size(0, 0);
			for (int i = 0; i < items.Count; i++)
			{
				RECT rect = new RECT();
				WindowsAPI.SendMessage(Handle, (int)ToolBarMessages.TB_GETRECT, i, ref rect);
				int height = rect.bottom - rect.top;
				if (height > size.Height) size.Height = height;
				size.Width += rect.right - rect.left;
			}
			return size.Width;
		}

		void UpdateItem(int index)
		{
			if (!IsHandleCreated) return;

			if ( items[index].Visible == handledItemsVisible[index] )
			{
				TBBUTTONINFO tbi = GetButtonInfo(index);
				WindowsAPI.SendMessage(Handle, (int)ToolBarMessages.TB_SETBUTTONINFOW, index, ref tbi);
				if ( items[index].Style == ToolBarItemStyle.ComboBox ) UpdateComboBoxPosition(index);
			}			
			else
			{
				UpdateItems();
			}
		}
	}

	public interface IChevron
	{
		void Show(Control control, Point point);
	}

}

⌨️ 快捷键说明

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