ColorComboBox.cs
Upload User: nnpulika
Upload Date: 2013-02-15
Package Size: 597k
Code Size: 5k
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.Diagnostics;
  8. using UtilityLibrary.Win32;
  9. using UtilityLibrary.General;
  10. using UtilityLibrary.Menus;
  11. namespace UtilityLibrary.WinControls
  12. {
  13. /// <summary>
  14. /// Summary description for ColorComboBox.
  15. /// </summary>
  16. [ToolboxItem(true)]
  17. [ToolboxBitmap(typeof(UtilityLibrary.WinControls.ColorComboBox), 
  18.      "UtilityLibrary.WinControls.ColorComboBox.bmp")]
  19. [Designer(typeof(UtilityLibrary.Designers.ColorComboBoxDesigner))]
  20. public class ColorComboBox : ComboBoxBase
  21. {
  22. #region Class Variables
  23. private const int PREVIEW_BOX_WIDTH = 20;
  24. #endregion
  25. #region Constructos
  26. // For use when hosted by a toolbar
  27. public ColorComboBox(bool toolBarUse) : base(toolBarUse)
  28. {
  29. DropDownStyle = ComboBoxStyle.DropDownList;
  30. Items.AddRange(ColorUtil.KnownColorNames);
  31. }
  32. public ColorComboBox()
  33. {
  34. DropDownStyle = ComboBoxStyle.DropDownList;
  35. Items.AddRange(ColorUtil.KnownColorNames);
  36. }
  37. #endregion
  38.         #region Overrides
  39. protected override void DrawComboBoxItem(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
  40. {
  41. // Call base class to do the "Flat ComboBox" drawing
  42. base.DrawComboBoxItem(g, bounds, Index, selected, editSel);
  43. if ( Index != -1)
  44. {
  45. string item = Items[Index].ToString();
  46. Color currentColor = Color.FromName(item);
  47. Brush brush;
  48. brush = new SolidBrush(SystemColors.MenuText);
  49. Brush currentColorBrush = new SolidBrush(currentColor);
  50. g.FillRectangle(currentColorBrush, bounds.Left+2, bounds.Top+2, PREVIEW_BOX_WIDTH , bounds.Height-4);
  51. currentColorBrush.Dispose();
  52. Pen blackPen = new Pen(Brushes.Black, 1);
  53. g.DrawRectangle(blackPen, new Rectangle(bounds.Left+1, bounds.Top+1, PREVIEW_BOX_WIDTH+1, bounds.Height-3));
  54. blackPen.Dispose();
  55.                 Size textSize = TextUtil.GetTextSize(g, Items[Index].ToString(), Font);
  56. int top = bounds.Top + (bounds.Height - textSize.Height)/2;
  57. g.DrawString(item, Font, brush, new Point(bounds.Left + 28, top));
  58.                 brush.Dispose();
  59. }
  60. }
  61. protected override void DrawComboBoxItemEx(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
  62. {
  63. // This "hack" is necessary to avoid a clipping bug that comes from the fact that sometimes
  64. // we are drawing using the Graphics object for the edit control in the combobox and sometimes
  65. // we are using the graphics object for the combobox itself. If we use the same function to do our custom
  66. // drawing it is hard to adjust for the clipping because of this situation
  67. base.DrawComboBoxItemEx(g, bounds, Index, selected, editSel);
  68. if ( Index != -1)
  69. {
  70. string item = Items[Index].ToString();
  71. Color currentColor = Color.FromName(item);
  72. SolidBrush brush;
  73. brush = new SolidBrush(SystemColors.MenuText);
  74. Rectangle rc = bounds;
  75. rc.Inflate(-3, -3);
  76. Brush currentColorBrush = new SolidBrush(currentColor);
  77. g.FillRectangle(currentColorBrush, rc.Left+2, rc.Top+2, PREVIEW_BOX_WIDTH , rc.Height-4);
  78. Pen blackPen = new Pen(Brushes.Black, 1);
  79. g.DrawRectangle(blackPen, new Rectangle(rc.Left+1, rc.Top+1, PREVIEW_BOX_WIDTH+1, rc.Height-3));
  80. blackPen.Dispose();
  81. Size textSize = TextUtil.GetTextSize(g, Items[Index].ToString(), Font);
  82. int top = bounds.Top + (bounds.Height - textSize.Height)/2;
  83. // Clipping rectangle
  84. Rectangle clipRect = new Rectangle(bounds.Left + 31, top, bounds.Width - 31 - ARROW_WIDTH - 4, top+textSize.Height);
  85.                 g.DrawString(Items[Index].ToString(), Font, brush, clipRect);
  86. brush.Dispose();
  87. }
  88. }
  89. protected override void DrawDisableState()
  90. {
  91. // Draw the combobox state disable
  92. base.DrawDisableState();
  93.             // Draw the specific disable state to
  94. // this derive class
  95. using ( Graphics g = CreateGraphics() )
  96. {
  97. using ( Brush b = new SolidBrush(SystemColors.ControlDark) )
  98. {
  99. Rectangle rc = ClientRectangle;
  100. Rectangle bounds = new Rectangle(rc.Left, rc.Top, rc.Width, rc.Height);
  101. bounds.Inflate(-3, -3);
  102. g.DrawRectangle(SystemPens.ControlDark, new Rectangle(bounds.Left+2, 
  103. bounds.Top+2, PREVIEW_BOX_WIDTH, bounds.Height-4));
  104. int index = SelectedIndex;
  105. Size textSize = TextUtil.GetTextSize(g, Items[index].ToString(), Font);
  106. // Clipping rectangle
  107. int top = rc.Top + (rc.Height - textSize.Height)/2;
  108. Rectangle clipRect = new Rectangle(rc.Left + 31, 
  109. top, rc.Width - 31 - ARROW_WIDTH - 4, top+textSize.Height);
  110. g.DrawString(Items[index].ToString(), Font, b, clipRect);
  111. }
  112. }
  113. }
  114. #endregion
  115. #region Methods
  116. public void PassMsg(ref Message m)
  117. {
  118. base.WndProc(ref m);
  119. }
  120. #endregion
  121.         
  122. }
  123. }