Click here to Skip to main content
13,900,017 members
Click here to Skip to main content
Add your own
alternative version

Stats

99.3K views
777 downloads
50 bookmarked
Posted 29 Sep 2006
Licenced CPOL

Vista Internet Explorer 7 Style Navigate Button Control

, 25 Feb 2008
Rate this:
Please Sign up or sign in to vote.
Imitate a Vista Internet Explorer 7 style navigate button control
IE7_style_TravelButton.gif

Introduction

This is a Vista Internet Explorer 7 style navigate button control. Sorry! My English is bad. Please download the source and try it.

The Code Preview

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace Demo.Controls
{
    #region TravelButton
    [DefaultEvent("ItemClicked")]
    public partial class TravelButton : Control
    {
        #region Fields
        [Browsable(true)]
        public event TravelButtonItemClickedEventHandler ItemClicked;
        [Browsable(false)]
        public event EventHandler DropDownMenuChanged;

        private readonly Rectangle BACKGROUND_RECT = 
                         new Rectangle(0, 0, 74, 29);
        private readonly Rectangle BACK_BUTTON_RECT = 
                         new Rectangle(2, 2, 25, 25);
        private readonly Rectangle FORWARD_BUTTON_RECT = 
                         new Rectangle(30, 2, 25, 25); 
        private readonly Rectangle DROPDOWN_ARROW_RECT = 
                         new Rectangle(57, 4, 17, 20);

        private Image _backGround = null;
        private Image _backButton = null;
        private Image _forwardButton = null;

        private ControlState _bButtonState = ControlState.Normal;
        private ControlState _fButtonState = ControlState.Normal;
        private ControlState _ddArrowState = ControlState.Normal;

        private bool _bButtonEnabled = true;
        private bool _fButtonEnabled = true;
        private bool _showMenu = true;

        private string _bToolTip = null;
        private string _fToolTip = null;
        private string _curToolTip = null;

        private ContextMenuStrip _dropDownMenu = null;

        private enum ControlState
        {
            Normal,
            Hover,
            Pressed,
            Disabled
        }
        #endregion

        #region Constructors
        public TravelButton()
        {
            Assembly assembly = Assembly.GetAssembly(typeof(TravelButton));

            _backGround = new Bitmap(
              assembly.GetManifestResourceStream(
              "Demo.Resources.BACKGROUND.PNG"));
            _backButton = new Bitmap(
              assembly.GetManifestResourceStream(
              "Demo.Resources.LEFTBUTTON.PNG"));
            _forwardButton = new Bitmap(
              assembly.GetManifestResourceStream(
              "Demo.Resources.RIGHTBUTTON.PNG"));

            this.DoubleBuffered = true;

            InitializeComponent();
        }
        #endregion

        #region Properties
        [Category("Behavior"), Browsable(true), DefaultValue(true)]
        public bool BackEnabled
        {
            get
            {
                return _bButtonEnabled;
            }
            set
            {
                _bButtonEnabled = value;
                this.Invalidate();
            }
        }

        [Category("Behavior"), Browsable(true), DefaultValue(true)]
        public bool ForwardEnabled
        {
            get
            {
                return _fButtonEnabled;
            }
            set
            {
                _fButtonEnabled = value;
                this.Invalidate();
            }
        }

        [Browsable(true)]
        public string BackToolTip
        {
            get
            {
                return _bToolTip;
            }
            set
            {
                _bToolTip = value;
            }
        }

        [Browsable(true)]
        public string ForwardToolTip
        {
            get
            {
                return _fToolTip;
            }
            set
            {
                _fToolTip = value;
            }
        }

        [Category("Behavior"), Browsable(true), DefaultValue(null)]
        public ContextMenuStrip DropDownMenu
        {
            get
            {
                return _dropDownMenu;
            }
            set
            {
                if (!DesignMode && _dropDownMenu != null)
                    _dropDownMenu.VisibleChanged -= 
                      new EventHandler(DropDownMenu_VisibleChanged);
                
                _dropDownMenu = value;

                if (!DesignMode && _dropDownMenu != null)
                    _dropDownMenu.VisibleChanged += 
                      new EventHandler(DropDownMenu_VisibleChanged);

                if (DropDownMenuChanged != null)
                    DropDownMenuChanged(this, EventArgs.Empty);
            }
        }
        #endregion

        #region Override
        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            if (this.Enabled)
            {
                if (ForwardEnabled)
                    DrawItem(pe.Graphics, TravelButtonItem.BackGround, 
                             _ddArrowState);
                else
                    DrawItem(pe.Graphics, TravelButtonItem.BackGround, 
                             ControlState.Disabled);

                if (BackEnabled)
                    DrawItem(pe.Graphics, TravelButtonItem.BackButton, 
                             _bButtonState);
                else
                    DrawItem(pe.Graphics, TravelButtonItem.BackButton, 
                             ControlState.Disabled);

                if (ForwardEnabled)
                    DrawItem(pe.Graphics, TravelButtonItem.ForwardButton,
                             _fButtonState);
                else
                    DrawItem(pe.Graphics, TravelButtonItem.ForwardButton, 
                             ControlState.Disabled);
            }
            else
            {
                DrawItem(pe.Graphics, TravelButtonItem.BackGround, 
                         ControlState.Disabled);
                DrawItem(pe.Graphics, TravelButtonItem.BackButton, 
                         ControlState.Disabled);
                DrawItem(pe.Graphics, TravelButtonItem.ForwardButton, 
                         ControlState.Disabled);
            }

            base.OnPaint(pe);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            _bButtonState = ControlState.Normal;
            _fButtonState = ControlState.Normal;

            if (_dropDownMenu != null && _dropDownMenu.Visible)
                _ddArrowState = ControlState.Pressed;
            else
                _ddArrowState = ControlState.Normal;

            this.Invalidate();

            _showMenu = true;
            _curToolTip = null;
            //toolTip.Hide(this);
        }

        protected override void OnMouseDown(MouseEventArgs me)
        {
            base.OnMouseDown(me);

            if (this.Enabled)
            {
                if (me.Button == MouseButtons.Left)
                {
                    if (BackEnabled && 
                        BACK_BUTTON_RECT.Contains(me.X, me.Y))
                    {
                        _bButtonState = ControlState.Pressed;
                        _fButtonState = ControlState.Normal;
                        _ddArrowState = ControlState.Normal;
                        _showMenu = true;
                    }
                    else if (ForwardEnabled && 
                             FORWARD_BUTTON_RECT.Contains(me.X, me.Y))
                    {
                        _bButtonState = ControlState.Normal;
                        _fButtonState = ControlState.Pressed;
                        _ddArrowState = ControlState.Normal;
                        _showMenu = true;
                    }
                    else if (ForwardEnabled && 
                             DROPDOWN_ARROW_RECT.Contains(me.X, me.Y))
                    {
                        _bButtonState = ControlState.Normal;
                        _fButtonState = ControlState.Normal;

                        if (_dropDownMenu != null)
                        {
                            if (_showMenu)
                            {
                                _dropDownMenu.Show(this, 
                                  new Point(BACKGROUND_RECT.X, 
                                  BACKGROUND_RECT.Height),
                                  ToolStripDropDownDirection.Default);
                                _ddArrowState = ControlState.Pressed;
                            }
                            else
                            {
                                _ddArrowState = ControlState.Hover;
                            }
                            _showMenu = !_showMenu;
                        }
                        else
                        {
                            _ddArrowState = ControlState.Hover;
                            _showMenu = true;
                        }
                    }
                    else
                    {
                        _bButtonState = ControlState.Normal;
                        _fButtonState = ControlState.Normal;
                        _ddArrowState = ControlState.Normal;
                        _showMenu = true;
                    }

                    this.Invalidate();
                }
            }
        }

        protected override void OnMouseMove(MouseEventArgs me)
        {
            base.OnMouseMove(me);

            if (this.Enabled && !this.Capture)
            {
                if (BackEnabled && BACK_BUTTON_RECT.Contains(me.X, me.Y))
                {
                    _bButtonState = ControlState.Hover;
                    _fButtonState = ControlState.Normal;
                    _ddArrowState = ControlState.Normal;

                    SetToolTip(TravelButtonItem.BackButton);
                }
                else if (ForwardEnabled && FORWARD_BUTTON_RECT.Contains(me.X, me.Y))
                {
                    _bButtonState = ControlState.Normal;
                    _fButtonState = ControlState.Hover;
                    _ddArrowState = ControlState.Normal;

                    SetToolTip(TravelButtonItem.ForwardButton);
                }
                else if (ForwardEnabled && DROPDOWN_ARROW_RECT.Contains(me.X, me.Y))
                {
                    _bButtonState = ControlState.Normal;
                    _fButtonState = ControlState.Normal;
                    _ddArrowState = ControlState.Hover;

                    SetToolTip(TravelButtonItem.DropDownArrow);
                }
                else
                {
                    _bButtonState = ControlState.Normal;
                    _fButtonState = ControlState.Normal;
                    _ddArrowState = ControlState.Normal;

                    SetToolTip(TravelButtonItem.BackGround);
                }

                this.Invalidate();
            }
        }

        protected override void OnMouseDoubleClick(MouseEventArgs me)
        {
            if (DROPDOWN_ARROW_RECT.Contains(me.X, me.Y))
                return;
            else
                base.OnMouseDoubleClick(me);
        }

        protected override void OnClick(EventArgs e)
        {
            if (this.Enabled)
            {
                Point p = this.PointToClient(Control.MousePosition);
                TravelButtonItem item = TravelButtonItem.BackGround;

                if (BackEnabled && BACK_BUTTON_RECT.Contains(p.X, p.Y))
                {
                    item = TravelButtonItem.BackButton;
                    _bButtonState = ControlState.Hover;
                }
                else if (ForwardEnabled && FORWARD_BUTTON_RECT.Contains(p.X, p.Y))
                {
                    item = TravelButtonItem.ForwardButton;
                    _fButtonState = ControlState.Hover;
                }
                else if (ForwardEnabled && DROPDOWN_ARROW_RECT.Contains(p.X, p.Y))
                {
                    item = TravelButtonItem.DropDownArrow;
                    //_dropDownArrowState = ControlState.Hover;
                }

                this.Invalidate();

                if (item != TravelButtonItem.BackGround && 
                    ItemClicked != null)
                    ItemClicked(this, new TravelButtonItemClickedEventArgs(item));
            }

            base.OnClick(e);
        }
        #endregion

        #region Pulbic
        public void SetEnabled(TravelButtonItem item, bool enable)
        {
            if (item == TravelButtonItem.BackButton)
                this.BackEnabled = enable;
            else if (item == TravelButtonItem.ForwardButton
                || item == TravelButtonItem.DropDownArrow)
                this.ForwardEnabled = enable;
        }

        public void SetToolTip(TravelButtonItem item, string tip)
        {
            if (item == TravelButtonItem.BackButton)
                this.BackToolTip = tip;
            else if (item == TravelButtonItem.ForwardButton
                || item == TravelButtonItem.DropDownArrow)
                this.ForwardToolTip = tip;
        }
        #endregion

        #region Private
        private void SetToolTip(TravelButtonItem item)
        {
            if (item == TravelButtonItem.BackButton)
            {
                if (_curToolTip != _bToolTip)
                {
                    _curToolTip = _bToolTip;
                    toolTip.SetToolTip(this, _curToolTip);
                }
            }
            else if (item == TravelButtonItem.ForwardButton)
            {
                if (_curToolTip != _fToolTip)
                {
                    _curToolTip = _fToolTip;
                    toolTip.SetToolTip(this, _curToolTip);
                }
            }
            else
            {
                _curToolTip = null;
                toolTip.Hide(this);
            }
        }

        private void DropDownMenu_VisibleChanged(object sender, EventArgs e)
        {
            ContextMenuStrip menu = sender as ContextMenuStrip;
            if (menu != null)
            {
                if (!menu.Visible)
                {
                    _ddArrowState = ControlState.Normal;
                }

                this.Invalidate();
            }
        }

        private void DrawItem(Graphics g, TravelButtonItem item, 
                              ControlState state)
        {
            Rectangle srcRect;
            switch (item)
            {
                case TravelButtonItem.BackButton:
                    srcRect = new Rectangle(new Point(0, 0),
                              BACK_BUTTON_RECT.Size);
                    break;
                case TravelButtonItem.ForwardButton:
                    srcRect = new Rectangle(new Point(0, 0), 
                              FORWARD_BUTTON_RECT.Size);
                    break;
                default:
                    srcRect = BACKGROUND_RECT;
                    break;
            }

            int xOffset = 0;
            switch (state)
            {
                case ControlState.Normal:
                    xOffset = 0;
                    break;
                case ControlState.Pressed:
                    xOffset = srcRect.Width;
                    break;
                case ControlState.Hover:
                    xOffset = srcRect.Width * 2;
                    break;
                case ControlState.Disabled:
                    xOffset = srcRect.Width * 3;
                    break;
            }

            srcRect.X = xOffset;

            if (item == TravelButtonItem.BackButton)
            {
                g.DrawImage(_backButton, BACK_BUTTON_RECT, srcRect, 
                            GraphicsUnit.Pixel);
            }
            else if (item == TravelButtonItem.ForwardButton)
            {
                g.DrawImage(_forwardButton, FORWARD_BUTTON_RECT, 
                            srcRect, GraphicsUnit.Pixel);
            }
            else if (item == TravelButtonItem.BackGround)
            {
                g.DrawImage(_backGround, BACKGROUND_RECT, 
                            srcRect, GraphicsUnit.Pixel);
            }
        }
        #endregion
    }
    #endregion

    #region Other
    public delegate void TravelButtonItemClickedEventHandler(
           object sender, TravelButtonItemClickedEventArgs e);

    public enum TravelButtonItem
    {
        BackGround,
        BackButton,
        ForwardButton,
        DropDownArrow
    }
    
    public class TravelButtonItemClickedEventArgs : EventArgs
    {
        private readonly TravelButtonItem _item = 
                         TravelButtonItem.BackGround;

        public TravelButtonItemClickedEventArgs(TravelButtonItem clickedItem)
                                                                     : base()
        {
            _item = clickedItem;
        }

        public TravelButtonItem ClickedItem
        {
            get
            {
                return _item;
            }
        }
    }
    #endregion
}

History

  • 2008-2-25: Added ToolStripButton and suboptimal support for Gradient background

License

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

Share

About the Author

YuReng
Web Developer
China China
cool chappy.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionmetro style Pin
Chris Anders9-Aug-14 3:23
memberChris Anders9-Aug-14 3:23 
Questionhide arrow Pin
Chris Anders4-Aug-14 23:08
memberChris Anders4-Aug-14 23:08 
GeneralVista don't allow AppInit_Dlls to inject Pin
sunil_20084-Sep-09 19:41
membersunil_20084-Sep-09 19:41 
Generalbackbuttononly should hide the background placeholder so can use it to mimic vista wizard Pin
Member 71792814-Jul-09 15:44
memberMember 71792814-Jul-09 15:44 
GeneralA request... Pin
Johnny J.5-Feb-08 4:51
professionalJohnny J.5-Feb-08 4:51 
QuestionResources Pin
elronalds9-May-07 14:30
memberelronalds9-May-07 14:30 
AnswerRe: Resources Pin
YuReng7-Jun-07 16:37
professionalYuReng7-Jun-07 16:37 
AnswerRe: Resources Pin
The Cake of Deceit13-Aug-08 5:28
memberThe Cake of Deceit13-Aug-08 5:28 
QuestionGreat! Support for Gradient background? Pin
Dr. Bonne19-Oct-06 23:19
memberDr. Bonne19-Oct-06 23:19 
GeneralVista button request Pin
TheCardinal29-Sep-06 15:49
memberTheCardinal29-Sep-06 15:49 
GeneralRe: Vista button request Pin
YuReng6-Jan-07 16:26
professionalYuReng6-Jan-07 16:26 
Generallearn Pin
DannSmith29-Sep-06 4:20
memberDannSmith29-Sep-06 4:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 25 Feb 2008
Article Copyright 2006 by YuReng
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid