LayoutCommon.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. internal 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. }
  93. ///// <summary>
  94. ///// 默认字体
  95. ///// </summary>
  96. //public static Font DefaultItemFont
  97. //{
  98. // get
  99. // {
  100. // return (Font)_defaultItemFont.Clone();
  101. // }
  102. //}
  103. ///// <summary>
  104. ///// 选择画刷
  105. ///// </summary>
  106. //public static SolidBrush SelectedBrush
  107. //{
  108. // get
  109. // {
  110. // return _selectedBrush;
  111. // }
  112. //}
  113. ///// <summary>
  114. ///// 打印范围用绿线标识
  115. ///// </summary>
  116. //public static Pen GreedDotPen
  117. //{
  118. // get
  119. // {
  120. // return _greedDotPen;
  121. // }
  122. //}
  123. /// <summary>
  124. /// StringFormat
  125. /// </summary>
  126. public static StringFormat StringFormatHorizontal
  127. {
  128. get
  129. {
  130. return _stringFormatHorizontal;
  131. }
  132. }
  133. ///// <summary>
  134. ///// 文本Item默认宽
  135. ///// </summary>
  136. //public static float DefaultTextItemSizeWidth
  137. //{
  138. // get
  139. // {
  140. // return _defaultTextItemSizeWidth;
  141. // }
  142. //}
  143. ///// <summary>
  144. ///// 文本Item默认高
  145. ///// </summary>
  146. //public static float DefaultTextItemSizeHeight
  147. //{
  148. // get
  149. // {
  150. // return _defaultTextItemSizeHeight;
  151. // }
  152. //}
  153. //public static CultureInfo CultureInfoJP
  154. //{
  155. // get
  156. // {
  157. // return _cultureInfoJP;
  158. // }
  159. //}
  160. #endregion 属性
  161. #region 函数
  162. /// <summary>
  163. /// 1/100 英寸转换为毫米
  164. /// </summary>
  165. /// <remarks>
  166. /// 表示倍率 100%
  167. /// </remarks>
  168. /// <param name="pixel">1/100 英寸</param>
  169. /// <returns>毫米</returns>
  170. public static float Inch100ToMillimeter(int inch100)
  171. {
  172. float mm = inch100 * LayoutConsts.MILLIMETER_PER_INCH / 100;
  173. return mm;
  174. }
  175. /// <summary>
  176. /// 1/100 英寸转换为毫米
  177. /// </summary>
  178. /// <remarks>
  179. /// 表示倍率 100%
  180. /// </remarks>
  181. /// <param name="pixel">1/100 英寸</param>
  182. /// <returns>毫米</returns>
  183. public static float Inch100ToMillimeter(float inch100)
  184. {
  185. float mm = inch100 * LayoutConsts.MILLIMETER_PER_INCH / 100;
  186. return mm;
  187. }
  188. /// <summary>
  189. /// 磅转换为毫米
  190. /// </summary>
  191. /// <remarks>
  192. /// 表示倍率 100%
  193. /// </remarks>
  194. /// <param name="point">磅</param>
  195. /// <returns>毫米</returns>
  196. public static float PointToMillimeter(float point)
  197. {
  198. float mm = point / LayoutConsts.POINT_PER_INCH * LayoutConsts.MILLIMETER_PER_INCH;
  199. return mm;
  200. }
  201. /// <summary>
  202. /// 磅转换为像素
  203. /// </summary>
  204. /// <remarks>
  205. /// 表示倍率 100%
  206. /// </remarks>
  207. /// <param name="point">磅</param>
  208. /// <returns>像素</returns>
  209. public static int PointToPixel(float point)
  210. {
  211. decimal pixel = System.Convert.ToDecimal(point) /
  212. System.Convert.ToDecimal(LayoutConsts.POINT_PER_INCH) *
  213. System.Convert.ToDecimal(DPI);
  214. return System.Convert.ToInt32(Math.Round(pixel, 0));
  215. }
  216. /// <summary>
  217. /// 毫米转换为1/100 英寸
  218. /// </summary>
  219. /// <remarks>
  220. /// 表示倍率 100%
  221. /// </remarks>
  222. /// <param name="mm">毫米</param>
  223. /// <returns>1/100 英寸</returns>
  224. public static int MillimeterToInch100(float mm)
  225. {
  226. float inch100 = mm / LayoutConsts.MILLIMETER_PER_INCH * 100;
  227. return System.Convert.ToInt32(Math.Round(inch100, 0));
  228. }
  229. /// <summary>
  230. /// 毫米转换为1/100 英寸
  231. /// </summary>
  232. /// <remarks>
  233. /// 表示倍率 100%
  234. /// </remarks>
  235. /// <param name="mm">毫米</param>
  236. /// <returns>1/100 英寸</returns>
  237. public static float MillimeterToInch100Single(float mm)
  238. {
  239. float inch100 = mm / LayoutConsts.MILLIMETER_PER_INCH * 100;
  240. return inch100;
  241. }
  242. /// <summary>
  243. /// 毫米转换为像素
  244. /// </summary>
  245. /// <remarks>
  246. /// 表示倍率 100%
  247. /// </remarks>
  248. /// <param name="mm">毫米</param>
  249. /// <returns>像素</returns>
  250. public static int MillimeterToPixel(float mm)
  251. {
  252. decimal pixel = System.Convert.ToDecimal(mm) /
  253. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  254. System.Convert.ToDecimal(DPI);
  255. return System.Convert.ToInt32(Math.Round(pixel, 0));
  256. }
  257. /// <summary>
  258. /// 毫米转换为像素
  259. /// </summary>
  260. /// <param name="mm">毫米</param>
  261. /// <param name="paperZoom">表示倍率%</param>
  262. /// <returns>像素</returns>
  263. public static int MillimeterToPixel(float mm, int paperZoom)
  264. {
  265. if (0 < paperZoom)
  266. {
  267. decimal pixel = System.Convert.ToDecimal(mm) /
  268. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  269. System.Convert.ToDecimal(DPI) *
  270. System.Convert.ToDecimal(paperZoom) / 100m;
  271. return System.Convert.ToInt32(Math.Round(pixel, 0));
  272. }
  273. else
  274. {
  275. return 0;
  276. }
  277. }
  278. /// <summary>
  279. /// 毫米转换为像素
  280. /// </summary>
  281. /// <param name="mm">毫米</param>
  282. /// <param name="paperZoom">表示倍率%</param>
  283. /// <returns>像素</returns>
  284. public static int MillimeterToPixel(float mm, double paperZoom)
  285. {
  286. if (0 < paperZoom)
  287. {
  288. decimal pixel = System.Convert.ToDecimal(mm) /
  289. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  290. System.Convert.ToDecimal(DPI) *
  291. System.Convert.ToDecimal(paperZoom);
  292. return System.Convert.ToInt32(Math.Round(pixel, 0));
  293. }
  294. else
  295. {
  296. return 0;
  297. }
  298. }
  299. /// <summary>
  300. /// 毫米转换为像素
  301. /// </summary>
  302. /// <param name="mm">毫米</param>
  303. /// <param name="paperZoom">表示倍率%</param>
  304. /// <returns>像素</returns>
  305. public static float MillimeterToPixelSingle(float mm, int paperZoom)
  306. {
  307. if (0 < paperZoom)
  308. {
  309. decimal pixel = System.Convert.ToDecimal(mm) /
  310. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
  311. System.Convert.ToDecimal(DPI) *
  312. System.Convert.ToDecimal(paperZoom) / 100m;
  313. return System.Convert.ToSingle(pixel);
  314. }
  315. else
  316. {
  317. return 0f;
  318. }
  319. }
  320. /// <summary>
  321. /// 像素转换为毫米
  322. /// </summary>
  323. /// <remarks>
  324. /// 表示倍率 100%
  325. /// </remarks>
  326. /// <param name="pixel">像素</param>
  327. /// <returns>毫米</returns>
  328. public static float PixelToMillimeter(int pixel)
  329. {
  330. decimal mm = System.Convert.ToDecimal(pixel) *
  331. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  332. System.Convert.ToDecimal(DPI);
  333. return System.Convert.ToSingle(mm);
  334. }
  335. /// <summary>
  336. /// 像素转换为毫米
  337. /// </summary>
  338. /// <param name="pixel">像素</param>
  339. /// <param name="paperZoom">表示倍率%</param>
  340. /// <returns>毫米</returns>
  341. public static float PixelToMillimeter(int pixel, int paperZoom)
  342. {
  343. if (0 < paperZoom)
  344. {
  345. decimal mm = System.Convert.ToDecimal(pixel) *
  346. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  347. System.Convert.ToDecimal(DPI) /
  348. System.Convert.ToDecimal(paperZoom) * 100m;
  349. return System.Convert.ToSingle(mm);
  350. }
  351. else
  352. {
  353. return 0f;
  354. }
  355. }
  356. /// <summary>
  357. /// 像素转换为毫米
  358. /// </summary>
  359. /// <param name="pixel">像素</param>
  360. /// <param name="paperZoom">表示倍率%</param>
  361. /// <param name="dpi">DPI</param>
  362. /// <returns>毫米</returns>
  363. public static float PixelToMillimeter(int pixel, int paperZoom, float dpi)
  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. /// <returns>毫米</returns>
  384. public static float PixelToMillimeter(int pixel, double paperZoom)
  385. {
  386. if (0 < paperZoom)
  387. {
  388. decimal mm = System.Convert.ToDecimal(pixel) *
  389. System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
  390. System.Convert.ToDecimal(DPI) /
  391. System.Convert.ToDecimal(paperZoom);
  392. return System.Convert.ToSingle(mm);
  393. }
  394. else
  395. {
  396. return 0f;
  397. }
  398. }
  399. /// <summary>
  400. /// 截断到指定的精度。
  401. /// </summary>
  402. /// <param name="value">截断的目标</param>
  403. /// <param name="digits">精度</param>
  404. /// <returns>截断后的数值</returns>
  405. public static float Truncate(float value, int digits)
  406. {
  407. double v = Math.Pow(10, digits);
  408. return Convert.ToSingle(Math.Truncate((double)value * v) / v);
  409. }
  410. /// <summary>
  411. /// 截断到指定的精度。
  412. /// </summary>
  413. /// <param name="value">截断的目标</param>
  414. /// <param name="digits">精度</param>
  415. /// <returns>截断后的数值</returns>
  416. public static decimal Truncate(decimal value, int digits)
  417. {
  418. decimal v = (decimal)Math.Pow(10, digits);
  419. return Math.Truncate(value * v) / v;
  420. }
  421. ///// <summary>
  422. ///// 全角字符尺寸
  423. ///// </summary>
  424. ///// <param name="font">字体</param>
  425. ///// <returns>全角字符尺寸</returns>
  426. //public static SizeF GetMaxSizeF(Font font)
  427. //{
  428. // return _graphics.MeasureString(LayoutConsts.FULLSHAPE_TEXT,
  429. // font, PointF.Empty, _stringFormatHorizontal);
  430. //}
  431. ///// <summary>
  432. ///// 文字间隔计算
  433. ///// </summary>
  434. ///// <param name="width">宽</param>
  435. ///// <param name="charCount">文字个数</param>
  436. ///// <param name="font">字体</param>
  437. ///// <returns>文字间隔</returns>
  438. //public static float EqualityChars(float width, int charCount, Font font)
  439. //{
  440. // SizeF maxSizeF = GetMaxSizeF(font);
  441. // double equalityCharSpace;
  442. // if (1 < charCount)
  443. // {
  444. // equalityCharSpace =
  445. // ((width
  446. // - LayoutConsts.TEXT_MARGIN
  447. // - LayoutConsts.TEXT_MARGIN
  448. // - maxSizeF.Width)
  449. // / (charCount - 1))
  450. // - maxSizeF.Width;
  451. // }
  452. // else
  453. // {
  454. // equalityCharSpace = 0;
  455. // }
  456. // return (float)equalityCharSpace;
  457. //}
  458. ///// <summary>
  459. ///// 文字间隔计算
  460. ///// </summary>
  461. ///// <param name="width">宽</param>
  462. ///// <param name="charCount">文字个数</param>
  463. ///// <param name="font">字体</param>
  464. ///// <returns>文字间隔</returns>
  465. //public static decimal EqualityChars(decimal width, decimal charCount, Font font)
  466. //{
  467. // SizeF sizef = GetMaxSizeF(font);
  468. // decimal equalityCharSpace;
  469. // if (1 < charCount)
  470. // {
  471. // equalityCharSpace =
  472. // ((width
  473. // - System.Convert.ToDecimal(LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN)
  474. // - System.Convert.ToDecimal(sizef.Width))
  475. // / (charCount - 1))
  476. // - System.Convert.ToDecimal(sizef.Width);
  477. // }
  478. // else
  479. // {
  480. // equalityCharSpace =
  481. // (width
  482. // - System.Convert.ToDecimal(LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN)
  483. // - System.Convert.ToDecimal(sizef.Width));
  484. // }
  485. // if (equalityCharSpace < 0)
  486. // {
  487. // equalityCharSpace = 0;
  488. // }
  489. // return equalityCharSpace;
  490. //}
  491. #endregion 函数
  492. }
  493. }