HatchComboBox.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Windows.Forms;
  3. using System.ComponentModel;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing;
  6. using FastReport.Utils;
  7. namespace FastReport.Controls
  8. {
  9. internal class HatchComboBox : ComboBox
  10. {
  11. private bool updating;
  12. public event EventHandler HatchSelected;
  13. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  14. public new ObjectCollection Items
  15. {
  16. get { return base.Items; }
  17. }
  18. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  19. public HatchStyle Style
  20. {
  21. get
  22. {
  23. if (SelectedItem == null)
  24. return HatchStyle.BackwardDiagonal;
  25. return (HatchStyle)SelectedItem;
  26. }
  27. set
  28. {
  29. updating = true;
  30. SelectedItem = value;
  31. updating = false;
  32. }
  33. }
  34. protected override void OnDrawItem(DrawItemEventArgs e)
  35. {
  36. e.DrawBackground();
  37. if (e.Index >= 0)
  38. {
  39. Bitmap bmp = new Bitmap(this.LogicalToDevice(20), this.LogicalToDevice(20));
  40. HatchStyle style = (HatchStyle)Items[e.Index];
  41. using (Graphics g = Graphics.FromImage(bmp))
  42. using (Brush b = new HatchBrush(style, Color.Black, Color.White))
  43. {
  44. Rectangle rect = new Rectangle(0, 0, bmp.Width - 1, bmp.Height - 1);
  45. g.FillRectangle(b, rect);
  46. g.DrawRectangle(Pens.Black, rect);
  47. }
  48. this.DrawImageAndText(e, bmp, style.ToString());
  49. }
  50. }
  51. protected override void OnSelectedIndexChanged(EventArgs e)
  52. {
  53. base.OnSelectedIndexChanged(e);
  54. if (updating)
  55. return;
  56. if (HatchSelected != null)
  57. HatchSelected(this, EventArgs.Empty);
  58. }
  59. private void UpdateItems()
  60. {
  61. HatchStyle[] styles = new HatchStyle[] {
  62. HatchStyle.BackwardDiagonal,
  63. HatchStyle.Cross,
  64. HatchStyle.DarkDownwardDiagonal,
  65. HatchStyle.DarkHorizontal,
  66. HatchStyle.DarkUpwardDiagonal,
  67. HatchStyle.DarkVertical,
  68. HatchStyle.DashedDownwardDiagonal,
  69. HatchStyle.DashedHorizontal,
  70. HatchStyle.DashedUpwardDiagonal,
  71. HatchStyle.DashedVertical,
  72. HatchStyle.DiagonalBrick,
  73. HatchStyle.DiagonalCross,
  74. HatchStyle.Divot,
  75. HatchStyle.DottedDiamond,
  76. HatchStyle.DottedGrid,
  77. HatchStyle.ForwardDiagonal,
  78. HatchStyle.Horizontal,
  79. HatchStyle.HorizontalBrick,
  80. HatchStyle.LargeCheckerBoard,
  81. HatchStyle.LargeConfetti,
  82. HatchStyle.LightDownwardDiagonal,
  83. HatchStyle.LightHorizontal,
  84. HatchStyle.LightUpwardDiagonal,
  85. HatchStyle.LightVertical,
  86. HatchStyle.NarrowHorizontal,
  87. HatchStyle.NarrowVertical,
  88. HatchStyle.OutlinedDiamond,
  89. HatchStyle.Percent05,
  90. HatchStyle.Percent10,
  91. HatchStyle.Percent20,
  92. HatchStyle.Percent25,
  93. HatchStyle.Percent30,
  94. HatchStyle.Percent40,
  95. HatchStyle.Percent50,
  96. HatchStyle.Percent60,
  97. HatchStyle.Percent70,
  98. HatchStyle.Percent75,
  99. HatchStyle.Percent80,
  100. HatchStyle.Percent90,
  101. HatchStyle.Plaid,
  102. HatchStyle.Shingle,
  103. HatchStyle.SmallCheckerBoard,
  104. HatchStyle.SmallConfetti,
  105. HatchStyle.SmallGrid,
  106. HatchStyle.SolidDiamond,
  107. HatchStyle.Sphere,
  108. HatchStyle.Trellis,
  109. HatchStyle.Vertical,
  110. HatchStyle.Weave,
  111. HatchStyle.WideDownwardDiagonal,
  112. HatchStyle.WideUpwardDiagonal,
  113. HatchStyle.ZigZag };
  114. Items.Clear();
  115. foreach (HatchStyle style in styles)
  116. {
  117. Items.Add(style);
  118. }
  119. }
  120. public HatchComboBox()
  121. {
  122. DrawMode = DrawMode.OwnerDrawFixed;
  123. DropDownStyle = ComboBoxStyle.DropDownList;
  124. ItemHeight = 24;
  125. IntegralHeight = false;
  126. UpdateItems();
  127. }
  128. }
  129. }