LayoutCommon.cs 15 KB

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