Advertisement
Yassine_Abbani

circular Red [button], [Costume Control]

May 22nd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1.  
  2. #region Import
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8. #endregion
  9.  
  10.  
  11.  
  12. public class Ce_SocialButton : Control
  13. {
  14.  
  15.     #region  Variables
  16.  
  17.     private Image _Image;
  18.     private Size _ImageSize;
  19.     private Color EllipseColor; // VBConversions Note: Initial value cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
  20.  
  21.     #endregion
  22.     #region  Properties
  23.  
  24.     public Image Image
  25.     {
  26.         get
  27.         {
  28.             return _Image;
  29.         }
  30.         set
  31.         {
  32.             if (value == null)
  33.             {
  34.                 _ImageSize = Size.Empty;
  35.             }
  36.             else
  37.             {
  38.                 _ImageSize = value.Size;
  39.             }
  40.  
  41.             _Image = value;
  42.             Invalidate();
  43.         }
  44.     }
  45.  
  46.     protected Size ImageSize
  47.     {
  48.         get
  49.         {
  50.             return _ImageSize;
  51.         }
  52.     }
  53.  
  54.     #endregion
  55.     #region  EventArgs
  56.  
  57.     protected override void OnResize(EventArgs e)
  58.     {
  59.         base.OnResize(e);
  60.         this.Size = new Size(54, 54);
  61.     }
  62.  
  63.     protected override void OnMouseEnter(EventArgs e)
  64.     {
  65.         base.OnMouseEnter(e);
  66.         EllipseColor = Color.FromArgb(181, 41, 42);
  67.         Refresh();
  68.     }
  69.     protected override void OnMouseLeave(EventArgs e)
  70.     {
  71.         base.OnMouseLeave(e);
  72.         EllipseColor = Color.FromArgb(66, 76, 85);
  73.         Refresh();
  74.     }
  75.  
  76.     protected override void OnMouseDown(MouseEventArgs e)
  77.     {
  78.         base.OnMouseDown(e);
  79.         EllipseColor = Color.FromArgb(153, 34, 34);
  80.         Focus();
  81.         Refresh();
  82.     }
  83.     protected override void OnMouseUp(MouseEventArgs e)
  84.     {
  85.         base.OnMouseUp(e);
  86.         EllipseColor = Color.FromArgb(181, 41, 42);
  87.         Refresh();
  88.     }
  89.  
  90.     #endregion
  91.     #region  Image Designer
  92.  
  93.     private static PointF ImageLocation(StringFormat SF, SizeF Area, SizeF ImageArea)
  94.     {
  95.         PointF MyPoint = new PointF();
  96.         switch (SF.Alignment)
  97.         {
  98.             case StringAlignment.Center:
  99.                 MyPoint.X = (float)((Area.Width - ImageArea.Width) / 2);
  100.                 break;
  101.         }
  102.  
  103.         switch (SF.LineAlignment)
  104.         {
  105.             case StringAlignment.Center:
  106.                 MyPoint.Y = (float)((Area.Height - ImageArea.Height) / 2);
  107.                 break;
  108.         }
  109.         return MyPoint;
  110.     }
  111.  
  112.     private StringFormat GetStringFormat(ContentAlignment _ContentAlignment)
  113.     {
  114.         StringFormat SF = new StringFormat();
  115.         switch (_ContentAlignment)
  116.         {
  117.             case ContentAlignment.MiddleCenter:
  118.                 SF.LineAlignment = StringAlignment.Center;
  119.                 SF.Alignment = StringAlignment.Center;
  120.                 break;
  121.         }
  122.         return SF;
  123.     }
  124.  
  125.     #endregion
  126.  
  127.     public Ce_SocialButton()
  128.     {
  129.         DoubleBuffered = true;
  130.         EllipseColor = Color.FromArgb(66, 76, 85);
  131.     }
  132.  
  133.     protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  134.     {
  135.         Graphics G = e.Graphics;
  136.         G.Clear(Parent.BackColor);
  137.         G.SmoothingMode = SmoothingMode.HighQuality;
  138.  
  139.         PointF ImgPoint = ImageLocation(GetStringFormat(ContentAlignment.MiddleCenter), Size, ImageSize);
  140.         G.FillEllipse(new SolidBrush(EllipseColor), new Rectangle(0, 0, 53, 53));
  141.  
  142.         // HINTS:
  143.         // The best size for the drawn image is 32x32\
  144.         // The best matching color of drawn image is (RGB: 31, 40, 49)
  145.         if (Image != null)
  146.         {
  147.             G.DrawImage(_Image, (int)ImgPoint.X, (int)ImgPoint.Y, ImageSize.Width, ImageSize.Height);
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement