Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / Windows Forms

ButtonBar Control using .NET

Rate me:
Please Sign up or sign in to vote.
4.95/5 (43 votes)
3 Dec 2009CPOL5 min read 64.4K   5.5K   104  
Themed ButtonBar control supporting custom draw with full Designer support
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using ButtonBarsControl.Design.Entity;
using ButtonBarsControl.Design.Enums;
using ButtonBarsControl.Design.Layout;

namespace ButtonBarTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            cbxTheme.SelectedIndex = 0;
        }

        private void bBarMain_CustomDrawBackGround(object sender, DrawBackGroundEventArgs e)
        {
            if (!chkUseCustomDrawBackground.Checked)
                return;
            e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), e.Bounds);
            e.Graphics.DrawString("CustDrawBackground", Font, new SolidBrush(Color.LightYellow), 0, bBarMain.Height/2);
            e.DrawBorder();
            e.Handeled = true;
        }

        private void bBarMain_CustomDrawItems(object sender, DrawItemsEventArgs e)
        {
            if (!chkCustomDrawItems.Checked)
                return;
            Color c = Color.Transparent;
            switch (e.State)
            {
                case State.Disabled:
                    c = Color.Gray;
                    break;
                case State.Hover:
                    c = Color.Green;
                    break;
                case State.Pressed:
                    c = Color.Red;
                    break;
                case State.Normal:
                    c = Color.Blue;
                    break;
                case State.Selected:
                    c = Color.BlueViolet;
                    break;
                case State.SelectedHover:
                    c = Color.Green;
                    break;
            }
            DrawString(e.Graphics, e.Bounds, "CustomDrawItem", Color.LightYellow, e.Item.Appearance.AppearenceText, true);
            DrawString(e.Graphics, e.Bounds, e.Item.Caption, c, e.Item.Appearance.AppearenceText, e.Bar.UseMnemonic);
            e.Handeled = true;
        }

        private void DrawString(Graphics g, Rectangle rectangle, string text, Color color, AppearenceText app,
                                bool showShort)
        {
            if (rectangle.IsEmpty)
                return;
            var format = new StringFormat();
            format.Trimming = app.Trimming;
            format.Alignment = app.Alignment;
            format.LineAlignment = app.LineAlignment;
            if (showShort)
            {
                format.HotkeyPrefix = HotkeyPrefix.Show;
            }
            g.DrawString(text, app.Font, new SolidBrush(color), rectangle, format);
        }

        private void chkUseCustomDrawBackground_CheckedChanged(object sender, EventArgs e)
        {
            bBarMain.Invalidate();
        }

        private void chkCustomDrawItems_CheckedChanged(object sender, EventArgs e)
        {
            bBarMain.Invalidate();
        }

        private void chkUseTheme_CheckedChanged(object sender, EventArgs e)
        {
            cbxTheme.Enabled = chkUseTheme.Checked;
            bBarMain.ThemeProperty.UseTheme = chkUseTheme.Checked;
            bBarMain.Invalidate();
        }

        private void cbxTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            bBarMain.ThemeProperty.ColorScheme =
                (ColorScheme) Enum.Parse(typeof (ColorScheme), cbxTheme.SelectedItem.ToString());
            bBarMain.Invalidate();
        }

        private void chkShowBorders_CheckedChanged(object sender, EventArgs e)
        {
            bBarApp.ShowBorders = chkShowBorders.Checked;
            bBarApp.Invalidate();
        }

        private void buttonBar3_Click(object sender, EventArgs e)
        {
            HitTestInfo test = bBarHit.HitTest(bBarHit.PointToClient(MousePosition));
            richTextBox1.Text = "HitArea = " + test.Area + ", ButtonIndex = " + test.ButtonIndex + Environment.NewLine +
                                richTextBox1.Text;
        }

        private void chkRound_CheckedChanged(object sender, EventArgs e)
        {
            if (chkRound.Checked)
            {
                bBarApp.Appearance.Bar.CornerRadius = 8;
                bBarApp.Appearance.Bar.AppearanceBorder.CornerShape.Assign(new CornerShape(CornerType.Round));
                bBarApp.Padding = new Padding(6);
            }
            else
            {
                bBarApp.Appearance.Bar.CornerRadius = 0;
                bBarApp.Appearance.Bar.AppearanceBorder.CornerShape.Assign(new CornerShape(CornerType.Square));
                bBarApp.Padding = new Padding(4);
            }
        }

        private void chkShadow_CheckedChanged(object sender, EventArgs e)
        {
            if (chkShadow.Checked)
            {
                bBarApp.Appearance.Item.AppearenceText.Xshift = 1;
                bBarApp.Appearance.Item.AppearenceText.Yshift = 1;
            }
            else
            {
                bBarApp.Appearance.Item.AppearenceText.Xshift = 0;
                bBarApp.Appearance.Item.AppearenceText.Yshift = 0;
            }
            bBarApp.Invalidate();
        }

        private void chkDisabled_CheckedChanged(object sender, EventArgs e)
        {
            bBarApp.Enabled = !chkDisabled.Checked;
        }

        private void chkChangeSpacing_CheckedChanged(object sender, EventArgs e)
        {
            bBarApp.Spacing = chkChangeSpacing.Checked ? 8 : 4;
            bBarApp.Invalidate();
        }

        private void chkAlignment_CheckedChanged(object sender, EventArgs e)
        {
            bBarApp.Appearance.Item.AppearenceText.Alignment = chkAlignment.Checked
                                                                   ? StringAlignment.Near
                                                                   : StringAlignment.Center;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions