Simple DropDownButton (Winform)

Source: Internet
Author: User

 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.Mo Usebuttons.left) {if (M_buttonrect.contains (e.location)) {is                Pressleft = 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.Mous Ebuttons.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 s            Tatel = 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.Pres            Sed } 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 (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_combobutton            Rect.y, M_combobuttonrect.height); M_combomenu.righttoleft = SysTem.            Windows.Forms.RightToLeft.Yes;        M_combomenu.show (this, contextmenupoint); }    }

  

Simple DropDownButton (Winform)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.