王安禹的blog

导航

简单的DropDownButton(Winform)

 public class DropDownButton : System.Windows.Forms.Control
    {
        private System.ComponentModel.Container components = null;

        private bool isHover = false;
        private bool isPressLeft = false;
        private bool isPressRight = false;


        public event EventHandler ClickEvent;

        public Menu.MenuItemCollection MenuItems { get; set; }

        public string Caption { get; set; }

        public DropDownButton()
        {
            InitializeComponent();

            this.RefreshButtonsRects();

            m_comboMenu = new ContextMenu();
            MenuItems = new Menu.MenuItemCollection(m_comboMenu);

            this.SizeChanged += new EventHandler(DropDownButton_SizeChanged);
            this.MouseUp += new MouseEventHandler(DropDownButton_MouseUp);
        }


        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void OnMouseHover(EventArgs e)
        {
            this.isHover = true;
            this.Invalidate();
            base.OnMouseHover(e);
        }
        
        protected override void OnMouseLeave(EventArgs e)
        {
            this.isHover = false;
            this.Invalidate();
            base.OnMouseLeave(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (m_buttonRect.Contains(e.Location))
                {
                    isPressLeft = true;
                }
                if (m_comboButtonRect.Contains(e.Location))
                {
                    isPressRight = true;
                }
                this.Invalidate();
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                isPressLeft = false;
                isPressRight = false;
                this.Invalidate();
            }
            base.OnMouseUp(e);
        }
        private void InitializeComponent()
        {

        }

        private const int COMBOBUTTON_WIDTH = 20;
        private Rectangle m_buttonRect;
        private Rectangle m_comboButtonRect;
        private ContextMenu m_comboMenu;

        protected override void OnPaint(PaintEventArgs pe)
        {
            System.Windows.Forms.VisualStyles.PushButtonState stateL = System.Windows.Forms.VisualStyles.PushButtonState.Normal;
            System.Windows.Forms.VisualStyles.PushButtonState stateR = System.Windows.Forms.VisualStyles.PushButtonState.Normal;

            if (isHover)
            {
                stateL = System.Windows.Forms.VisualStyles.PushButtonState.Hot;
                stateR = System.Windows.Forms.VisualStyles.PushButtonState.Hot;
            }

            if (isPressLeft)
            {
                stateL = System.Windows.Forms.VisualStyles.PushButtonState.Pressed;
            }

            if (isPressRight)
            {
                stateR = System.Windows.Forms.VisualStyles.PushButtonState.Pressed;
            }

            this.CreateGraphics().DrawRectangle(new Pen(SystemBrushes.Control), this.ClientRectangle);


            ButtonRenderer.DrawButton(this.CreateGraphics(),
                m_buttonRect, Caption,
                new Font(this.Font, FontStyle.Regular), false,
                stateL);

            ButtonRenderer.DrawButton(this.CreateGraphics(),
                m_comboButtonRect, "v",
                new Font(this.Font, FontStyle.Regular), false,
                stateR);

            base.OnPaint(pe);
        }

        private void DropDownButton_SizeChanged(object sender, EventArgs e)
        {
            this.RefreshButtonsRects();
            this.Invalidate();
        }

        private void RefreshButtonsRects()
        {
            m_buttonRect = new Rectangle(
                new Point(0, 0),
                new Size(this.Width - COMBOBUTTON_WIDTH + 2, this.Height)
                );
            m_comboButtonRect = new Rectangle(
                new Point(this.Width - COMBOBUTTON_WIDTH, 0),
                new Size(COMBOBUTTON_WIDTH, this.Height)
                );
        }


        private void DropDownButton_MouseUp(object sender, MouseEventArgs e)
        {
            Point clickedPoint = new Point(e.X, e.Y);

            if (m_comboButtonRect.Contains(clickedPoint))
            {
                OnComboButtonClicked();
            }
            else
            {
                OnButtonClicked(e);
            }
        }

        private void OnButtonClicked(MouseEventArgs e)
        {
            if (this.ClickEvent != null)
            {
                ClickEvent(this, e);
            }
        }

        private void OnComboButtonClicked()
        {
            Point contextMenuPoint = new Point(m_comboButtonRect.Y, m_comboButtonRect.Height);
            //m_comboMenu.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            m_comboMenu.Show(this, contextMenuPoint);
        }
    }

  

posted on 2016-06-17 15:57  王安禹的blog  阅读(1931)  评论(0编辑  收藏  举报