Advertisement
Guest User

C# Custom Transparent ListBox

a guest
Jul 28th, 2012
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Creator: Pingo
  2.  *Site: mpgh.net
  3.  *Created: 28/07/2012
  4.  *Version: 1.0.0
  5.  *Contact: ping0@hotmail.co.uk
  6.  * */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Windows.Forms;
  10. using System.Drawing;
  11. using System.ComponentModel;
  12.  
  13. public class ListBox : Label
  14. {
  15.     public ListBox()
  16.     {
  17.         this.Size = new Size(165, 124);
  18.         this.MinimumSize = new Size(25, 25);
  19.         this.ForeColor = SystemColors.Highlight;
  20.         this.Font = new Font("Verdana", 9F, FontStyle.Bold, GraphicsUnit.Point, 0);
  21.     }
  22.  
  23.     #region Storage
  24.     List<object> lst = new List<object>();
  25.     Color SBorCol = Color.DarkBlue, SelCol = Color.DodgerBlue, BorCol = Color.Navy, InBorCol = Color.SlateBlue;
  26.     string tmp = string.Empty;
  27.     byte SelAlpha = 50;
  28.     Size FSize;
  29.     int _Index = -1, SIndex = 0;
  30.     bool _Focus;
  31.     #endregion
  32.  
  33.     #region Overrides
  34.  
  35.     protected override void OnFontChanged(EventArgs e)
  36.     {
  37.         base.OnFontChanged(e);
  38.         FSize = TextRenderer.MeasureText("X", this.Font);
  39.     }
  40.  
  41.     protected override void OnHandleCreated(EventArgs e)
  42.     {
  43.         base.OnHandleCreated(e);
  44.         this.AutoSize = false;
  45.         this.Text = string.Empty;
  46.         this.BackColor = Color.Transparent;
  47.     }
  48.  
  49.     protected override void OnMouseDown(MouseEventArgs e)
  50.     {
  51.         base.OnMouseDown(e);
  52.         if (e.Button == MouseButtons.Left)
  53.         {
  54.             this.Focus();
  55.             _Focus = true;
  56.             _Index = -1;
  57.             Point P = this.PointToClient(MousePosition);
  58.             for (int i = lst.Count; i-- > 0; )
  59.             {
  60.                 if (i < MaxCount)
  61.                 {
  62.                     if (P.Y >= (FSize.Height * i) + 5 & P.Y < (FSize.Height * i) + (FSize.Height + 5))
  63.                     {
  64.                         this.Refresh();
  65.                         _Index = i + SIndex;
  66.                         this.CreateGraphics().FillRectangle(new SolidBrush(Color.FromArgb(SelAlpha, SelCol)), new Rectangle(5, (FSize.Height * i) + 5, this.Width - 12, FSize.Height));
  67.                         this.CreateGraphics().DrawRectangle(new Pen(SBorCol), new Rectangle(5, (FSize.Height * i) + 5, this.Width - 12, FSize.Height));
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     protected override void OnInvalidated(InvalidateEventArgs e)//
  75.     {
  76.         base.OnInvalidated(e);
  77.         tmp = string.Empty;
  78.         int Co = 0;
  79.         for (int i = SIndex; i < lst.Count; i++)
  80.         {
  81.             if (Co == MaxCount) return;
  82.             tmp += lst[i] + "\n";
  83.             Co++;
  84.         }
  85.     }
  86.  
  87.     protected override void OnDoubleClick(EventArgs e)
  88.     {
  89.         base.OnDoubleClick(e);
  90.         if (SelectedIndex != -1)
  91.         {
  92.             if (MessageBox.Show("Are you sure you want to delete\n" + lst[SelectedIndex], "Delete Dat Shizer", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  93.             {
  94.                 lst.RemoveAt(SelectedIndex);
  95.                 _Index = -1;
  96.                 this.Invalidate();
  97.             }
  98.         }
  99.     }
  100.  
  101.     #endregion
  102.  
  103.     #region Draw
  104.  
  105.     protected override void OnPaint(PaintEventArgs e)
  106.     {
  107.         base.OnPaint(e);
  108.         e.Graphics.DrawRectangle(new Pen(BorCol, 4), new Rectangle(2, 2, this.Width - 5, this.Height - 5));
  109.         e.Graphics.DrawRectangle(new Pen(InBorCol, 2), new Rectangle(4, 4, this.Width - 9, this.Height - 9));
  110.         TextRenderer.DrawText(e.Graphics, tmp, this.Font, new Point(5, 5), this.ForeColor);
  111.         if (_Index != -1)//Draws item highlight when scrolling
  112.         {
  113.             e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(SelAlpha, SelCol)), new Rectangle(5, (FSize.Height * (_Index - SIndex)) + 5, this.Width - 12, FSize.Height));
  114.             e.Graphics.DrawRectangle(new Pen(SBorCol), new Rectangle(5, (FSize.Height * (_Index - SIndex)) + 5, this.Width - 12, FSize.Height));
  115.         }
  116.     }
  117.  
  118.     #endregion
  119.  
  120.     #region UserSettings
  121.  
  122.     public Color SelectBorderColor
  123.     {
  124.         get { return SBorCol; }
  125.         set { SBorCol = value; }
  126.     }
  127.  
  128.     [Browsable(false)]
  129.     public new Color BackColor
  130.     {
  131.         get { return base.BackColor; }
  132.         set { base.BackColor = Color.Transparent; }
  133.     }
  134.  
  135.     public Color BorderColor
  136.     {
  137.         get { return BorCol; }
  138.         set { BorCol = value; }
  139.     }
  140.     public Color BorderColorInner
  141.     {
  142.         get { return InBorCol; }
  143.         set { InBorCol = value; }
  144.     }
  145.     public Color SelectColor
  146.     {
  147.         get { return SelCol; }
  148.         set { SelCol = value; }
  149.     }
  150.  
  151.     public byte SelectAlpha
  152.     {
  153.         get { return SelAlpha; }
  154.         set
  155.         {
  156.             SelAlpha = value <= 100 ? value : (byte)50;
  157.         }
  158.     }
  159.  
  160.     #endregion
  161.  
  162.     #region Functions
  163.  
  164.     public void Add(object Value)
  165.     {
  166.         if (Value == null) return;
  167.         int i;
  168.         if ((i = lst.IndexOf(Value)) == -1)
  169.         {
  170.             lst.Add(Value);
  171.         }
  172.         this.Invalidate();
  173.     }
  174.  
  175.     [Browsable(false)]
  176.     public int SelectedIndex
  177.     {
  178.         get { return _Index; }
  179.     }
  180.  
  181.     public void RemoveAt(int Index)
  182.     {
  183.         if (Index != -1 & Index <= lst.Count)
  184.         {
  185.             lst.RemoveAt(Index);
  186.             this.Invalidate();
  187.         }
  188.     }
  189.  
  190.     public void Remove(object Value)
  191.     {
  192.         if (Value == null) return;
  193.         int i;
  194.         if ((i = lst.IndexOf(Value)) != -1)
  195.         {
  196.             lst.RemoveAt(i);
  197.             this.Invalidate();
  198.         }
  199.     }
  200.  
  201.     public bool Contains(object Value)
  202.     {
  203.         return lst.Contains(Value);
  204.     }
  205.  
  206.     public void Clear()
  207.     {
  208.         lst.Clear();
  209.         this.Invalidate();
  210.     }
  211.  
  212.     int MaxCount
  213.     {
  214.         get
  215.         {
  216.             return ((this.Height - 8) / FSize.Height);
  217.         }
  218.     }
  219.  
  220.     [Browsable(false)]
  221.     public object SelectedItem
  222.     {
  223.         get
  224.         {
  225.             return SelectedIndex != -1 ? lst[_Index] : null;
  226.         }
  227.     }
  228.  
  229.     #endregion
  230.  
  231.     #region Disabled Settings
  232.     [Browsable(false)]
  233.     public override string Text { get; set; }
  234.  
  235.     [Browsable(false)]
  236.     public override bool AutoSize { get; set; }
  237.  
  238.     [Browsable(false)]
  239.     public new BorderStyle BorderStyle { get; set; }
  240.  
  241.     [Browsable(false)]
  242.     public new FlatStyle FlatStyle { get; set; }
  243.  
  244.     [Browsable(false)]
  245.     public new Image Image { get; set; }
  246.  
  247.     [Browsable(false)]
  248.     public new Image ImageAlign { get; set; }
  249.  
  250.     [Browsable(false)]
  251.     public new Image ImageIndex { get; set; }
  252.  
  253.     [Browsable(false)]
  254.     public new Image ImageKey { get; set; }
  255.  
  256.     [Browsable(false)]
  257.     public new Image ImageList { get; set; }
  258.  
  259.     [Browsable(false)]
  260.     public new Size MinimumSize { get; set; }
  261.  
  262.     [Browsable(false)]
  263.     public new DockStyle Dock { get; set; }
  264.  
  265.     [Browsable(false)]
  266.     public new HorizontalAlignment TextAlign { get; set; }
  267.  
  268.     [Browsable(false)]
  269.     public new bool UseWaitCursor { get; set; }
  270.     #endregion
  271.  
  272.     #region Scroll Code
  273.  
  274.     protected override void OnMouseWheel(MouseEventArgs e)
  275.     {
  276.         base.OnMouseWheel(e);
  277.         if (_Focus)
  278.         {
  279.             if (e.Delta > 0)
  280.             {
  281.                 if (SIndex > 0)
  282.                     SIndex--;
  283.             }
  284.             else
  285.             {
  286.                 if ((SIndex + MaxCount) >= lst.Count) return;
  287.                 if (SIndex <= (lst.Count - 1))
  288.                     SIndex++;
  289.             }
  290.             this.Refresh();
  291.         }
  292.     }
  293.  
  294.     protected override void OnMouseEnter(EventArgs e)
  295.     {
  296.         base.OnMouseEnter(e);
  297.     }
  298.  
  299.     protected override void OnMouseLeave(EventArgs e)
  300.     {
  301.         base.OnMouseLeave(e);
  302.         _Focus = false;
  303.     }
  304.     #endregion
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement