Advertisement
Yassine_Abbani

Oriented [Label], [Custom Control]

May 22nd, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.98 KB | None | 0 0
  1. #region Import
  2. using System;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Text;
  7. using System.Drawing.Drawing2D;
  8.  
  9. #endregion
  10.  
  11. #region Copyright
  12. // Creator: Cheat Eye
  13. // CEO: Yassine Abbani
  14. // Link: https://www.facebook.com/YassineAbbani.user
  15. // Version: 1.0
  16. #endregion
  17. #region Oriented Label
  18.  
  19. /// <summary>
  20. /// This is a lable, in which you can set the text in any direction/angle
  21. /// </summary>
  22.  
  23. #region Properties
  24.  
  25. //Orientation of the text
  26.  
  27. public enum Orientation
  28. {
  29.     Circle,
  30.     Arc,
  31.     Rotate
  32. }
  33.  
  34. public enum Direction
  35. {
  36.     Clockwise,
  37.     AntiClockwise
  38. }
  39.  
  40. #endregion
  41.  
  42. public class Ce_OrientedLabel : System.Windows.Forms.Label
  43. {
  44.  
  45.     #region Variables
  46.  
  47.     private double rotationAngle;
  48.     private string text;
  49.     private Orientation textOrientation;
  50.     private Direction textDirection;
  51.  
  52.  
  53.     #endregion
  54.  
  55.     #region Constructor
  56.  
  57.     public Ce_OrientedLabel()
  58.     {
  59.         //Setting the initial condition.
  60.         rotationAngle = 0d;
  61.         textOrientation = Orientation.Rotate;
  62.         this.Size = new Size(105, 12);
  63.         Font = new Font("Century Gothic", 11);
  64.         ForeColor = Color.FromArgb(31, 73, 94);
  65.         BackColor = Color.Transparent;
  66.  
  67.     }
  68.  
  69.     #endregion
  70.  
  71.     #region Properties
  72.  
  73.     [Description("Rotation Angle"), Category("Appearance")]
  74.     public double RotationAngle
  75.     {
  76.         get
  77.         {
  78.             return rotationAngle;
  79.         }
  80.         set
  81.         {
  82.  
  83.             rotationAngle = value;
  84.             this.Invalidate();
  85.         }
  86.     }
  87.  
  88.     [Description("Kind of Text Orientation"), Category("Appearance")]
  89.     public Orientation TextOrientation
  90.     {
  91.         get
  92.         {
  93.             return textOrientation;
  94.         }
  95.         set
  96.         {
  97.  
  98.             textOrientation = value;
  99.             this.Invalidate();
  100.         }
  101.     }
  102.  
  103.     [Description("Direction of the Text"), Category("Appearance")]
  104.     public Direction TextDirection
  105.     {
  106.         get
  107.         {
  108.             return textDirection;
  109.         }
  110.         set
  111.         {
  112.  
  113.             textDirection = value;
  114.             this.Invalidate();
  115.         }
  116.     }
  117.  
  118.     [Description("Display Text"), Category("Appearance")]
  119.     public override string Text
  120.     {
  121.         get
  122.         {
  123.             return text;
  124.         }
  125.         set
  126.         {
  127.             text = value;
  128.             this.Invalidate();
  129.         }
  130.     }
  131.  
  132.     #endregion
  133.  
  134.     #region Method
  135.     protected override void OnPaint(PaintEventArgs e)
  136.     {
  137.         Graphics graphics = e.Graphics;
  138.  
  139.         StringFormat stringFormat = new StringFormat();
  140.         stringFormat.Alignment = StringAlignment.Center;
  141.         stringFormat.Trimming = StringTrimming.None;
  142.         Brush textBrush = new SolidBrush(this.ForeColor);
  143.  
  144.  
  145.  
  146.         //Getting the width and height of the text, which we are going to write
  147.         float width = graphics.MeasureString(text, this.Font).Width;
  148.         float height = graphics.MeasureString(text, this.Font).Height;
  149.  
  150.         //The radius is set to 0.9 of the width or height, b'cos not to
  151.         //hide and part of the text at any stage
  152.         float radius = 0f;
  153.         if (ClientRectangle.Width < ClientRectangle.Height)
  154.         {
  155.             radius = ClientRectangle.Width * 0.9f / 2;
  156.         }
  157.         else
  158.         {
  159.             radius = ClientRectangle.Height * 0.9f / 2;
  160.         }
  161.  
  162.         //Setting the text according to the selection
  163.         switch (textOrientation)
  164.         {
  165.             #region Arc
  166.             case Orientation.Arc:
  167.                 {
  168.                     //Arc angle must be get from the length of the text.
  169.                     float arcAngle = (2 * width / radius) / text.Length;
  170.                     if (textDirection == Direction.Clockwise)
  171.                     {
  172.                         for (int i = 0; i < text.Length; i++)
  173.                         {
  174.  
  175.                             graphics.TranslateTransform(
  176.                                 (float)(radius * (1 - Math.Cos(arcAngle * i + rotationAngle / 180 * Math.PI))),
  177.                                 (float)(radius * (1 - Math.Sin(arcAngle * i + rotationAngle / 180 * Math.PI))));
  178.                             graphics.RotateTransform((-90 + (float)rotationAngle + 180 * arcAngle * i / (float)Math.PI));
  179.                             graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
  180.                             graphics.ResetTransform();
  181.                             AutoSize = false;
  182.                         }
  183.                     }
  184.                     else
  185.                     {
  186.                         for (int i = 0; i < text.Length; i++)
  187.                         {
  188.  
  189.                             graphics.TranslateTransform(
  190.                                 (float)(radius * (1 - Math.Cos(arcAngle * i + rotationAngle / 180 * Math.PI))),
  191.                                 (float)(radius * (1 + Math.Sin(arcAngle * i + rotationAngle / 180 * Math.PI))));
  192.                             graphics.RotateTransform((-90 - (float)rotationAngle - 180 * arcAngle * i / (float)Math.PI));
  193.                             graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
  194.                             graphics.ResetTransform();
  195.                             AutoSize = false;
  196.                         }
  197.                     }
  198.                     break;
  199.                 }
  200.             #endregion
  201.             #region Circle
  202.             case Orientation.Circle:
  203.                 {
  204.                     if (textDirection == Direction.Clockwise)
  205.                     {
  206.                         for (int i = 0; i < text.Length; i++)
  207.                         {
  208.                             graphics.TranslateTransform(
  209.                                 (float)(radius * (1 - Math.Cos((2 * Math.PI / text.Length) * i + rotationAngle / 180 * Math.PI))),
  210.                                 (float)(radius * (1 - Math.Sin((2 * Math.PI / text.Length) * i + rotationAngle / 180 * Math.PI))));
  211.                             graphics.RotateTransform(-90 + (float)rotationAngle + (360 / text.Length) * i);
  212.                             graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
  213.                             graphics.ResetTransform();
  214.  
  215.                         }
  216.                     }
  217.                     else
  218.                     {
  219.                         for (int i = 0; i < text.Length; i++)
  220.                         {
  221.                             graphics.TranslateTransform(
  222.                                 (float)(radius * (1 - Math.Cos((2 * Math.PI / text.Length) * i + rotationAngle / 180 * Math.PI))),
  223.                                 (float)(radius * (1 + Math.Sin((2 * Math.PI / text.Length) * i + rotationAngle / 180 * Math.PI))));
  224.                             graphics.RotateTransform(-90 - (float)rotationAngle - (360 / text.Length) * i);
  225.                             graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
  226.                             graphics.ResetTransform();
  227.                             AutoSize = false;
  228.                         }
  229.  
  230.                     }
  231.                     break;
  232.                 }
  233.             #endregion
  234.             #region Rotate
  235.             case Orientation.Rotate:
  236.                 {
  237.                     //For rotation, who about rotation?
  238.                     double angle = (rotationAngle / 180) * Math.PI;
  239.                     graphics.TranslateTransform(
  240.                         (ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle))) / 2,
  241.                         (ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle))) / 2);
  242.                     graphics.RotateTransform((float)rotationAngle);
  243.                     graphics.DrawString(text, this.Font, textBrush, 0, 0);
  244.                     graphics.ResetTransform();
  245.                     AutoSize = true;
  246.                     break;
  247.                 }
  248.             #endregion
  249.         }
  250.     }
  251.     #endregion
  252.  
  253. }
  254. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement