Margins.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // System.Drawing.Margins.cs
  3. //
  4. // Authors:
  5. // Dennis Hayes ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc
  9. //
  10. using System;
  11. using System.ComponentModel;
  12. namespace System.Drawing.Printing
  13. {
  14. [TypeConverter (typeof (MarginsConverter))]
  15. public class Margins : ICloneable
  16. {
  17. int left;
  18. int right;
  19. int top;
  20. int bottom;
  21. public Margins()
  22. {
  23. left = 100;
  24. right = 100;
  25. top = 100;
  26. bottom = 100;
  27. }
  28. public Margins(int left, int right, int top, int bottom)
  29. {
  30. //Verify parameters
  31. if (left < 0)
  32. throw new System.ArgumentException("All Margins must be greater than 0", "left");
  33. if (right < 0)
  34. throw new System.ArgumentException("All Margins must be greater than 0", "right");
  35. if (top < 0)
  36. throw new System.ArgumentException("All Margins must be greater than 0", "top");
  37. if (bottom < 0)
  38. throw new System.ArgumentException("All Margins must be greater than 0", "bottom");
  39. //Set proprities
  40. this.left = left;
  41. this.right = right;
  42. this.top = top;
  43. this.bottom = bottom;
  44. }
  45. public int Left {
  46. get {
  47. return left;
  48. }
  49. set {
  50. if (left < 0)
  51. throw new System.ArgumentException("All Margins must be greater than 0", "left");
  52. left = value;
  53. }
  54. }
  55. public int Right {
  56. get {
  57. return right;
  58. }
  59. set {
  60. if (right < 0)
  61. throw new System.ArgumentException("All Margins must be greater than 0", "left");
  62. right = value;
  63. }
  64. }
  65. public int Top {
  66. get {
  67. return top;
  68. }
  69. set {
  70. if (top < 0)
  71. throw new System.ArgumentException("All Margins must be greater than 0", "left");
  72. top = value;
  73. }
  74. }
  75. public int Bottom {
  76. get {
  77. return bottom;
  78. }
  79. set {
  80. if (bottom < 0)
  81. throw new System.ArgumentException("All Margins must be greater than 0", "left");
  82. bottom = value;
  83. }
  84. }
  85. public object Clone()
  86. {
  87. return new Margins (this.Left, this.Right, this.Top, this.Bottom);
  88. }
  89. public override bool Equals (object obj)
  90. {
  91. Margins m = obj as Margins;
  92. if (m == null)
  93. return false;
  94. if (m.Left == left && m.Right == right && m.Top == top && m.Bottom == bottom)
  95. return true;
  96. return false;
  97. }
  98. public override int GetHashCode ()
  99. {
  100. // Try to create a somewhat meaningful hash
  101. int hash = left + right * 2^8 + top * 2^16 + bottom * 2^24;
  102. return hash;
  103. }
  104. public override string ToString()
  105. {
  106. string ret = "[Margins Left={0} Right={1} Top={2} Bottom={3}]";
  107. return String.Format (ret, this.Left, this.Right, this.Top, this.Bottom);
  108. }
  109. }
  110. }