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

状态条

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Design;
  6. using System.Data;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;
  9. using UtilityLibrary.General;
  10. namespace UtilityLibrary.WinControls
  11. {
  12. /// <summary>
  13. /// Summary description for ImageComboBox.
  14. /// </summary>
  15. [ToolboxItem(true)]
  16. [ToolboxBitmap(typeof(UtilityLibrary.WinControls.ImageComboBox), 
  17.  "UtilityLibrary.WinControls.ImageComboBox.bmp")]
  18. [Designer(typeof(UtilityLibrary.Designers.ImageComboBoxDesigner))]
  19. public class ImageComboBox : ComboBoxBase
  20. {
  21. #region Class Variables
  22. Bitmap[] bitmapsArray;
  23. string[] bitmapsNames;
  24. ImageList imageList;
  25. bool useImageList = false;
  26. private const int PREVIEW_BOX_WIDTH = 20;
  27. #endregion
  28. #region Constructors
  29. public ImageComboBox()
  30. {
  31. InitializeImageComboBox(null, true, null, null);
  32.             
  33. }
  34. public ImageComboBox(ImageList imageList)
  35. {
  36. InitializeImageComboBox(imageList, true, null, null);
  37. }
  38. // Run time support only
  39. public ImageComboBox(Bitmap[] bitmapsArray, String[] bitmapsNames)
  40. {
  41. InitializeImageComboBox(null, false, bitmapsArray, bitmapsNames);
  42. }
  43. // Run time support only
  44. public ImageComboBox(Bitmap[] bitmapsArray, String[] bitmapsNames, bool toolBarUse): base(toolBarUse)
  45. {
  46. // To be used when using the combobox in as a ToolBarItem in a ToolBarEx control
  47. InitializeImageComboBox(null, false, bitmapsArray, bitmapsNames);
  48. }
  49. void InitializeImageComboBox(ImageList list, bool useImageList, Bitmap[] bitmapsArray, String[] bitmapsNames)
  50. {
  51. DropDownStyle = ComboBoxStyle.DropDownList;
  52. imageList = list;
  53. this.useImageList = useImageList;
  54. if ( bitmapsArray != null && bitmapsNames != null && useImageList == false )
  55. {
  56. this.bitmapsArray = bitmapsArray;
  57. this.bitmapsNames = bitmapsNames;
  58. for ( int i = 0; i < bitmapsArray.Length; i++ ) 
  59. {
  60. Items.Add(bitmapsNames[i]);
  61. }
  62. }
  63. }
  64. #endregion
  65. #region Overrides
  66. protected override void DrawComboBoxItem(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
  67. {
  68. // Call base class to do the "Flat ComboBox" drawing
  69. // Draw bitmap
  70. base.DrawComboBoxItem(g, bounds, Index, selected, editSel);
  71. if ( Index != -1) 
  72. {
  73. Brush brush;
  74. brush = new SolidBrush(SystemColors.MenuText);
  75. if ( useImageList == false )
  76.                     g.DrawImage(bitmapsArray[Index], bounds.Left+2, bounds.Top+2, PREVIEW_BOX_WIDTH, bounds.Height-4);
  77. else
  78.                     g.DrawImage(imageList.Images[Index], bounds.Left+2, bounds.Top+2, PREVIEW_BOX_WIDTH, bounds.Height-4);
  79. Pen blackPen = new Pen(new SolidBrush(Color.Black), 1);
  80. g.DrawRectangle(blackPen, new Rectangle(bounds.Left+1, bounds.Top+1, PREVIEW_BOX_WIDTH+1, bounds.Height-3));
  81. Size textSize = TextUtil.GetTextSize(g, Items[Index].ToString(), Font);
  82. int top = bounds.Top + (bounds.Height - textSize.Height)/2;
  83. g.DrawString(Items[Index].ToString(), Font, brush,
  84. new Point(bounds.Left + 28, top));
  85. brush.Dispose();
  86. }
  87. }
  88. protected override void DrawComboBoxItemEx(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
  89. {
  90. // This "hack" is necessary to avoid a clipping bug that comes from the fact that sometimes
  91. // we are drawing using the Graphics object for the edit control in the combobox and sometimes
  92. // we are using the graphics object for the combobox itself. If we use the same function to do our custom
  93. // drawing it is hard to adjust for the clipping because of these limitations
  94. base.DrawComboBoxItemEx(g, bounds, Index, selected, editSel);
  95. if ( Index != -1)
  96. {
  97. SolidBrush brush;
  98. brush = new SolidBrush(SystemColors.MenuText);
  99. Rectangle rc = bounds;
  100. rc.Inflate(-3, -3);
  101. Pen blackPen = new Pen(new SolidBrush(Color.Black), 1);
  102. g.DrawRectangle(blackPen, new Rectangle(rc.Left+1, rc.Top+1, PREVIEW_BOX_WIDTH+1, rc.Height-3));
  103. if ( useImageList == false )
  104.                     g.DrawImage(bitmapsArray[Index], rc.Left+2, rc.Top+2, PREVIEW_BOX_WIDTH, rc.Height-4);
  105. else
  106.                     g.DrawImage(imageList.Images[Index], rc.Left+2, rc.Top+2, PREVIEW_BOX_WIDTH, rc.Height-4);
  107. Size textSize = TextUtil.GetTextSize(g, Items[Index].ToString(), Font);
  108. int top = bounds.Top + (bounds.Height - textSize.Height)/2;
  109. // Clipping rectangle
  110. Rectangle clipRect = new Rectangle(bounds.Left + 31, top, bounds.Width - 31 - ARROW_WIDTH - 4, top+textSize.Height);
  111. g.DrawString(Items[Index].ToString(), Font, brush, clipRect);
  112. brush.Dispose();
  113. }
  114. }
  115. protected override void DrawDisableState()
  116. {
  117. // Draw the combobox state disable
  118. base.DrawDisableState();
  119. // Draw the specific disable state to
  120. // this derive class
  121. using ( Graphics g = CreateGraphics() )
  122. {
  123. using ( Brush b = new SolidBrush(SystemColors.ControlDark) )
  124. {
  125. Rectangle rc = ClientRectangle;
  126. Rectangle bounds = new Rectangle(rc.Left, rc.Top, rc.Width, rc.Height);
  127. bounds.Inflate(-3, -3);
  128. g.DrawRectangle(SystemPens.ControlDark, new Rectangle(bounds.Left+2, 
  129. bounds.Top+2, PREVIEW_BOX_WIDTH, bounds.Height-4));
  130. int index = SelectedIndex;
  131. Size textSize = TextUtil.GetTextSize(g, Items[index].ToString(), Font);
  132. // Clipping rectangle
  133. int top = rc.Top + (rc.Height - textSize.Height)/2;
  134. Rectangle clipRect = new Rectangle(rc.Left + 31, 
  135. top, rc.Width - 31 - ARROW_WIDTH - 4, top+textSize.Height);
  136. g.DrawString(Items[index].ToString(), Font, b, clipRect);
  137. }
  138. }
  139. }
  140. #endregion
  141.         #region Properties
  142. public ImageList Images
  143. {
  144. get{ return imageList;}
  145. set{ imageList = value;}
  146. }
  147. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  148. [Editor(typeof(UtilityLibrary.Designers.ImageComboBoxEditor), typeof(UITypeEditor))]
  149. public new ComboBox.ObjectCollection Items
  150. {
  151. get { return base.Items; }
  152. }
  153. // Only Run time support
  154. [Browsable(false)]
  155. public Bitmap[] Bitmaps
  156. {
  157. set { bitmapsArray = value; }
  158. get { return bitmapsArray; }
  159. }
  160. // Only Run time support
  161. [Browsable(false)]
  162. public string[] BitmapNames
  163. {
  164. set
  165. {
  166. bitmapsNames = value;
  167. if ( bitmapsNames != null )
  168. {
  169. // Add empty element so that we can get call to draw
  170. // the bitmaps items
  171. for ( int i = 0; i < bitmapsNames.Length; i++ ) 
  172. {
  173. Items.Add(bitmapsNames[i]);
  174. }
  175. }
  176. }
  177. get { return bitmapsNames; }
  178. }
  179. #endregion
  180. #region Methods
  181. // Designer support
  182. public void PassMsg(ref Message m)
  183. {
  184. base.WndProc(ref m);
  185. }
  186. #endregion
  187. }
  188. }