THE_LORD

NButton

Sep 6th, 2017
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.ComponentModel.Design;
  5. using System.Drawing;
  6. using System.Drawing.Text;
  7. using System.Windows.Forms;
  8.  
  9. [System.ComponentModel.Designer(typeof(NButtonDesigner))]
  10. internal class NButton : Control
  11. {
  12.     private Color _ForeColor = Color.White;
  13.     private Color _BaseColor = Color.Crimson;
  14.     private Color _BorderColor = Color.Black;
  15.     public string _ButtonText = "Button";
  16.     public NButton()
  17.     {
  18.         SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
  19.         DoubleBuffered = true;
  20.         UpdateStyles();
  21.         BackColor = Color.Transparent;
  22.         Font = new Font("Segoe UI Light", 10);
  23.     }
  24.  
  25.     public new Color ForeColor
  26.     {
  27.         get { return _ForeColor; }
  28.         set { _ForeColor = value; Invalidate(); }
  29.     }
  30.  
  31.     public Color BaseColor
  32.     {
  33.         get { return _BaseColor; }
  34.         set { _BaseColor = value; Invalidate(); }
  35.     }
  36.  
  37.     public Color BorderColor
  38.     {
  39.         get { return _BorderColor; }
  40.         set { _BorderColor = value; Invalidate(); }
  41.     }
  42.  
  43.     protected override void OnPaint(PaintEventArgs pevent)
  44.     {
  45.         Graphics g = pevent.Graphics;
  46.         g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  47.         Rectangle r = new Rectangle(0, 0, Width - 1, Height - 1);
  48.  
  49.         using (SolidBrush BG = new SolidBrush(BaseColor))
  50.         {
  51.             using (SolidBrush F = new SolidBrush(ForeColor))
  52.             {
  53.                 using (Pen BD = new Pen(BorderColor))
  54.                 {
  55.                     using (StringFormat SF = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
  56.                     {
  57.                         g.FillRectangle(BG, r);
  58.                         g.DrawRectangle(BD, r);
  59.                         g.DrawString(Text, Font, F, r, SF);
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67. internal class NButtonDesigner : System.Windows.Forms.Design.ControlDesigner
  68. {
  69.     private readonly string[] _PropertiesToStay = { "ForeColor", "BaseColor", "Size", "Location","BorderColor","Font", "Text", "Name","TabIndex" };
  70.    
  71.     protected override void PostFilterProperties(IDictionary properties)
  72.     {
  73.         List<string> _ListToRemove = new List<string>();
  74.  
  75.         foreach (var item in properties.Keys)
  76.         {
  77.             if (!((IList) _PropertiesToStay).Contains(item))
  78.             {
  79.                 _ListToRemove.Add(item.ToString());
  80.             }
  81.         }
  82.  
  83.         foreach (var property in _ListToRemove)
  84.         {
  85.             properties.Remove(property);
  86.         }
  87.         base.PostFilterProperties(properties);
  88.     }
  89.  
  90.     private DesignerActionListCollection actionListCollection;
  91.  
  92.     public override DesignerActionListCollection ActionLists
  93.     {
  94.         get
  95.         {
  96.             if (actionListCollection == null)
  97.             {
  98.                 actionListCollection = new DesignerActionListCollection();
  99.                 actionListCollection.Add(new NButtonActionList(Component));
  100.             }
  101.  
  102.             return actionListCollection;
  103.         }
  104.     }
  105.  
  106.  
  107. }
  108.  
  109. internal class NButtonActionList : DesignerActionList
  110. {
  111.     private readonly NButton _NButton;
  112.  
  113.     public NButtonActionList(IComponent component) : base(component)
  114.     {
  115.         _NButton = (NButton) component;
  116.     }
  117.  
  118.     public Color ForeColor
  119.     {
  120.         get { return _NButton.ForeColor; }
  121.         set { _NButton.ForeColor = value; }
  122.     }
  123.  
  124.     public Color BaseColor
  125.     {
  126.         get { return _NButton.BaseColor; }
  127.         set { _NButton.BaseColor = value; }
  128.     }
  129.  
  130.     public Color BorderColor
  131.     {
  132.         get { return _NButton.BorderColor; }
  133.         set { _NButton.BorderColor = value; }
  134.     }
  135.  
  136.     public string Text
  137.     {
  138.         get { return _NButton.Text; }
  139.         set { _NButton.Text = value; }
  140.     }
  141.  
  142.     public Size Size
  143.     {
  144.         get { return _NButton.Size; }
  145.         set { _NButton.Size = value; }
  146.     }
  147.  
  148.     public Point Location
  149.     {
  150.         get { return _NButton.Location; }
  151.         set { _NButton.Location = value; }
  152.     }
  153.  
  154.  
  155.     public void OriginalSize()
  156.     {
  157.         _NButton.Size = new Size(190, 50);
  158.     }
  159.  
  160.  
  161.  
  162.     public override DesignerActionItemCollection GetSortedActionItems()
  163.     {
  164.         DesignerActionItemCollection items = new DesignerActionItemCollection
  165.         {        
  166.             //Header Name
  167.             new DesignerActionHeaderItem("Appearance"),
  168.             // 1st BaseColor is property name (it must be exist).
  169.             // 2nd BaseColor is the display name for the property.
  170.             // Color is the category name we added from above.
  171.             //"Gets or sets the background color."  is the description for the property.
  172.             new DesignerActionPropertyItem("BaseColor", "BaseColor", "Appearance", "Gets or sets the background color."),
  173.             new DesignerActionPropertyItem("BorderColor", "BorderColor", "Appearance", "Gets or sets the border color."),
  174.             new DesignerActionPropertyItem("ForeColor", "ForeColor", "Appearance", "Gets or sets the foreground color."),
  175.             new DesignerActionPropertyItem("Text", "Text", "Appearance", "Gets or sets the text associated with the control."),
  176.  
  177.             new DesignerActionHeaderItem("Layout"),
  178.             new DesignerActionPropertyItem("Location", "Location", "Layout", "Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container."),
  179.             new DesignerActionPropertyItem("Size", "Size", "Layout", "Gets or sets the height and width of the control."),
  180.  
  181.             new DesignerActionHeaderItem("Misc"),
  182.             new DesignerActionMethodItem(this, "OriginalSize","Set Original Size", "Misc", "Gets or sets the height and width of the control.")
  183.  
  184.         };
  185.  
  186.         return items;
  187.     }
  188.  
  189. }
Add Comment
Please, Sign In to add comment