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)