BitmapComboBox.cs
上传用户:nnpulika
上传日期:2013-02-15
资源大小:597k
文件大小:4k
源码类别:

状态条

开发平台:

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.General;
  9. namespace UtilityLibrary.WinControls
  10. {
  11. /// <summary>
  12. /// Summary description for TextComboBox.
  13. /// </summary>
  14. [ToolboxItem(false)]
  15. public class BitmapComboBox : ComboBoxBase
  16. {
  17. private const int PREVIEW_BOX_WIDTH = 20;
  18. protected override void OnPaint(PaintEventArgs pe)
  19. {
  20. base.OnPaint(pe);
  21. }
  22.                 
  23. protected override void OnDrawItem(DrawItemEventArgs e)
  24. {
  25. // Call base class to do the "Flat ComboBox" drawing
  26. base.OnDrawItem(e);
  27. // Draw bitmap strech to the size of the size of the combobox
  28. Graphics g = e.Graphics;
  29. Rectangle bounds = e.Bounds;
  30. bool selected = (e.State & DrawItemState.Selected) > 0;
  31. bool editSel = (e.State & DrawItemState.ComboBoxEdit ) > 0;
  32. if ( e.Index != -1 )
  33. DrawComboBoxItem(g, bounds, e.Index, selected, editSel);
  34. }
  35. protected override void DrawComboBoxItem(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
  36. {
  37. // Call base class to do the "Flat ComboBox" drawing
  38. // Draw bitmap
  39. base.DrawComboBoxItem(g, bounds, Index, selected, editSel);
  40. if ( Index != -1) 
  41. {
  42. Brush brush;
  43. brush = new SolidBrush(SystemColors.MenuText);
  44. g.DrawImage(bitmapsArray[Index], bounds.Left+2, bounds.Top+2, PREVIEW_BOX_WIDTH, bounds.Height-4);
  45. Pen blackPen = new Pen(new SolidBrush(Color.Black), 1);
  46. g.DrawRectangle(blackPen, new Rectangle(bounds.Left+1, bounds.Top+1, PREVIEW_BOX_WIDTH+1, bounds.Height-3));
  47. Size textSize = TextUtil.GetTextSize(g, Items[Index].ToString(), Font);
  48. int top = bounds.Top + (bounds.Height - textSize.Height)/2;
  49. g.DrawString(Items[Index].ToString(), Font, brush,
  50. new Point(bounds.Left + 28, top));
  51. brush.Dispose();
  52. }
  53. }
  54. protected override void DrawComboBoxItemEx(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
  55. {
  56. // This "hack" is necessary to avoid a clipping bug that comes from the fact that sometimes
  57. // we are drawing using the Graphics object for the edit control in the combobox and sometimes
  58. // we are using the graphics object for the combobox itself. If we use the same function to do our custom
  59. // drawing it is hard to adjust for the clipping because of these limitations
  60. base.DrawComboBoxItemEx(g, bounds, Index, selected, editSel);
  61. if ( Index != -1)
  62. {
  63. SolidBrush brush;
  64. brush = new SolidBrush(SystemColors.MenuText);
  65. Rectangle rc = bounds;
  66. rc.Inflate(-3, -3);
  67. Pen blackPen = new Pen(new SolidBrush(Color.Black), 1);
  68. g.DrawRectangle(blackPen, new Rectangle(rc.Left+1, rc.Top+1, PREVIEW_BOX_WIDTH+1, rc.Height-3));
  69. g.DrawImage(bitmapsArray[Index], rc.Left+2, rc.Top+2, PREVIEW_BOX_WIDTH, rc.Height-4);
  70. Size textSize = TextUtil.GetTextSize(g, Items[Index].ToString(), Font);
  71. int top = bounds.Top + (bounds.Height - textSize.Height)/2;
  72. // Clipping rectangle
  73. Rectangle clipRect = new Rectangle(bounds.Left + 31, top, bounds.Width - 31 - ARROW_WIDTH - 4, top+textSize.Height);
  74. g.DrawString(Items[Index].ToString(), Font, brush, clipRect);
  75. brush.Dispose();
  76. }
  77. }
  78. protected override void DrawDisableState()
  79. {
  80. // Draw the combobox state disable
  81. base.DrawDisableState();
  82. // Draw the specific disable state to
  83. // this derive class
  84. using ( Graphics g = CreateGraphics() )
  85. {
  86. using ( Brush b = new SolidBrush(SystemColors.ControlDark) )
  87. {
  88. Rectangle rc = ClientRectangle;
  89. Rectangle bounds = new Rectangle(rc.Left, rc.Top, rc.Width, rc.Height);
  90. bounds.Inflate(-3, -3);
  91. g.DrawRectangle(SystemPens.ControlDark, new Rectangle(bounds.Left+2, 
  92. bounds.Top+2, PREVIEW_BOX_WIDTH, bounds.Height-4));
  93. int index = SelectedIndex;
  94. Size textSize = TextUtil.GetTextSize(g, Items[index].ToString(), Font);
  95. // Clipping rectangle
  96. int top = rc.Top + (rc.Height - textSize.Height)/2;
  97. Rectangle clipRect = new Rectangle(rc.Left + 31, 
  98. top, rc.Width - 31 - ARROW_WIDTH - 4, top+textSize.Height);
  99. g.DrawString(Items[index].ToString(), Font, b, clipRect);
  100. }
  101. }
  102. }
  103. }
  104. }