PaperAreaSetting.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. 
  2. using System;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing.Printing;
  6. namespace Dongke.WinForm.Controls.InvoiceLayout
  7. {
  8. internal partial class PaperAreaSetting : Setting
  9. {
  10. #region 成员变量
  11. private static DataTable _paperSizes; // 纸张尺寸一览
  12. private bool _isSizeChanged = false;
  13. private bool _allowChangeSize = false;
  14. private int _indexA4 = -1;
  15. #endregion 成员变量
  16. #region 属性
  17. /// <summary>
  18. /// 纸张的宽(mm)
  19. /// </summary>
  20. [DefaultValue(210)]
  21. public float PaperWidth
  22. {
  23. get
  24. {
  25. return System.Convert.ToSingle(numerWidth.Value);
  26. }
  27. set
  28. {
  29. decimal d = System.Convert.ToDecimal(value);
  30. if (d < numerWidth.Minimum)
  31. {
  32. d = numerWidth.Minimum;
  33. }
  34. else if (numerWidth.Maximum < d)
  35. {
  36. d = numerWidth.Maximum;
  37. }
  38. if (numerWidth.Value != d)
  39. {
  40. _isSizeChanged = true;
  41. numerWidth.Value = d;
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// 纸张的高(mm)
  47. /// </summary>
  48. [DefaultValue(297)]
  49. public float PaperHeight
  50. {
  51. get
  52. {
  53. return System.Convert.ToSingle(numerHeight.Value);
  54. }
  55. set
  56. {
  57. decimal d = System.Convert.ToDecimal(value);
  58. if (d < numerHeight.Minimum)
  59. {
  60. d = numerHeight.Minimum;
  61. }
  62. else if (numerHeight.Maximum < d)
  63. {
  64. d = numerHeight.Maximum;
  65. }
  66. if (numerHeight.Value != d)
  67. {
  68. _isSizeChanged = true;
  69. numerHeight.Value = d;
  70. }
  71. }
  72. }
  73. #endregion 属性
  74. #region 构造函数
  75. /// <summary>
  76. /// 构造函数
  77. /// </summary>
  78. public PaperAreaSetting()
  79. {
  80. InitializeComponent();
  81. dgrdPaperSize.AutoGenerateColumns = false;
  82. InitializePaperSize();
  83. }
  84. #endregion 构造函数
  85. #region 函数
  86. /// <summary>
  87. /// 纸张尺寸一览
  88. /// </summary>
  89. private void InitializePaperSize()
  90. {
  91. if (_paperSizes == null)
  92. {
  93. _paperSizes = new DataTable();
  94. _paperSizes.TableName = "PaperSizes";
  95. _paperSizes.Columns.Add("SizeName", typeof(string));
  96. _paperSizes.Columns.Add("Width", typeof(float));
  97. _paperSizes.Columns.Add("Height", typeof(float));
  98. }
  99. else
  100. {
  101. _paperSizes.Clear();
  102. }
  103. float height = 0;
  104. float width = 0;
  105. int index = 0;
  106. PrintDocument pd = new PrintDocument();
  107. foreach (PaperSize ps in pd.PrinterSettings.PaperSizes)
  108. {
  109. DataRow dr = _paperSizes.NewRow();
  110. height = LayoutCommon.Inch100ToMillimeter(ps.Height);
  111. width = LayoutCommon.Inch100ToMillimeter(ps.Width);
  112. dr[0] = string.Format("{0} {1:0}mm×{2:0}mm",
  113. ps.PaperName,
  114. width,
  115. height);
  116. dr[1] = Math.Round(width);
  117. dr[2] = Math.Round(height);
  118. _paperSizes.Rows.Add(dr);
  119. if (ps.PaperName.StartsWith("A4")
  120. && 210.0f.Equals(dr[1])
  121. && 297.0f.Equals(dr[2])
  122. && _indexA4 < 0)
  123. {
  124. _indexA4 = index;
  125. }
  126. index++;
  127. }
  128. dgrdPaperSize.DataSource = _paperSizes;
  129. }
  130. #endregion 函数
  131. #region 事件处理
  132. /// <summary>
  133. /// shown
  134. /// </summary>
  135. /// <param name="sender">指定的对象</param>
  136. /// <param name="e">提供的事件数据</param>
  137. private void PaperAreaSetting_Shown(object sender, EventArgs e)
  138. {
  139. // 新建时选择A4纸张
  140. if (_isSizeChanged || _indexA4 < 0)
  141. {
  142. dgrdPaperSize.ClearSelection();
  143. }
  144. else
  145. {
  146. dgrdPaperSize.Focus();
  147. dgrdPaperSize.Rows[_indexA4].Selected = true;
  148. dgrdPaperSize.CurrentCell = dgrdPaperSize.Rows[_indexA4].Cells[0];
  149. }
  150. _allowChangeSize = true;
  151. }
  152. /// <summary>
  153. /// 【纸张尺寸一览】选择
  154. /// </summary>
  155. /// <param name="sender">指定的对象</param>
  156. /// <param name="e">提供的事件数据</param>
  157. private void dgrdPaperSize_Leave(object sender, EventArgs e)
  158. {
  159. dgrdPaperSize.ClearSelection();
  160. }
  161. /// <summary>
  162. /// 【纸张尺寸一览】选择
  163. /// </summary>
  164. /// <param name="sender">指定的对象</param>
  165. /// <param name="e">提供的事件数据</param>
  166. private void dgrdPaperSize_SelectionChanged(object sender, EventArgs e)
  167. {
  168. if (dgrdPaperSize.CurrentRow != null && _allowChangeSize
  169. && 0 < dgrdPaperSize.SelectedRows.Count)
  170. {
  171. PaperHeight =
  172. System.Convert.ToSingle(dgrdPaperSize.CurrentRow.Cells["colHeight"].Value);
  173. PaperWidth =
  174. System.Convert.ToSingle(dgrdPaperSize.CurrentRow.Cells["colWidth"].Value);
  175. }
  176. }
  177. #endregion 事件处理
  178. }
  179. }