Advertisement
Yassine_Abbani

Custom Rating [tool], [Custom Control]

May 24th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. #region Directives
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. #endregion
  6.  
  7. #region Copyright & Contact
  8. //>-------------------------------------------------------------------------------<
  9. // Tool Name    : Rating Strip                                                     *
  10. // From Project : Creator Eye                                                      *
  11. // Creator      : Yassine Abbani                                                   *
  12. // Facebook     : https://www.facebook.com/YassineAbbani.user                      *
  13. // Pastebin     : https://pastebin.com/u/Yassine_Abbani                            *
  14. // Youtube      : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw         *
  15. // Version      : 1.0 Beta                                                         *
  16. // Color        : Nul                                                              *
  17. // Style        : Costume                                                          *
  18. //>-------------------------------------------------------------------------------<
  19. /*
  20.  * Properties
  21.  * Add Image as rated,Unrated control
  22.  *
  23. */
  24. #endregion
  25. #region Costume Rating Strip
  26. public class Ce_CostumeRating : UserControl
  27. {
  28.  
  29.     #region  Variables
  30.  
  31.     private Size _ImageSize;
  32.     private Image _ImageRated;
  33.     private Image _ImageUnrated;
  34.     private int _Stars = 0;
  35.     private int _MaximumStars = 5;
  36.     private int TempStar = -1;
  37.  
  38.     #endregion
  39.     #region  Properties
  40.  
  41.     protected Size ImageSize
  42.     {
  43.         get
  44.         {
  45.             return _ImageSize;
  46.         }
  47.     }
  48.  
  49.     public int Stars
  50.     {
  51.         get
  52.         {
  53.             return this._Stars;
  54.         }
  55.         set
  56.         {
  57.             if (value > _MaximumStars)
  58.             {
  59.                 MessageBox.Show("Value can\'t be higher than the maximum number of stars!");
  60.             }
  61.             this._Stars = value;
  62.             this.Invalidate();
  63.         }
  64.     }
  65.  
  66.     public int MaximumStars
  67.     {
  68.         get
  69.         {
  70.             return this._MaximumStars;
  71.         }
  72.         set
  73.         {
  74.             this._MaximumStars = value;
  75.         }
  76.     }
  77.  
  78.     public Image ImageRated
  79.     {
  80.         get
  81.         {
  82.             return _ImageRated;
  83.         }
  84.         set
  85.         {
  86.             if (value == null)
  87.             {
  88.                 _ImageSize = Size.Empty;
  89.             }
  90.             else
  91.             {
  92.                 _ImageSize = value.Size;
  93.             }
  94.             _ImageRated = value;
  95.             Invalidate();
  96.         }
  97.     }
  98.  
  99.     public Image ImageUnrated
  100.     {
  101.         get
  102.         {
  103.             return _ImageUnrated;
  104.         }
  105.         set
  106.         {
  107.             if (value == null)
  108.             {
  109.                 _ImageSize = Size.Empty;
  110.             }
  111.             else
  112.             {
  113.                 _ImageSize = value.Size;
  114.             }
  115.             _ImageUnrated = value;
  116.             Invalidate();
  117.         }
  118.     }
  119.  
  120.     #endregion
  121.     #region  EventArgs
  122.  
  123.     protected override void OnMouseMove(MouseEventArgs e)
  124.     {
  125.         base.OnMouseMove(e);
  126.         double StarLoc = (e.X + _ImageRated.Width - 5) / _ImageRated.Width;
  127.         int HoverStar = Convert.ToInt32(Math.Floor(StarLoc));
  128.  
  129.         if (!HoverStar.Equals(TempStar))
  130.         {
  131.             TempStar = HoverStar;
  132.             this.Invalidate();
  133.         }
  134.     }
  135.  
  136.     protected override void OnMouseLeave(EventArgs e)
  137.     {
  138.         base.OnMouseLeave(e);
  139.         TempStar = -1;
  140.         this.Invalidate();
  141.     }
  142.  
  143.     protected override void OnMouseDown(MouseEventArgs e)
  144.     {
  145.         base.OnMouseDown(e);
  146.         double StarLoc = (e.X + _ImageRated.Width - 5) / _ImageRated.Width;
  147.         int StarToAdd = Convert.ToInt32(Math.Floor(StarLoc));
  148.  
  149.         if (!StarToAdd.Equals(_Stars))
  150.         {
  151.             _Stars = StarToAdd;
  152.             if (_Stars > _MaximumStars)
  153.             {
  154.                 _Stars = _MaximumStars;
  155.             }
  156.             this.Invalidate();
  157.         }
  158.     }
  159.  
  160.     #endregion
  161.     #region
  162.     public Ce_CostumeRating()
  163.     {
  164.         Size = new Size(82, 17);
  165.         BackColor = Color.Transparent;
  166.         DoubleBuffered = true;
  167.     }
  168.     #endregion
  169.     #region Draw Control
  170.     protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  171.     {
  172.         base.OnPaint(e);
  173.         Graphics G = e.Graphics;
  174.  
  175.         if (_ImageRated == null || _ImageUnrated == null)
  176.         {
  177.             return;
  178.         }
  179.  
  180.         for (int i = 0; i <= _MaximumStars - 1; i++)
  181.         {
  182.             if (i < (TempStar == -1 ? _Stars : TempStar))
  183.             {
  184.                 G.DrawImage(_ImageRated, _ImageRated.Width * i, 0, ImageSize.Width, ImageSize.Height);
  185.             }
  186.             else
  187.             {
  188.                 G.DrawImage(_ImageUnrated, _ImageRated.Width * i, 0, ImageSize.Width, ImageSize.Height);
  189.             }
  190.         }
  191.     }
  192.     #endregion
  193. }
  194. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement