Exemple #1
0
        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;
                    }
                }
            }
        }
Exemple #2
0
        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();
        }
Exemple #3
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 | NativeMethods.TB_DELETEBUTTON;
            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);
        }