MenuButton.cs (2993B)
1 // 2 // Copyright 2020 Electronic Arts Inc. 3 // 4 // The Command & Conquer Map Editor and corresponding source code is free 5 // software: you can redistribute it and/or modify it under the terms of 6 // the GNU General Public License as published by the Free Software Foundation, 7 // either version 3 of the License, or (at your option) any later version. 8 9 // The Command & Conquer Map Editor and corresponding source code is distributed 10 // in the hope that it will be useful, but with permitted additional restrictions 11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 12 // distributed with this program. You should have received a copy of the 13 // GNU General Public License along with permitted additional restrictions 14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection 15 using System.ComponentModel; 16 using System.Drawing; 17 using System.Drawing.Drawing2D; 18 using System.Windows.Forms; 19 20 namespace MobiusEditor.Controls 21 { 22 public partial class MenuButton : Button 23 { 24 public const int DefaultSplitWidth = 20; 25 26 [DefaultValue(null), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 27 public ContextMenuStrip Menu { get; set; } 28 29 [DefaultValue(DefaultSplitWidth), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 30 public int SplitWidth { get; set; } = DefaultSplitWidth; 31 32 public MenuButton() 33 { 34 InitializeComponent(); 35 } 36 37 protected override void OnMouseDown(MouseEventArgs mevent) 38 { 39 var splitRect = new Rectangle(Width - SplitWidth, 0, SplitWidth, Height); 40 41 if ((Menu != null) && (mevent.Button == MouseButtons.Left) && splitRect.Contains(mevent.Location)) 42 { 43 Menu.Show(this, 0, Height); 44 } 45 else 46 { 47 base.OnMouseDown(mevent); 48 } 49 } 50 51 protected override void OnPaint(PaintEventArgs pevent) 52 { 53 base.OnPaint(pevent); 54 55 if ((Menu != null) && (SplitWidth > 0)) 56 { 57 int arrowX = ClientRectangle.Width - 14; 58 int arrowY = ClientRectangle.Height / 2 - 1; 59 60 var arrowBrush = Enabled ? SystemBrushes.ControlText : SystemBrushes.ButtonShadow; 61 var arrows = new[] { new Point(arrowX, arrowY), new Point(arrowX + 7, arrowY), new Point(arrowX + 3, arrowY + 4) }; 62 pevent.Graphics.FillPolygon(arrowBrush, arrows); 63 64 int lineX = ClientRectangle.Width - SplitWidth; 65 int lineYFrom = arrowY - 4; 66 int lineYTo = arrowY + 8; 67 using (var separatorPen = new Pen(Brushes.DarkGray) { DashStyle = DashStyle.Dot }) 68 { 69 pevent.Graphics.DrawLine(separatorPen, lineX, lineYFrom, lineX, lineYTo); 70 } 71 } 72 } 73 } 74 }