PenMapper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. namespace IndexFormula.Finance
  2. {
  3. using System;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Design;
  7. using System.Drawing.Drawing2D;
  8. using System.Xml.Serialization;
  9. [Serializable, Editor(typeof(PenMapperEditor), typeof(UITypeEditor)), TypeConverter(typeof(ExpandableObjectConverter))]
  10. public class PenMapper
  11. {
  12. private byte alpha;
  13. private System.Drawing.Color color;
  14. private System.Drawing.Drawing2D.DashCap dashCap;
  15. private float[] dashPattern;
  16. private System.Drawing.Drawing2D.DashStyle dashStyle;
  17. private static PenMapper defaultPen = new PenMapper();
  18. private ArrowCap endCap;
  19. private ArrowCap startCap;
  20. private int width;
  21. public PenMapper()
  22. {
  23. this.color = System.Drawing.Color.Black;
  24. this.alpha = 0xff;
  25. this.width = 1;
  26. }
  27. public PenMapper(System.Drawing.Color color) : this()
  28. {
  29. this.color = color;
  30. }
  31. public PenMapper(System.Drawing.Color color, int width) : this(color)
  32. {
  33. this.width = width;
  34. }
  35. public PenMapper(System.Drawing.Color color, int width, byte alpha) : this(color, width)
  36. {
  37. this.alpha = alpha;
  38. }
  39. public PenMapper Clone()
  40. {
  41. PenMapper mapper = new PenMapper();
  42. mapper.color = this.color;
  43. mapper.alpha = this.alpha;
  44. mapper.dashStyle = this.dashStyle;
  45. mapper.startCap = this.startCap;
  46. mapper.endCap = this.endCap;
  47. mapper.width = this.width;
  48. mapper.dashPattern = this.dashPattern;
  49. return mapper;
  50. }
  51. public Pen GetPen()
  52. {
  53. Pen pen = new Pen(System.Drawing.Color.FromArgb(this.alpha, this.color), (float) this.width);
  54. pen.DashCap = this.DashCap;
  55. if (this.dashPattern != null)
  56. {
  57. pen.DashPattern = this.dashPattern;
  58. }
  59. else
  60. {
  61. pen.DashStyle = this.dashStyle;
  62. }
  63. if (((this.startCap != null) && (this.startCap.Width != 0)) && (this.startCap.Height != 0))
  64. {
  65. pen.CustomStartCap = new AdjustableArrowCap((float) this.startCap.Width, (float) this.startCap.Height, this.startCap.Filled);
  66. }
  67. if (((this.endCap != null) && (this.endCap.Width != 0)) && (this.endCap.Height != 0))
  68. {
  69. pen.CustomEndCap = new AdjustableArrowCap((float) this.endCap.Width, (float) this.endCap.Height, this.endCap.Filled);
  70. }
  71. return pen;
  72. }
  73. public static bool NotDefault(PenMapper op)
  74. {
  75. return (((((defaultPen.Alpha != op.Alpha) || (defaultPen.Color != op.Color)) || ((defaultPen.DashCap != op.DashCap) || (defaultPen.DashStyle != op.DashStyle))) || (((defaultPen.EndCap != op.EndCap) || (defaultPen.StartCap != op.StartCap)) || (defaultPen.Width != op.Width))) || (op.dashPattern != null));
  76. }
  77. public override string ToString()
  78. {
  79. return (this.color.Name + "," + this.width);
  80. }
  81. [RefreshProperties(RefreshProperties.All), XmlAttribute, DefaultValue(0xff)]
  82. public byte Alpha
  83. {
  84. get
  85. {
  86. return this.alpha;
  87. }
  88. set
  89. {
  90. this.alpha = value;
  91. }
  92. }
  93. [XmlIgnore]
  94. public System.Drawing.Color Color
  95. {
  96. get
  97. {
  98. return this.color;
  99. }
  100. set
  101. {
  102. this.color = value;
  103. }
  104. }
  105. [XmlAttribute, DefaultValue(0), RefreshProperties(RefreshProperties.All)]
  106. public System.Drawing.Drawing2D.DashCap DashCap
  107. {
  108. get
  109. {
  110. return this.dashCap;
  111. }
  112. set
  113. {
  114. this.dashCap = value;
  115. }
  116. }
  117. [TypeConverter(typeof(FloatArrayConverter)), Editor(typeof(FloatArrayEditor), typeof(UITypeEditor))]
  118. public float[] DashPattern
  119. {
  120. get
  121. {
  122. return this.dashPattern;
  123. }
  124. set
  125. {
  126. this.dashPattern = value;
  127. if (value != null)
  128. {
  129. for (int i = 0; i < value.Length; i++)
  130. {
  131. if (value[i] <= 0f)
  132. {
  133. value[i] = 1f;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. [DefaultValue(0), XmlAttribute, RefreshProperties(RefreshProperties.All)]
  140. public System.Drawing.Drawing2D.DashStyle DashStyle
  141. {
  142. get
  143. {
  144. return this.dashStyle;
  145. }
  146. set
  147. {
  148. this.dashStyle = value;
  149. }
  150. }
  151. public ArrowCap EndCap
  152. {
  153. get
  154. {
  155. return this.endCap;
  156. }
  157. set
  158. {
  159. this.endCap = value;
  160. }
  161. }
  162. public ArrowCap StartCap
  163. {
  164. get
  165. {
  166. return this.startCap;
  167. }
  168. set
  169. {
  170. this.startCap = value;
  171. }
  172. }
  173. [XmlAttribute, RefreshProperties(RefreshProperties.All), DefaultValue(1)]
  174. public int Width
  175. {
  176. get
  177. {
  178. return this.width;
  179. }
  180. set
  181. {
  182. this.width = value;
  183. }
  184. }
  185. [DefaultValue("Black"), XmlAttribute(AttributeName="Color"), Browsable(false)]
  186. public string XmlColor
  187. {
  188. get
  189. {
  190. return TypeDescriptor.GetConverter(typeof(System.Drawing.Color)).ConvertToString(null, FormulaHelper.enUS, this.color);
  191. }
  192. set
  193. {
  194. TypeConverter converter = TypeDescriptor.GetConverter(typeof(System.Drawing.Color));
  195. this.color = (System.Drawing.Color) converter.ConvertFromString(null, FormulaHelper.enUS, value);
  196. }
  197. }
  198. }
  199. }