博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的DropDownButton(Winform)
阅读量:7099 次
发布时间:2019-06-28

本文共 5157 字,大约阅读时间需要 17 分钟。

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);        }    }

  

转载于:https://www.cnblogs.com/systemview/p/5594282.html

你可能感兴趣的文章
android 循环操作
查看>>
Promise & Deferred objects in JavaScript Pt.1: Theory and Semantics.
查看>>
Joyoi花店橱窗(原tyvj1124)
查看>>
JavaMail基础案例开发
查看>>
被称"硬盘杀手"的几个win7系统服务如何关闭(转)
查看>>
C# 存储过程
查看>>
软件体系结构的第二次实验
查看>>
无聊记记
查看>>
ODI Scenario 场景
查看>>
操作JSON对象
查看>>
iOS 模态视图,视图之间的切换
查看>>
iptables
查看>>
.NET自动识别GB2312与UTF-8编码的文件
查看>>
Linux下apache日志分析与状态查看方法
查看>>
hdu2412(树形dp)
查看>>
js返回函数, 函数名后带多个括号的用法及join()的注意事项
查看>>
【NOIP2007】矩阵取数
查看>>
关于VIM在Win10下的无意义折腾
查看>>
ibatis example Class 使用
查看>>
android的触摸事件(转)
查看>>