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

📄 commandbar.cs

📁 c#源代码
💻 CS
📖 第 1 页 / 共 3 页
字号:
				{
					if ((menuItem != null) && (menuItem.OwnerDraw) && (menuItem.Mnemonic == key))
					{
						message.Result = (IntPtr) ((NativeMethods.MNC_EXECUTE << 16) | index);
						return;
					}
					
					if (menuItem.Visible) index++;
				}
			}
		}

		private void NotifyDropDown(ref Message message)
		{
			if (this.Style == CommandBarStyle.ToolBar) 
			{
				NativeMethods.NMTOOLBAR nmtb = (NativeMethods.NMTOOLBAR) message.GetLParam(typeof(NativeMethods.NMTOOLBAR));
				this.TrackDropDown(nmtb.iItem);
			}
		}

		private void NotifyNeedTextA(ref Message message)
		{
			if (this.Style != CommandBarStyle.Menu)
			{
				NativeMethods.TOOLTIPTEXTA toolTipText = (NativeMethods.TOOLTIPTEXTA) message.GetLParam(typeof(NativeMethods.TOOLTIPTEXTA));
				CommandBarItem item = (CommandBarItem) this.items[toolTipText.hdr.idFrom];
				toolTipText.szText = item.Text;

				CommandBarButtonBase buttonBase = item as CommandBarButtonBase;
				if ((buttonBase != null) && (buttonBase.Shortcut != Keys.None))
				{
					toolTipText.szText += " (" + TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(null, CultureInfo.InvariantCulture, buttonBase.Shortcut) + ")";
				}

				toolTipText.hinst = IntPtr.Zero;
				if (RightToLeft == RightToLeft.Yes)
				{
					toolTipText.uFlags |= NativeMethods.TTF_RTLREADING;
				}

				Marshal.StructureToPtr(toolTipText, message.LParam, true);
				message.Result = (IntPtr) 1;
			}
		}

		private void NotifyNeedTextW(ref Message message)
		{
			if ((this.Style != CommandBarStyle.Menu) && (Marshal.SystemDefaultCharSize == 2))
			{
				// this code is a duplicate of NotifyNeedTextA
				NativeMethods.TOOLTIPTEXT toolTipText = (NativeMethods.TOOLTIPTEXT) message.GetLParam(typeof(NativeMethods.TOOLTIPTEXT));
				CommandBarItem item = (CommandBarItem) items[toolTipText.hdr.idFrom];
				toolTipText.szText = item.Text;

				CommandBarButtonBase buttonBase = item as CommandBarButton;
				if ((buttonBase != null) && (buttonBase.Shortcut != Keys.None))
				{
					toolTipText.szText += " (" + TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(null, CultureInfo.InvariantCulture, buttonBase.Shortcut) + ")";
				}

				toolTipText.hinst = IntPtr.Zero;
				if (RightToLeft == RightToLeft.Yes) 
				{
					toolTipText.uFlags |= NativeMethods.TTF_RTLREADING;
				}

				Marshal.StructureToPtr(toolTipText, message.LParam, true);
				message.Result = (IntPtr) 1;
			}
		}
	
		protected override void OnFontChanged(EventArgs e) 
		{
			base.OnFontChanged(e);
			this.contextMenu.Font = this.Font;
			this.UpdateItems();
		}

		private bool PerformClick(CommandBarItem item)
		{
			Application.DoEvents();

			CommandBarControl control = item as CommandBarControl;
			if (control != null)
			{
				control.PerformClick(EventArgs.Empty);
				return true;
			}

			return false;
		}
	
		private void BeginUpdate()
		{
			NativeMethods.SendMessage(Handle, NativeMethods.WM_SETREDRAW, 0, 0);	
		}
	
		void EndUpdate()
		{
			NativeMethods.SendMessage(Handle, NativeMethods.WM_SETREDRAW, 1, 0);
		}
	
		private NativeMethods.TBBUTTONINFO GetButtonInfo(int index)
		{
			CommandBarItem item = items[index];

			NativeMethods.TBBUTTONINFO buttonInfo = new NativeMethods.TBBUTTONINFO();
			buttonInfo.cbSize = Marshal.SizeOf(typeof(NativeMethods.TBBUTTONINFO));

			buttonInfo.dwMask = NativeMethods.TBIF_IMAGE | NativeMethods.TBIF_STATE | NativeMethods.TBIF_STYLE | NativeMethods.TBIF_COMMAND;
			buttonInfo.idCommand = index;			
			buttonInfo.iImage = NativeMethods.I_IMAGECALLBACK;
			buttonInfo.fsStyle = NativeMethods.BTNS_BUTTON | NativeMethods.BTNS_AUTOSIZE;
			buttonInfo.fsState = 0;
			buttonInfo.cx = 0;
			buttonInfo.lParam = IntPtr.Zero;
			buttonInfo.pszText = IntPtr.Zero;
			buttonInfo.cchText = 0;

			if (!item.IsVisible)
			{
				buttonInfo.fsState |= NativeMethods.TBSTATE_HIDDEN;
			}

			CommandBarComboBox comboBox = item as CommandBarComboBox;
			if (comboBox != null)
			{
				buttonInfo.cx = (short) (comboBox.Width + 4);
				buttonInfo.dwMask = NativeMethods.TBIF_SIZE;
			}

			if (item is CommandBarSeparator)
			{
				buttonInfo.fsStyle |= NativeMethods.BTNS_SEP;
			}			
			else
			{
				if (item.IsEnabled)
				{
					buttonInfo.fsState |= NativeMethods.TBSTATE_ENABLED;
				}

				CommandBarMenu menu = item as CommandBarMenu;
				if ((menu != null) && (menu.Items.Count > 0))
				{
					buttonInfo.fsStyle |= NativeMethods.BTNS_DROPDOWN;
				}

				if (style == CommandBarStyle.ToolBar)
				{
					if (item is CommandBarMenu)
					{
						buttonInfo.fsStyle |= NativeMethods.BTNS_WHOLEDROPDOWN;
					}
				}

				CommandBarCheckBox checkBox = item as CommandBarCheckBox;
				if ((checkBox != null) && (checkBox.IsChecked))
				{
					buttonInfo.fsState |= NativeMethods.TBSTATE_CHECKED;
				}
			}

			if (item is CommandBarSeparator)
			{
				buttonInfo.iImage = NativeMethods.I_IMAGENONE;
			}
			else if (item.Image != null)
			{
				buttonInfo.iImage = index;
			}

			if ((this.Style == CommandBarStyle.Menu) && (item.Text != null) && (item.Text.Length != 0))
			{
				buttonInfo.dwMask |= NativeMethods.TBIF_TEXT;
				buttonInfo.pszText = Marshal.StringToHGlobalUni(item.Text + "\0");
				buttonInfo.cchText = item.Text.Length;
			}

			return buttonInfo;				
		}

		private void UpdateImageList()
		{
			IntPtr handle = IntPtr.Zero;
			if (this.Style != CommandBarStyle.Menu)
			{
				Size size = new Size(8, 8);
				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;
						}
					}
				}

				Image[] images = new Image[items.Count];
				for (int i = 0; i < items.Count; i++)
				{
					Image image = items[i].Image;
					images[i] = (image != null) ? image : new Bitmap(size.Width, size.Height);
				}

				if (this.imageList == null)
				{
					this.imageList = new ImageList();	
					this.imageList.ImageSize = size;
					this.imageList.ColorDepth = ColorDepth.Depth32Bit;

					for (int i = 0; i < images.Length; i++)
					{
						this.imageList.Images.Add(images[i]);	
					}
				}
				else if (this.imageList.Images.Count == images.Length)
				{
					for (int i = 0; i < images.Length; i++)
					{
						this.imageList.Images[i] = images[i];
					}
				}
				else
				{
					this.imageList.Images.Clear();
					this.imageList.ImageSize = size;

					for (int i = 0; i < images.Length; i++)
					{
						this.imageList.Images.Add(images[i]);
					}
				}

				handle = this.imageList.Handle;
			}

			NativeMethods.SendMessage(this.Handle, NativeMethods.TB_SETIMAGELIST, 0, handle);
		}

		private void UpdateItems()
		{
			if (this.IsHandleCreated)
			{
				this.BeginUpdate();
				this.RemoveItems();
				this.AddItems();
				this.EndUpdate();
			}
		}

		private void AddItems()
		{
			NativeMethods.SendMessage(Handle, NativeMethods.TB_BUTTONSTRUCTSIZE, Marshal.SizeOf(typeof(NativeMethods.TBBUTTON)), 0);

			int extendedStyle = NativeMethods.TBSTYLE_EX_HIDECLIPPEDBUTTONS | NativeMethods.TBSTYLE_EX_DOUBLEBUFFER;
			if (style == CommandBarStyle.ToolBar)
			{
				extendedStyle |= NativeMethods.TBSTYLE_EX_DRAWDDARROWS;
			}

			NativeMethods.SendMessage(Handle, NativeMethods.TB_SETEXTENDEDSTYLE, 0, extendedStyle);

			this.UpdateImageList();
			
			for (int i = 0; i < items.Count; i++)
			{
				NativeMethods.TBBUTTON button = new NativeMethods.TBBUTTON();
				button.idCommand = i;
				NativeMethods.SendMessage(this.Handle, NativeMethods.TB_INSERTBUTTON, i, ref button);
	
				NativeMethods.TBBUTTONINFO buttonInfo = this.GetButtonInfo(i);
				NativeMethods.SendMessage(this.Handle, NativeMethods.TB_SETBUTTONINFO, i, ref buttonInfo);
			}

			// Add ComboBox controls.
			this.Controls.Clear();
			for (int i = 0; i < items.Count; i++)
			{
				CommandBarComboBox comboBox = this.items[i] as CommandBarComboBox;
				if (comboBox != null)
				{
					NativeMethods.RECT rect = new NativeMethods.RECT();
					NativeMethods.SendMessage(this.Handle, NativeMethods.TB_GETITEMRECT, i, ref rect);

					rect.top = rect.top + (((rect.bottom - rect.top) - comboBox.Height) / 2);

					comboBox.ComboBox.Location = new Point(rect.left, rect.top);
					this.Controls.Add(comboBox.ComboBox);
				}
			}
	
			this.UpdateSize();
		}

		private void RemoveItems()
		{
						while (NativeMethods.SendMessage(this.Handle, NativeMethods.TB_BUTTONCOUNT, 0, 0) > 0)
						{
								NativeMethods.SendMessage(this.Handle, NativeMethods.TB_DELETEBUTTON, 0, 0);
						}
		}

		private void UpdateSize()
		{
			if (this.style == CommandBarStyle.Menu)
			{
				int fontHeight = Font.Height;

				using (Graphics graphics = this.CreateGraphics())
				{
					using (TextGraphics textGraphics = new TextGraphics(graphics))
					{
						foreach (CommandBarItem item in items)
						{
							Size textSize = textGraphics.MeasureText(item.Text, this.Font);
							if (fontHeight < textSize.Height)
							{
								fontHeight = textSize.Height;
							}
						}
					}
				}

				NativeMethods.SendMessage(this.Handle, NativeMethods.TB_SETBUTTONSIZE, 0, (fontHeight << 16) | 0xffff);
			}
			
			Size size = new Size(0, 0);
			for (int i = 0; i < items.Count; i++)
			{
				NativeMethods.RECT rect = new NativeMethods.RECT();
				NativeMethods.SendMessage(Handle, NativeMethods.TB_GETRECT, i, ref rect);
				int height = rect.bottom - rect.top;
				if (height > size.Height)
				{
					size.Height = height;
				}

				size.Width += rect.right - rect.left;
			}

			this.Size = size;
		}

		internal void AddItem(CommandBarItem item)
		{
			item.PropertyChanged += new PropertyChangedEventHandler(this.CommandBarItem_PropertyChanged);
			this.UpdateItems();
		}

		internal void RemoveItem(CommandBarItem item)
		{
			item.PropertyChanged -= new PropertyChangedEventHandler(this.CommandBarItem_PropertyChanged);
			this.UpdateItems();
		}

		private void CommandBarItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (this.IsHandleCreated)
			{
				CommandBarItem item = (CommandBarItem)sender;
				int index = this.Items.IndexOf(item);
				if (index != -1)
				{
					switch (e.PropertyName)
					{
						case "IsVisible":
							this.UpdateItems();
							break;

						case "Image":
							this.UpdateImageList();
							break;

						default:
							NativeMethods.TBBUTTONINFO buttonInfo = GetButtonInfo(index);
							NativeMethods.SendMessage(this.Handle, NativeMethods.TB_SETBUTTONINFO, index, ref buttonInfo);
							this.UpdateSize();
							break;
					}
				}
			}
		}
	}
}

⌨️ 快捷键说明

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