ColorListBox.cs
Upload User: nnpulika
Upload Date: 2013-02-15
Package Size: 597k
Code Size: 3k
Category:

StatusBar

Development Platform:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7. using System.Drawing.Design;
  8. using UtilityLibrary.General;
  9. namespace UtilityLibrary.WinControls 
  10. {
  11. /// <summary>
  12. /// Summary description for ColorListBox.
  13. /// </summary>
  14. [ToolboxBitmap(typeof(UtilityLibrary.WinControls.ColorListBox), 
  15.  "UtilityLibrary.WinControls.ColorListBox.bmp")]
  16. [Designer(typeof(UtilityLibrary.Designers.ColorListBoxDesigner))] 
  17. public class ColorListBox : System.Windows.Forms.ListBox
  18. {
  19. #region Class variables
  20. string[] colorArray = null;
  21. #endregion
  22. #region Constructors
  23. public ColorListBox()
  24. {
  25. DrawMode = DrawMode.OwnerDrawFixed;
  26. ItemHeight = ItemHeight + 1;
  27. }
  28. #endregion
  29. #region Overrides
  30. protected override void OnPaint(PaintEventArgs pe)
  31. {
  32. base.OnPaint(pe);
  33. }
  34. protected override void OnDrawItem(DrawItemEventArgs e)
  35. {
  36. Graphics g = e.Graphics;
  37. Rectangle bounds = e.Bounds;
  38. bool selected = (e.State & DrawItemState.Selected) > 0;
  39. bool editSel = (e.State & DrawItemState.ComboBoxEdit ) > 0;
  40. if ( e.Index != -1 )
  41. DrawListBoxItem(g, bounds, e.Index, selected, editSel);
  42. }
  43. #endregion
  44. #region Properties
  45. [Editor(typeof(UtilityLibrary.Designers.ColorListBoxEditor), typeof(UITypeEditor))]
  46. public  new ListBox.ObjectCollection Items
  47. {
  48. get{ return base.Items; }
  49. }
  50. public String[] ColorArray
  51. {
  52. get
  53. {
  54. return colorArray;
  55. }
  56. set
  57. {
  58. colorArray = value;
  59. if ( colorArray != null )
  60. Items.AddRange(value);
  61. }
  62. }
  63. #endregion
  64. #region Methods
  65. public void PassMsg(ref Message m)
  66. {
  67. base.WndProc(ref m);
  68. }
  69. #endregion
  70. #region Implementation
  71. protected void DrawListBoxItem(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
  72. {
  73. // Draw List box item
  74. if ( Index != -1)
  75. {
  76. if ( selected )
  77. {
  78. // Draw highlight rectangle
  79. using ( Brush b = new SolidBrush(ColorUtil.VSNetSelectionColor) )
  80. {
  81. g.FillRectangle(b, bounds.Left, bounds.Top, bounds.Width, bounds.Height);
  82. }
  83. using ( Pen p = new Pen(ColorUtil.VSNetBorderColor) )
  84. {
  85. g.DrawRectangle(p, bounds.Left, bounds.Top, bounds.Width-1, bounds.Height-1);
  86. }
  87. }
  88. else
  89. {
  90. // Erase highlight rectangle
  91. g.FillRectangle(SystemBrushes.Window, bounds.Left, bounds.Top, bounds.Width, bounds.Height);
  92. }
  93. string item = (string)Items[Index];
  94. Color currentColor = Color.FromName(item);
  95. using ( Brush b = new SolidBrush(currentColor) )
  96. {
  97. g.FillRectangle(new SolidBrush(currentColor), bounds.Left+2, bounds.Top+2, 20, bounds.Height-4);
  98. }
  99. g.DrawRectangle(Pens.Black, new Rectangle(bounds.Left+1, bounds.Top+1, 21, bounds.Height-3));
  100. g.DrawString(item, SystemInformation.MenuFont, SystemBrushes.ControlText, new Point(bounds.Left + 28, bounds.Top));
  101. }
  102. }
  103. #endregion
  104. }
  105. }