public CommandBarComboBox AddComboBox(string text, ComboBox comboBox) { CommandBarComboBox item = new CommandBarComboBox(text, comboBox); this.Add(item); return(item); }
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; CommandBarComboBox comboBox = items[i] as CommandBarComboBox; if ((comboBox != null) && (comboBox.ComboBox != null)) { if (comboBox.ComboBox.Height > size.Height) { size.Height = comboBox.ComboBox.Height; } this.UpdateComboBoxLocation(comboBox, i); } } this.Size = size; }
private void UpdateComboBoxLocation(CommandBarComboBox comboBox, int index) { NativeMethods.RECT rect = new NativeMethods.RECT(); NativeMethods.SendMessage(this.Handle, NativeMethods.TB_GETITEMRECT, index, ref rect); int height = rect.bottom - rect.top; if (height > comboBox.ComboBox.Height) { rect.top = rect.top + ((height - comboBox.ComboBox.Height) / 2); } comboBox.ComboBox.Location = new Point(rect.left + 2, rect.top); }
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 NativeMethods.TBBUTTONINFO GetButtonInfo(int index) { CommandBarItem item = items[index]; NativeMethods.TBBUTTONINFO buttonInfo = new NativeMethods.TBBUTTONINFO(); buttonInfo.cbSize = Marshal.SizeOf(buttonInfo); 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.lpszText = string.Empty; buttonInfo.cchText = 0; if (!item.Visible) { 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.Enabled) { 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.lpszText = item.Text + '\0'; buttonInfo.cchText = item.Text.Length; } return(buttonInfo); }