ResPool.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. namespace System.Windows.Forms
  5. {
  6. internal static class ResPool
  7. {
  8. private static Hashtable pens = new();
  9. private static Hashtable brushes = new();
  10. private static Hashtable fonts = new();
  11. public static Pen GetPen(Color color)
  12. {
  13. return GetPen(color, 1, DashStyle.Solid);
  14. }
  15. public static Pen GetPen(Color color, float width)
  16. {
  17. return GetPen(color, width, DashStyle.Solid);
  18. }
  19. public static Pen GetPen(Color color, float width, DashStyle style)
  20. {
  21. int hash = color.GetHashCode() ^ width.GetHashCode() ^ style.GetHashCode();
  22. Pen result = pens[hash] as Pen;
  23. if (result == null)
  24. {
  25. result = new Pen(color, width);
  26. result.DashStyle = style;
  27. pens[hash] = result;
  28. }
  29. return result;
  30. }
  31. public static SolidBrush GetSolidBrush(Color color)
  32. {
  33. int hash = color.GetHashCode();
  34. SolidBrush result = brushes[hash] as SolidBrush;
  35. if (result == null)
  36. {
  37. result = new SolidBrush(color);
  38. brushes[hash] = result;
  39. }
  40. return result;
  41. }
  42. public static Font GetFont(FontFamily name, float size, Drawing.FontStyle style)
  43. {
  44. int hash = name.GetHashCode() ^ size.GetHashCode() ^ style.GetHashCode();
  45. Font result = fonts[hash] as Font;
  46. if (result == null)
  47. {
  48. result = new Font(name, size, style);
  49. fonts[hash] = result;
  50. }
  51. return result;
  52. }
  53. }
  54. }