LayoutCommon.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. namespace Dongke.WinForm.Controls.InvoiceLayout
  8. {
  9. /// ====================================================================
  10. /// 类名:LayoutUtility
  11. /// <summary>
  12. /// Layout共通方法
  13. /// </summary>
  14. /// ====================================================================
  15. internal static class LayoutCommon
  16. {
  17. #region 成员变量
  18. // DPI用
  19. private static readonly Control _control;
  20. // 图形
  21. private static readonly Graphics _graphics;
  22. private static float _dpiX = -1;
  23. // 默认字体
  24. private static readonly Font _defaultItemFont;
  25. // 选择画刷
  26. private static readonly SolidBrush _selectedBrush;
  27. // 打印范围用绿线标识
  28. private static readonly Pen _greedDotPen;
  29. // StringFormat
  30. private static readonly StringFormat _stringFormatHorizontal;
  31. // 文本Item的默认宽
  32. private static readonly float _defaultTextItemSizeWidth = 0f;
  33. // 文本Item的默认高
  34. private static readonly float _defaultTextItemSizeHeight = 0f;
  35. // JPカルチャ
  36. private static readonly CultureInfo _cultureInfoJP;
  37. // 格式
  38. public static DataTable SYSFormat = new DataTable("SYSFormat");
  39. // 默认值
  40. public static DataTable SYSDefaultValue = new DataTable("SYSDefaultValue");
  41. #endregion 成员变量
  42. #region 构造函数
  43. /// <summary>
  44. /// 构造函数
  45. /// </summary>
  46. static LayoutCommon()
  47. {
  48. _control = new Control();
  49. _graphics = _control.CreateGraphics();
  50. _graphics.PageUnit = GraphicsUnit.Millimeter;
  51. _dpiX = _graphics.DpiX;
  52. _selectedBrush = new SolidBrush(Color.LightGray);
  53. _greedDotPen = new Pen(Color.Green, 1);
  54. _greedDotPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
  55. _cultureInfoJP = new CultureInfo(LayoutConsts.CULTUREINFO_JP);
  56. _cultureInfoJP.DateTimeFormat.Calendar = new JapaneseCalendar();
  57. _stringFormatHorizontal = new StringFormat(StringFormat.GenericTypographic);
  58. _stringFormatHorizontal.Alignment = StringAlignment.Center;
  59. _stringFormatHorizontal.LineAlignment = StringAlignment.Center;
  60. _stringFormatHorizontal.LineAlignment = StringAlignment.Near;
  61. _stringFormatHorizontal.FormatFlags = StringFormatFlags.FitBlackBox
  62. | StringFormatFlags.NoWrap
  63. | StringFormatFlags.NoClip;
  64. _defaultItemFont = new Font(LayoutConsts.ITEM_FONT_FAMILYNAME_DEFAULT,
  65. LayoutConsts.ITEM_FONT_EMSIZE_DEFAULT);
  66. float fontHeight = Convert.ToSingle(Math.Ceiling(GetMaxSizeF(_defaultItemFont).Height * 10) / 10);
  67. _defaultTextItemSizeHeight = LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN + fontHeight;
  68. _defaultTextItemSizeWidth = 40f;
  69. Stream sysFormat = System.Reflection.Assembly.GetExecutingAssembly().
  70. GetManifestResourceStream(@"Dongke.WinForm.Controls.InvoiceLayout.Resources.SYSFormat.xml");
  71. SYSFormat.ReadXml(sysFormat);
  72. Stream sysDefaultValue = System.Reflection.Assembly.GetExecutingAssembly().
  73. GetManifestResourceStream(@"Dongke.WinForm.Controls.InvoiceLayout.Resources.SYSDefaultValue.xml");
  74. SYSDefaultValue.ReadXml(sysDefaultValue);
  75. }
  76. #endregion 构造函数
  77. #region 属性
  78. /// <summary>
  79. /// 获取系统DPI
  80. /// </summary>
  81. public static Graphics ControlGraphics
  82. {
  83. get
  84. {
  85. return _graphics;
  86. }
  87. }
  88. /// <summary>
  89. /// 获取系统DPI
  90. /// </summary>
  91. public static float DPI
  92. {
  93. get
  94. {
  95. if (_dpiX < 0)
  96. {
  97. _dpiX = _graphics.DpiX;
  98. }
  99. return _dpiX;
  100. }
  101. }
  102. /// <summary>
  103. /// 默认字体
  104. /// </summary>
  105. public static Font DefaultItemFont
  106. {
  107. get
  108. {
  109. return (Font)_defaultItemFont.Clone();
  110. }
  111. }
  112. /// <summary>
  113. /// 选择画刷
  114. /// </summary>
  115. public static SolidBrush SelectedBrush
  116. {
  117. get
  118. {
  119. return _selectedBrush;
  120. }
  121. }
  122. /// <summary>
  123. /// 打印范围用绿线标识
  124. /// </summary>
  125. public static Pen GreedDotPen
  126. {
  127. get
  128. {
  129. return _greedDotPen;
  130. }
  131. }
  132. /// <summary>
  133. /// StringFormat
  134. /// </summary>
  135. public static StringFormat StringFormatHorizontal
  136. {
  137. get
  138. {
  139. return _stringFormatHorizontal;
  140. }
  141. }
  142. /// <summary>
  143. /// 文本Item默认宽
  144. /// </summary>
  145. public static float DefaultTextItemSizeWidth
  146. {
  147. get
  148. {
  149. return _defaultTextItemSizeWidth;
  150. }
  151. }
  152. /// <summary>
  153. /// 文本Item默认高
  154. /// </summary>
  155. public static float DefaultTextItemSizeHeight
  156. {
  157. get
  158. {
  159. return _defaultTextItemSizeHeight;
  160. }
  161. }
  162. public static CultureInfo CultureInfoJP
  163. {
  164. get
  165. {
  166. return _cultureInfoJP;
  167. }
  168. }
  169. #endregion 属性
  170. #region 函数
  171. /// <summary>
  172. /// 1/100 英寸转换为毫米
  173. /// </summary>
  174. /// <remarks>
  175. /// 表示倍率 100%
  176. /// </remarks>
  177. /// <param name="inch100">1/100 英寸</param>
  178. /// <returns>毫米</returns>
  179. public static float Inch100ToMillimeter(int inch100)
  180. {
  181. float mm = inch100 * LayoutConsts.MILLIMETER_PER_INCH / 100;
  182. return mm;
  183. }
  184. /// <summary>
  185. /// 1/100 英寸转换为毫米
  186. /// </summary>
  187. /// <remarks>
  188. /// 表示倍率 100%
  189. /// </remarks>
  190. /// <param name="inch100">1/100 英寸</param>
  191. /// <returns>毫米</returns>
  192. public static float Inch100ToMillimeter(float inch100)
  193. {
  194. float mm = inch100 * LayoutConsts.MILLIMETER_PER_INCH / 100;
  195. return mm;
  196. }
  197. /// <summary>
  198. /// 磅转换为毫米
  199. /// </summary>
  200. /// <remarks>
  201. /// 表示倍率 100%
  202. /// </remarks>
  203. /// <param name="point">磅</param>
  204. /// <returns>毫米</returns>
  205. public static float PointToMillimeter(float point)
  206. {
  207. float mm = point / LayoutConsts.POINT_PER_INCH * LayoutConsts.MILLIMETER_PER_INCH;
  208. return mm;
  209. }
  210. /// <summary>
  211. /// 磅转换为像素
  212. /// </summary>
  213. /// <remarks>
  214. /// 表示倍率 100%
  215. /// </remarks>
  216. /// <param name="point">磅</param>
  217. /// <returns>像素</returns>
  218. public static int PointToPixel(float point)
  219. {
  220. decimal pixel = System.Convert.ToDecimal(point) /
  221. System.Convert.ToDecimal(LayoutConsts.POINT_PER_INCH) *
  222. System.Convert.ToDecimal(DPI);
  223. return System.Convert.ToInt32(Math.Round(pixel, 0));
  224. }
  225. /// <summary>
  226. /// 毫米转换为1/100 英寸
  227. /// </summary>
  228. /// <remarks>
  229. /// 表示倍率 100%
  230. /// </remarks>
  231. /// <param name="mm">毫米</param>
  232. /// <returns>1/100 英寸</returns>
  233. public static int MillimeterToInch100(float mm)
  234. {
  235. float inch100 = mm / LayoutConsts.MILLIMETER_PER_INCH * 100;
  236. return System.Convert.ToInt32(Math.Round(inch100, 0));
  237. }
  238. /// <summary>
  239. /// 毫米转换为1/100 英寸
  240. /// </summary>
  241. /// <remarks>
  242. /// 表示倍率 100%
  243. /// </remarks>
  244. /// <param name="mm">毫米</param>
  245. /// <returns>1/100 英寸</returns>
  246. public static float MillimeterToInch100Single(float mm)
  247. {
  248. float inch100 = mm / LayoutConsts.MILLIMETER_PER_INCH * 100;
  249. return inch100;
  250. }
  251. /// <summary>
  252. /// 毫米转换为像素
  253. /// </summary>
  254. /// <remarks>
  255. /// 表示倍率 100%
  256. /// </remarks>
  257. /// <param name="mm">毫米</param>
  258. /// <returns>像素</returns>
  259. public static int MillimeterToPixel(float mm)
  260. {
  261. decimal pixel = System.Convert.ToDecimal(mm) /
  262. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  263. System.Convert.ToDecimal(DPI);
  264. return System.Convert.ToInt32(Math.Round(pixel, 0));
  265. }
  266. /// <summary>
  267. /// 毫米转换为像素
  268. /// </summary>
  269. /// <param name="mm">毫米</param>
  270. /// <param name="paperZoom">表示倍率%</param>
  271. /// <returns>像素</returns>
  272. public static int MillimeterToPixel(float mm, int paperZoom)
  273. {
  274. if (0 < paperZoom)
  275. {
  276. decimal pixel = System.Convert.ToDecimal(mm) /
  277. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  278. System.Convert.ToDecimal(DPI) *
  279. System.Convert.ToDecimal(paperZoom) / 100m;
  280. return System.Convert.ToInt32(Math.Round(pixel, 0));
  281. }
  282. else
  283. {
  284. return 0;
  285. }
  286. }
  287. /// <summary>
  288. /// 毫米转换为像素
  289. /// </summary>
  290. /// <param name="mm">毫米</param>
  291. /// <param name="paperZoom">表示倍率%</param>
  292. /// <returns>像素</returns>
  293. public static int MillimeterToPixelDpi(float mm, float dpi)
  294. {
  295. decimal pixel = System.Convert.ToDecimal(mm) /
  296. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  297. System.Convert.ToDecimal(dpi);
  298. return System.Convert.ToInt32(Math.Round(pixel, 0));
  299. }
  300. /// <summary>
  301. /// 毫米转换为像素
  302. /// </summary>
  303. /// <param name="mm">毫米</param>
  304. /// <param name="paperZoom">表示倍率%</param>
  305. /// <returns>像素</returns>
  306. public static int MillimeterToPixel(float mm, double paperZoom)
  307. {
  308. if (0 < paperZoom)
  309. {
  310. decimal pixel = System.Convert.ToDecimal(mm) /
  311. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  312. System.Convert.ToDecimal(DPI) *
  313. System.Convert.ToDecimal(paperZoom);
  314. return System.Convert.ToInt32(Math.Round(pixel, 0));
  315. }
  316. else
  317. {
  318. return 0;
  319. }
  320. }
  321. /// <summary>
  322. /// 毫米转换为像素
  323. /// </summary>
  324. /// <param name="mm">毫米</param>
  325. /// <param name="paperZoom">表示倍率%</param>
  326. /// <returns>像素</returns>
  327. public static float MillimeterToPixelSingle(float mm, int paperZoom)
  328. {
  329. if (0 < paperZoom)
  330. {
  331. decimal pixel = System.Convert.ToDecimal(mm) /
  332. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  333. System.Convert.ToDecimal(DPI) *
  334. System.Convert.ToDecimal(paperZoom) / 100m;
  335. return System.Convert.ToSingle(pixel);
  336. }
  337. else
  338. {
  339. return 0f;
  340. }
  341. }
  342. /// <summary>
  343. /// 像素转换为毫米
  344. /// </summary>
  345. /// <remarks>
  346. /// 表示倍率 100%
  347. /// </remarks>
  348. /// <param name="pixel">像素</param>
  349. /// <returns>毫米</returns>
  350. public static float PixelToMillimeter(int pixel)
  351. {
  352. decimal mm = System.Convert.ToDecimal(pixel) *
  353. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  354. System.Convert.ToDecimal(DPI);
  355. return System.Convert.ToSingle(mm);
  356. }
  357. /// <summary>
  358. /// 像素转换为毫米
  359. /// </summary>
  360. /// <param name="pixel">像素</param>
  361. /// <param name="paperZoom">表示倍率%</param>
  362. /// <returns>毫米</returns>
  363. public static float PixelToMillimeter(int pixel, int paperZoom)
  364. {
  365. if (0 < paperZoom)
  366. {
  367. decimal mm = System.Convert.ToDecimal(pixel) *
  368. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  369. System.Convert.ToDecimal(DPI) /
  370. System.Convert.ToDecimal(paperZoom) * 100m;
  371. return System.Convert.ToSingle(mm);
  372. }
  373. else
  374. {
  375. return 0f;
  376. }
  377. }
  378. /// <summary>
  379. /// 像素转换为毫米
  380. /// </summary>
  381. /// <param name="pixel">像素</param>
  382. /// <param name="paperZoom">表示倍率%</param>
  383. /// <param name="dpi">DPI</param>
  384. /// <returns>毫米</returns>
  385. public static float PixelToMillimeter(int pixel, int paperZoom, float dpi)
  386. {
  387. if (0 < paperZoom)
  388. {
  389. decimal mm = System.Convert.ToDecimal(pixel) *
  390. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  391. System.Convert.ToDecimal(dpi) /
  392. System.Convert.ToDecimal(paperZoom) * 100m;
  393. return System.Convert.ToSingle(mm);
  394. }
  395. else
  396. {
  397. return 0f;
  398. }
  399. }
  400. /// <summary>
  401. /// 像素转换为毫米
  402. /// </summary>
  403. /// <param name="pixel">像素</param>
  404. /// <param name="paperZoom">表示倍率%</param>
  405. /// <returns>毫米</returns>
  406. public static float PixelToMillimeter(int pixel, double paperZoom)
  407. {
  408. if (0 < paperZoom)
  409. {
  410. decimal mm = System.Convert.ToDecimal(pixel) *
  411. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  412. System.Convert.ToDecimal(DPI) /
  413. System.Convert.ToDecimal(paperZoom);
  414. return System.Convert.ToSingle(mm);
  415. }
  416. else
  417. {
  418. return 0f;
  419. }
  420. }
  421. /// <summary>
  422. /// 截断到指定的精度。
  423. /// </summary>
  424. /// <param name="value">截断的目标</param>
  425. /// <param name="digits">精度</param>
  426. /// <returns>截断后的数值</returns>
  427. public static float Truncate(float value, int digits)
  428. {
  429. double v = Math.Pow(10, digits);
  430. return Convert.ToSingle(Math.Truncate((double)value * v) / v);
  431. }
  432. /// <summary>
  433. /// 截断到指定的精度。
  434. /// </summary>
  435. /// <param name="value">截断的目标</param>
  436. /// <param name="digits">精度</param>
  437. /// <returns>截断后的数值</returns>
  438. public static decimal Truncate(decimal value, int digits)
  439. {
  440. decimal v = (decimal)Math.Pow(10, digits);
  441. return Math.Truncate(value * v) / v;
  442. }
  443. /// <summary>
  444. /// 全角字符尺寸
  445. /// </summary>
  446. /// <param name="font">字体</param>
  447. /// <returns>全角字符尺寸</returns>
  448. public static SizeF GetMaxSizeF(Font font)
  449. {
  450. return _graphics.MeasureString(LayoutConsts.FULLSHAPE_TEXT,
  451. font, PointF.Empty, _stringFormatHorizontal);
  452. }
  453. /// <summary>
  454. /// 文字间隔计算
  455. /// </summary>
  456. /// <param name="width">宽</param>
  457. /// <param name="charCount">文字个数</param>
  458. /// <param name="font">字体</param>
  459. /// <returns>文字间隔</returns>
  460. public static float EqualityChars(float width, int charCount, Font font)
  461. {
  462. SizeF maxSizeF = GetMaxSizeF(font);
  463. double equalityCharSpace;
  464. if (1 < charCount)
  465. {
  466. equalityCharSpace =
  467. ((width
  468. - LayoutConsts.TEXT_MARGIN
  469. - LayoutConsts.TEXT_MARGIN
  470. - maxSizeF.Width)
  471. / (charCount - 1))
  472. - maxSizeF.Width;
  473. }
  474. else
  475. {
  476. equalityCharSpace = 0;
  477. }
  478. return (float)equalityCharSpace;
  479. }
  480. /// <summary>
  481. /// 文字间隔计算
  482. /// </summary>
  483. /// <param name="width">宽</param>
  484. /// <param name="charCount">文字个数</param>
  485. /// <param name="font">字体</param>
  486. /// <returns>文字间隔</returns>
  487. public static decimal EqualityChars(decimal width, decimal charCount, Font font)
  488. {
  489. SizeF sizef = GetMaxSizeF(font);
  490. decimal equalityCharSpace;
  491. if (1 < charCount)
  492. {
  493. equalityCharSpace =
  494. ((width
  495. - System.Convert.ToDecimal(LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN)
  496. - System.Convert.ToDecimal(sizef.Width))
  497. / (charCount - 1))
  498. - System.Convert.ToDecimal(sizef.Width);
  499. }
  500. else
  501. {
  502. equalityCharSpace =
  503. (width
  504. - System.Convert.ToDecimal(LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN)
  505. - System.Convert.ToDecimal(sizef.Width));
  506. }
  507. if (equalityCharSpace < 0)
  508. {
  509. equalityCharSpace = 0;
  510. }
  511. return equalityCharSpace;
  512. }
  513. #endregion 函数
  514. }
  515. }