LayoutCommon.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 MillimeterToPixel(float mm, double paperZoom)
  294. {
  295. if (0 < paperZoom)
  296. {
  297. decimal pixel = System.Convert.ToDecimal(mm) /
  298. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  299. System.Convert.ToDecimal(DPI) *
  300. System.Convert.ToDecimal(paperZoom);
  301. return System.Convert.ToInt32(Math.Round(pixel, 0));
  302. }
  303. else
  304. {
  305. return 0;
  306. }
  307. }
  308. /// <summary>
  309. /// 毫米转换为像素
  310. /// </summary>
  311. /// <param name="mm">毫米</param>
  312. /// <param name="paperZoom">表示倍率%</param>
  313. /// <returns>像素</returns>
  314. public static float MillimeterToPixelSingle(float mm, int paperZoom)
  315. {
  316. if (0 < paperZoom)
  317. {
  318. decimal pixel = System.Convert.ToDecimal(mm) /
  319. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  320. System.Convert.ToDecimal(DPI) *
  321. System.Convert.ToDecimal(paperZoom) / 100m;
  322. return System.Convert.ToSingle(pixel);
  323. }
  324. else
  325. {
  326. return 0f;
  327. }
  328. }
  329. /// <summary>
  330. /// 像素转换为毫米
  331. /// </summary>
  332. /// <remarks>
  333. /// 表示倍率 100%
  334. /// </remarks>
  335. /// <param name="pixel">像素</param>
  336. /// <returns>毫米</returns>
  337. public static float PixelToMillimeter(int pixel)
  338. {
  339. decimal mm = System.Convert.ToDecimal(pixel) *
  340. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  341. System.Convert.ToDecimal(DPI);
  342. return System.Convert.ToSingle(mm);
  343. }
  344. /// <summary>
  345. /// 像素转换为毫米
  346. /// </summary>
  347. /// <param name="pixel">像素</param>
  348. /// <param name="paperZoom">表示倍率%</param>
  349. /// <returns>毫米</returns>
  350. public static float PixelToMillimeter(int pixel, int paperZoom)
  351. {
  352. if (0 < paperZoom)
  353. {
  354. decimal mm = System.Convert.ToDecimal(pixel) *
  355. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  356. System.Convert.ToDecimal(DPI) /
  357. System.Convert.ToDecimal(paperZoom) * 100m;
  358. return System.Convert.ToSingle(mm);
  359. }
  360. else
  361. {
  362. return 0f;
  363. }
  364. }
  365. /// <summary>
  366. /// 像素转换为毫米
  367. /// </summary>
  368. /// <param name="pixel">像素</param>
  369. /// <param name="paperZoom">表示倍率%</param>
  370. /// <param name="dpi">DPI</param>
  371. /// <returns>毫米</returns>
  372. public static float PixelToMillimeter(int pixel, int paperZoom, float dpi)
  373. {
  374. if (0 < paperZoom)
  375. {
  376. decimal mm = System.Convert.ToDecimal(pixel) *
  377. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  378. System.Convert.ToDecimal(dpi) /
  379. System.Convert.ToDecimal(paperZoom) * 100m;
  380. return System.Convert.ToSingle(mm);
  381. }
  382. else
  383. {
  384. return 0f;
  385. }
  386. }
  387. /// <summary>
  388. /// 像素转换为毫米
  389. /// </summary>
  390. /// <param name="pixel">像素</param>
  391. /// <param name="paperZoom">表示倍率%</param>
  392. /// <returns>毫米</returns>
  393. public static float PixelToMillimeter(int pixel, double paperZoom)
  394. {
  395. if (0 < paperZoom)
  396. {
  397. decimal mm = System.Convert.ToDecimal(pixel) *
  398. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  399. System.Convert.ToDecimal(DPI) /
  400. System.Convert.ToDecimal(paperZoom);
  401. return System.Convert.ToSingle(mm);
  402. }
  403. else
  404. {
  405. return 0f;
  406. }
  407. }
  408. /// <summary>
  409. /// 截断到指定的精度。
  410. /// </summary>
  411. /// <param name="value">截断的目标</param>
  412. /// <param name="digits">精度</param>
  413. /// <returns>截断后的数值</returns>
  414. public static float Truncate(float value, int digits)
  415. {
  416. double v = Math.Pow(10, digits);
  417. return Convert.ToSingle(Math.Truncate((double)value * v) / v);
  418. }
  419. /// <summary>
  420. /// 截断到指定的精度。
  421. /// </summary>
  422. /// <param name="value">截断的目标</param>
  423. /// <param name="digits">精度</param>
  424. /// <returns>截断后的数值</returns>
  425. public static decimal Truncate(decimal value, int digits)
  426. {
  427. decimal v = (decimal)Math.Pow(10, digits);
  428. return Math.Truncate(value * v) / v;
  429. }
  430. /// <summary>
  431. /// 全角字符尺寸
  432. /// </summary>
  433. /// <param name="font">字体</param>
  434. /// <returns>全角字符尺寸</returns>
  435. public static SizeF GetMaxSizeF(Font font)
  436. {
  437. return _graphics.MeasureString(LayoutConsts.FULLSHAPE_TEXT,
  438. font, PointF.Empty, _stringFormatHorizontal);
  439. }
  440. /// <summary>
  441. /// 文字间隔计算
  442. /// </summary>
  443. /// <param name="width">宽</param>
  444. /// <param name="charCount">文字个数</param>
  445. /// <param name="font">字体</param>
  446. /// <returns>文字间隔</returns>
  447. public static float EqualityChars(float width, int charCount, Font font)
  448. {
  449. SizeF maxSizeF = GetMaxSizeF(font);
  450. double equalityCharSpace;
  451. if (1 < charCount)
  452. {
  453. equalityCharSpace =
  454. ((width
  455. - LayoutConsts.TEXT_MARGIN
  456. - LayoutConsts.TEXT_MARGIN
  457. - maxSizeF.Width)
  458. / (charCount - 1))
  459. - maxSizeF.Width;
  460. }
  461. else
  462. {
  463. equalityCharSpace = 0;
  464. }
  465. return (float)equalityCharSpace;
  466. }
  467. /// <summary>
  468. /// 文字间隔计算
  469. /// </summary>
  470. /// <param name="width">宽</param>
  471. /// <param name="charCount">文字个数</param>
  472. /// <param name="font">字体</param>
  473. /// <returns>文字间隔</returns>
  474. public static decimal EqualityChars(decimal width, decimal charCount, Font font)
  475. {
  476. SizeF sizef = GetMaxSizeF(font);
  477. decimal equalityCharSpace;
  478. if (1 < charCount)
  479. {
  480. equalityCharSpace =
  481. ((width
  482. - System.Convert.ToDecimal(LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN)
  483. - System.Convert.ToDecimal(sizef.Width))
  484. / (charCount - 1))
  485. - System.Convert.ToDecimal(sizef.Width);
  486. }
  487. else
  488. {
  489. equalityCharSpace =
  490. (width
  491. - System.Convert.ToDecimal(LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN)
  492. - System.Convert.ToDecimal(sizef.Width));
  493. }
  494. if (equalityCharSpace < 0)
  495. {
  496. equalityCharSpace = 0;
  497. }
  498. return equalityCharSpace;
  499. }
  500. #endregion 函数
  501. }
  502. }