コード例 #1
0
            /// <summary>
            /// button の 順番を newIndex に変更します。
            /// </summary>
            /// <param name="button"></param>
            /// <param name="newIndex"></param>
            public void ChangeIndex(CSharpToolBarButton button, int newIndex)
            {
                if (button.toolBar == null)
                {
                    throw new ArgumentException("button に親が存在しません");
                }

                if (newIndex < 0 || newIndex > Count)
                {
                    throw new ArgumentOutOfRangeException();
                }

                if (button.Index == newIndex)
                {
                    return;
                }

                if (button.Index < newIndex)
                {
                    newIndex -= 1;
                }

                innerList.Remove(button);
                innerList.Insert(newIndex, button);

                toolBar.UpdateButtons();
            }
コード例 #2
0
        /// <summary>
        /// ドラッグ先を表す縦のラインを描画
        /// </summary>
        /// <param name="index">描画するボタンのインデックス (-1なら線を消す)</param>
        protected void DrawHorzLine(int index)
        {
            if (tempDropLine != Rectangle.Empty)
            {
                ControlPaint.FillReversibleRectangle(tempDropLine, Color.Black);
            }

            if (index >= 0)
            {
                CSharpToolBarButton button = (index < Buttons.Count) ?
                                             Buttons[index] : Buttons[Buttons.Count - 1];

                Rectangle rc = button.Bounds;
                rc.Width = 2;

                if (index >= Buttons.Count)
                {
                    rc.X = button.Bounds.Right - 2;
                }

                tempDropLine = RectangleToScreen(rc);

                using (Graphics g = CreateGraphics())
                    ControlPaint.FillReversibleRectangle(tempDropLine, Color.Black);
            }
            else
            {
                tempDropLine = Rectangle.Empty;
            }
        }
コード例 #3
0
 /// <summary>
 /// 指定したボタンを再描画
 /// </summary>
 /// <param name="button">再描画するボタン (nullを指定した場合は何もしない)</param>
 protected void UpdateButton(CSharpToolBarButton button)
 {
     if (button != null)
     {
         Invalidate(button.Bounds, false);
         Update();
     }
 }
コード例 #4
0
 /// <summary>
 /// CSharpToolBarButtonEventArgsクラスのインスタンスを初期化
 /// </summary>
 /// <param name="button">クリックされたボタン</param>
 public CSharpToolBarButtonEventArgs(CSharpToolBarButton button)
 {
     if (button == null)
     {
         throw new ArgumentNullException("button");
     }
     this.button = button;
 }
コード例 #5
0
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            CSharpToolBarButton button = ButtonFromPoint(e.X, e.Y);

            // セパレータの場合は何もしない
            if (button != null && button.Style == CSharpToolBarButtonStyle.Separator)
            {
                return;
            }

            // フラット形式の場合は、浮き出る境界線を描画
            if (e.Button == MouseButtons.None)
            {
                if (button == activeButton)
                {
                    return;
                }

                UpdateButton(activeButton);

                activeButton = button;

                if (appearance == CSharpToolBarAppearance.Flat &&
                    button != null)
                {
                    using (Graphics g = CreateGraphics())
                        ControlPaint.DrawBorder3D(g, button.Bounds, Border3DStyle.RaisedInner);
                }
                else if (appearance == CSharpToolBarAppearance.VisualStudio &&
                         button != null)
                {
                    using (Graphics g = CreateGraphics())
                        DrawButton(g, button, false, true);
                }
            }
            // ボタンのドラッグ操作
            else if (e.Button == MouseButtons.Left && allowDragButton)
            {
                if (ClientRect.Contains(e.X, e.Y))
                {
                    DrawHorzLine(GetDropButtonIndex(e.X, e.Y));
                    Cursor = Cursors.Default;
                }
                // クライアント領域から出ていればドラッグ操作を中止
                else
                {
                    DrawHorzLine(-1);
                    Cursor = Cursors.No;
                }
            }
        }
コード例 #6
0
            /// <summary>
            /// ツールバーの指定したインデックスの位置にボタンを挿入
            /// </summary>
            /// <param name="index">buttonを挿入する0から始まるインデックス番号</param>
            /// <param name="button">挿入するCSharpToolBarButton</param>
            public void Insert(int index, CSharpToolBarButton button)
            {
                if (index < 0 || index > Count)
                {
                    throw new ArgumentOutOfRangeException("index");
                }

                button.imageList = toolBar.ImageList;
                button.toolBar   = toolBar;

                innerList.Insert(index, button);
                toolBar.UpdateButtons();
            }
コード例 #7
0
        /// <summary>
        /// CSharpToolBarButtonクラスのインスタンスを初期化
        /// </summary>
        /// <param name="button">コピー元のボタン</param>
        private CSharpToolBarButton(CSharpToolBarButton button)
            : this()
        {
            if (button == null)
            {
                throw new ArgumentNullException("button");
            }

            this.text       = button.text;
            this.imageIndex = button.imageIndex;
            this.style      = button.style;
            this.tag        = button.tag;
        }
コード例 #8
0
            /// <summary>
            /// ボタンをツールバーから削除
            /// </summary>
            /// <param name="button">ツールバーから削除するCSharpToolBarButton</param>
            public void Remove(CSharpToolBarButton button)
            {
                int index = innerList.IndexOf(button);

                if (index >= 0)
                {
                    button.toolBar   = null;
                    button.imageList = null;

                    innerList.Remove(button);
                    toolBar.UpdateButtons();
                }
            }
コード例 #9
0
            /// <summary>
            /// ツールバーから指定したインデックスにあるボタンを削除
            /// </summary>
            /// <param name="index">削除するCSharpToolBarButtonのインデックス</param>
            public void RemoveAt(int index)
            {
                if (index < 0 || index >= Count)
                {
                    throw new ArgumentOutOfRangeException("index");
                }

                CSharpToolBarButton button = this[index];

                button.toolBar   = null;
                button.imageList = null;

                innerList.RemoveAt(index);
                toolBar.UpdateButtons();
            }
コード例 #10
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            activeButton = ButtonFromPoint(e.X, e.Y);

            // ボタンが押されたように描画
            if (e.Button == MouseButtons.Left &&
                activeButton != null)
            {
                if (activeButton.Style != CSharpToolBarButtonStyle.Separator)
                {
                    using (Graphics g = CreateGraphics())
                        DrawButton(g, activeButton, true, true);
                }
            }
        }
コード例 #11
0
        /// <summary>
        /// ドロップ先のボタンを取得
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        protected int GetDropButtonIndex(int x, int y)
        {
            CSharpToolBarButton button = ButtonFromPoint(x, y);

            if (button != null)
            {
                int x2 = x - button.Bounds.X;

                if (x2 >= button.Bounds.Width / 2)
                {
                    return(button.Index + 1);
                }
                else
                {
                    return(button.Index);
                }
            }
            return(-1);
        }
コード例 #12
0
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            // ボタンが押されたように描画
            if (e.Button == MouseButtons.Left &&
                activeButton != null)
            {
                if (activeButton.Style == CSharpToolBarButtonStyle.Separator)
                {
                    return;
                }

                CSharpToolBarButton button = ButtonFromPoint(e.X, e.Y);

                UpdateButton(activeButton);
                DrawHorzLine(-1);

                // クリックされたボタンと現在のマウス座標にあるボタンが別の物であれば、
                // activeButtonsを移動
                if (activeButton != button)
                {
                    if (allowDragButton && ClientRect.Contains(e.X, e.Y))
                    {
                        int index = GetDropButtonIndex(e.X, e.Y);

                        if (index >= 0 && index <= buttons.Count)
                        {
                            buttons.ChangeIndex(activeButton, index);
                        }
                    }
                    Cursor = Cursors.Default;
                }
                else
                {
                    // クリックイベントを発生させる
                    OnButtonClick(new CSharpToolBarButtonEventArgs(activeButton));
                }

                activeButton = null;
            }
        }
コード例 #13
0
            /// <summary>
            /// ツールバーの末尾にボタンを追加
            /// </summary>
            /// <param name="button">ツールバーに追加するボタン</param>
            /// <returns>ボタンが追加されたコレクション内のインデックス</returns>
            public int Add(CSharpToolBarButton button)
            {
                if (button == null)
                {
                    throw new ArgumentNullException("button");
                }
                if (button.toolBar != null)
                {
                    throw new ArgumentException("このボタンは既に他のツールバーに登録されています");
                }

                int index = innerList.Add(button);

                button.toolBar   = toolBar;
                button.imageList = toolBar.ImageList;

                toolBar.UpdateButtons();

                return(index);
            }
コード例 #14
0
        /// <summary>
        /// 指定したbuttonのRectangle座標を計算
        /// </summary>
        /// <param name="g">文字列幅の計算に使用するGraphicsクラスのインスタンス</param>
        /// <param name="button">サイズを計算するCSharpToolBarButton</param>
        /// <returns>buttonのRectangle座標を返す</returns>
        protected Rectangle GetButtonRect(Graphics g, CSharpToolBarButton button)
        {
            Size      borderSize = borderStyle == Border3DStyle.Adjust ? new Size(0, 0) : SystemInformation.Border3DSize;
            Rectangle rect       = new Rectangle(borderSize.Width, borderSize.Height, 0, 0);
            int       height     = 0;

            foreach (CSharpToolBarButton b in buttons)
            {
                Size size;

                if (b.Style == CSharpToolBarButtonStyle.Separator)
                {
                    size = GetButtonSize(g, b);
                }
                else
                {
                    size = autoAdjustSize ? GetButtonSize(g, b) : buttonSize;
                }

                rect.Width  = size.Width;
                rect.Height = size.Height;
                height      = Math.Max(height, size.Height);

                // 座標がツールバーの幅をはみ出して、
                // なおかつWrappableプロパティがtrueの場合
                if ((rect.X + rect.Width) > ClientSize.Width && Wrappable)
                {
                    rect.X  = borderSize.Width;
                    rect.Y += height;
                }

                if (b.Equals(button))
                {
                    return(rect);
                }

                rect.X += size.Width;
            }

            return(Rectangle.Empty);
        }
コード例 #15
0
        /// <summary>
        /// このインスタンスのコピーを作成
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            CSharpToolBarButton clone = new CSharpToolBarButton(this);

            return(clone);
        }
コード例 #16
0
        /// <summary>
        /// ボタンを描画
        /// </summary>
        /// <param name="g"></param>
        /// <param name="button"></param>
        /// <param name="pushed"></param>
        protected void DrawButton(Graphics g, CSharpToolBarButton button, bool pushed, bool active)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (button == null)
            {
                throw new ArgumentNullException("button");
            }

            StringFormat format = StringFormat.GenericDefault;

            format.Alignment     = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            if (textAlign == ToolBarTextAlign.Right)
            {
                format.FormatFlags = StringFormatFlags.NoWrap;
            }

            Rectangle bounds = button.Bounds;
            Rectangle imageRect = Rectangle.Empty, textRect = Rectangle.Empty;
            Size      imgSize = (imageList != null) ? imageList.ImageSize : new Size(0, 0);

            if (button.Style == CSharpToolBarButtonStyle.Separator)
            {
                // 境界線を描画
                Size      border = SystemInformation.Border3DSize;
                Rectangle rect   = button.Bounds;
                rect.X      += rect.Width / 2 - border.Width / 2;
                rect.Y      += _Margin.Y;
                rect.Height -= _Margin.Y;
                rect.Width   = border.Width;
                ControlPaint.DrawBorder3D(g, rect, Border3DStyle.Etched, Border3DSide.Right);
                return;
            }

            switch (textAlign)
            {
            // イメージを上辺、テキストを下辺に配置
            case ToolBarTextAlign.Underneath:
                imageRect = new Rectangle(bounds.X + bounds.Width / 2 - imgSize.Width / 2, bounds.Y + _Margin.Y, imgSize.Width, imgSize.Height);
                textRect  = new Rectangle(bounds.X, imageRect.Bottom, bounds.Width, bounds.Height - imageRect.Height);
                break;

            // イメージを左辺、テキストを右辺に配置
            case ToolBarTextAlign.Right:
                imageRect = new Rectangle(bounds.X + _Margin.X, bounds.Y + bounds.Height / 2 - imgSize.Height / 2, imgSize.Width, imgSize.Height);
                textRect  = new Rectangle(imageRect.Right, bounds.Y, bounds.Width - imageRect.Width, bounds.Height);
                break;
            }

            if (appearance == CSharpToolBarAppearance.Normal)
            {
                if (pushed)
                {
                    // 通常のボタンが押された状態を描画
                    ControlPaint.DrawButton(g,
                                            activeButton.Bounds, ButtonState.Pushed);
                }
                else
                {
                    // 通常のボタンを描画
                    ControlPaint.DrawButton(g,
                                            bounds, ButtonState.Normal);
                }
            }
            else if (appearance == CSharpToolBarAppearance.Flat)
            {
                if (pushed)
                {
                    // フラットボタンが押された状態を描画
                    ControlPaint.DrawBorder3D(g,
                                              activeButton.Bounds, Border3DStyle.SunkenOuter);
                }
            }
            else if (appearance == CSharpToolBarAppearance.VisualStudio)
            {
                if (active)
                {
                    Rectangle rc = button.Bounds;

                    rc.Width  -= 2;
                    rc.Height -= 2;

                    Color color = pushed ?
                                  SystemColors.ControlDark : SystemColors.Highlight;

                    using (Brush b = new SolidBrush(Color.FromArgb(50, color)))
                        g.FillRectangle(b, rc);

                    using (Pen pen = new Pen(color))
                        g.DrawRectangle(pen, rc);
                }
            }

            if (imageList != null &&
                button.ImageIndex >= 0 && button.ImageIndex < imageList.Images.Count)
            {
                // アイコンを描画
                g.DrawImage(imageList.Images[button.ImageIndex], imageRect.X, imageRect.Y);
            }

            if (button.Text.Length > 0)
            {
                // テキストを描画
                g.DrawString(button.Text, Font, SystemBrushes.ControlText, textRect, format);
            }
        }
コード例 #17
0
 public int IndexOf(CSharpToolBarButton button)
 {
     return(innerList.IndexOf(button));
 }
コード例 #18
0
 protected override void OnMouseLeave(EventArgs e)
 {
     base.OnMouseLeave(e);
     UpdateButton(activeButton);
     activeButton = null;
 }
コード例 #19
0
        /// <summary>
        /// 指定したbuttonのサイズを計算
        /// </summary>
        /// <param name="g">文字列幅の計算に使用するGraphicsクラスのインスタンス</param>
        /// <param name="button">サイズを計算するCSharpToolBarButton</param>
        /// <returns>buttonのサイズを返す</returns>
        protected Size GetButtonSize(Graphics g, CSharpToolBarButton button)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (button == null)
            {
                throw new ArgumentNullException("button");
            }

            Size size, space = g.MeasureString(" ", Font).ToSize();

            // セパレータ
            if (button.Style == CSharpToolBarButtonStyle.Separator)
            {
                size       = space;
                size.Width = SystemInformation.Border3DSize.Width;

                if (textAlign == ToolBarTextAlign.Underneath)
                {
                    size.Height += space.Height;
                }
            }
            // 文字、画像ともに設定されていない
            else if (button.Text.Length == 0 && button.ImageIndex == -1)
            {
                size = space;

                if (textAlign == ToolBarTextAlign.Underneath)
                {
                    size.Height += space.Height;
                }
            }
            // 文字のみ設定されている
            else if (button.Text.Length > 0 && button.ImageIndex == -1)
            {
                size = g.MeasureString(button.Text, Font).ToSize();

                if (textAlign == ToolBarTextAlign.Underneath)
                {
                    size.Height += space.Height;
                }
            }
            // 画像のみ設定されている
            else if (button.Text.Length == 0 && button.ImageIndex != -1)
            {
                if (imageList != null)
                {
                    size = imageList.ImageSize;
                }
                else                  // 画像が設定されているのに ImageList が無い場合は空白でサイズ調整
                {
                    size = space;
                }
                if (textAlign == ToolBarTextAlign.Underneath)
                {
                    size.Height += space.Height;
                }
            }
            else
            {
                size = g.MeasureString(button.Text, Font).ToSize();

                // アイコンが存在すればアイコンサイズを足す
                if (imageList != null && button.ImageIndex != -1)
                {
                    Size imageSize = imageList.ImageSize;

                    switch (textAlign)
                    {
                    // テキストがイメージの下に配置される
                    case ToolBarTextAlign.Underneath:
                        size.Width   = Math.Max(size.Width, imageSize.Width);
                        size.Height += imageSize.Height;
                        break;

                    // テキストがイメージの左に配置される
                    case ToolBarTextAlign.Right:
                        size.Width += imageSize.Width;
                        size.Height = Math.Max(size.Height, imageSize.Height);
                        break;
                    }
                }
            }

            size.Width  += _Margin.X + _Margin.Width;
            size.Height += _Margin.Y + _Margin.Height;

            return(size);
        }