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; } } } }
/// <include file='doc\ToolBarButton.uex' path='docs/doc[@for="ToolBarButton.GetTBBUTTONINFO"]/*' /> /// <devdoc> /// Returns a TBBUTTONINFO object that represents this ToolBarButton. /// </devdoc> internal NativeMethods.TBBUTTONINFO GetTBBUTTONINFO(bool updateText, int newCommandId) { NativeMethods.TBBUTTONINFO button = new NativeMethods.TBBUTTONINFO(); button.cbSize = Marshal.SizeOf(typeof(NativeMethods.TBBUTTONINFO)); button.dwMask = NativeMethods.TBIF_IMAGE | NativeMethods.TBIF_STATE | NativeMethods.TBIF_STYLE; // Comctl on Win98 interprets null strings as empty strings, which forces // the button to leave space for text. The only workaround is to avoid having comctl // update the text. if (updateText) { button.dwMask |= NativeMethods.TBIF_TEXT; } button.iImage = ImageIndexer.ActualIndex; if (newCommandId != commandId) { commandId = newCommandId; button.idCommand = newCommandId; button.dwMask |= NativeMethods.TBIF_COMMAND; } // set up the state of the button // button.fsState = 0; if (enabled) button.fsState |= NativeMethods.TBSTATE_ENABLED; if (partialPush && style == ToolBarButtonStyle.ToggleButton) button.fsState |= NativeMethods.TBSTATE_INDETERMINATE; if (pushed) button.fsState |= NativeMethods.TBSTATE_CHECKED; if (!visible) button.fsState |= NativeMethods.TBSTATE_HIDDEN; // set the button style // switch (style) { case ToolBarButtonStyle.PushButton: button.fsStyle = NativeMethods.TBSTYLE_BUTTON; break; case ToolBarButtonStyle.ToggleButton: button.fsStyle = NativeMethods.TBSTYLE_CHECK; break; case ToolBarButtonStyle.Separator: button.fsStyle = NativeMethods.TBSTYLE_SEP; break; } if (text == null) { button.pszText = Marshal.StringToHGlobalAuto("\0\0"); } else { string textValue = this.text; PrefixAmpersands(ref textValue); button.pszText = Marshal.StringToHGlobalAuto(textValue); } return button; }
// VSWhidbey 177016: This is necessary to get the width of the buttons in the toolbar, // including the width of separators, so that we can accurately position the tooltip adjacent // to the currently hot button when the user uses keyboard navigation to access the toolbar. internal int GetButtonWidth() { // Assume that this button is the same width as the parent's ButtonSize's Width int buttonWidth = Parent.ButtonSize.Width; NativeMethods.TBBUTTONINFO button = new NativeMethods.TBBUTTONINFO(); button.cbSize = Marshal.SizeOf(typeof(NativeMethods.TBBUTTONINFO)); button.dwMask = NativeMethods.TBIF_SIZE; int buttonID = (int)UnsafeNativeMethods.SendMessage(new HandleRef(Parent, Parent.Handle), NativeMethods.TB_GETBUTTONINFO, commandId, ref button); if (buttonID != -1) { buttonWidth = button.cx; } return buttonWidth; }
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(); }
/// <include file='doc\ToolBar.uex' path='docs/doc[@for="ToolBar.ForceButtonWidths"]/*' /> /// <devdoc> /// Forces the button sizes based on various different things. The default /// ToolBar button sizing rules are pretty primitive and this tends to be /// a little better, and lets us actually show things like DropDown Arrows /// for ToolBars /// </devdoc> /// <internalonly/> private void ForceButtonWidths() { if (buttons != null && buttonSize.IsEmpty && IsHandleCreated) { // force ourselves to re-compute this each time // maxWidth = -1; for (int x = 0; x < buttonCount; x++) { NativeMethods.TBBUTTONINFO tbbi = new NativeMethods.TBBUTTONINFO(); tbbi.cbSize = Marshal.SizeOf(typeof(NativeMethods.TBBUTTONINFO)); tbbi.cx = buttons[x].Width; if (tbbi.cx > maxWidth) { maxWidth = tbbi.cx; } tbbi.dwMask = NativeMethods.TBIF_SIZE; UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TB_SETBUTTONINFO, x, ref tbbi); } } }
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 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.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.pszText = Marshal.StringToHGlobalUni(item.Text + "\0"); buttonInfo.cchText = item.Text.Length; } return(buttonInfo); }