| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- using System;
- using System.Drawing;
- namespace Dongke.WinForm.Controls.InvoiceLayout
- {
- /// ====================================================================
- /// 类名:LayoutUtility
- /// <summary>
- /// Layout共通方法
- /// </summary>
- /// ====================================================================
- public static class LayoutCommon
- {
- #region 成员变量
- // DPI用
- private static float _dpi = 300;
- //private static readonly Control _control;
- // 图形
- //private static readonly Graphics _graphics;
- // 默认字体
- //private static readonly Font _defaultItemFont;
- // 选择画刷
- //private static readonly SolidBrush _selectedBrush;
- // 打印范围用绿线标识
- //private static readonly Pen _greedDotPen;
- // StringFormat
- private static readonly StringFormat _stringFormatHorizontal;
- // 文本Item的默认宽
- //private static readonly float _defaultTextItemSizeWidth = 0f;
- // 文本Item的默认高
- //private static readonly float _defaultTextItemSizeHeight = 0f;
- // JPカルチャ
- //private static readonly CultureInfo _cultureInfoJP;
- // 格式
- //public static DataTable SYSFormat = new DataTable("SYSFormat");
- // 默认值
- //public static DataTable SYSDefaultValue = new DataTable("SYSDefaultValue");
- #endregion 成员变量
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- static LayoutCommon()
- {
- //_control = new Control();
- //_graphics = _control.CreateGraphics();
- //_graphics.PageUnit = GraphicsUnit.Millimeter;
- //_selectedBrush = new SolidBrush(Color.LightGray);
- //_greedDotPen = new Pen(Color.Green, 1);
- //_greedDotPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
- //_cultureInfoJP = new CultureInfo(LayoutConsts.CULTUREINFO_JP);
- //_cultureInfoJP.DateTimeFormat.Calendar = new JapaneseCalendar();
- _stringFormatHorizontal = new StringFormat(StringFormat.GenericTypographic);
- _stringFormatHorizontal.Alignment = StringAlignment.Center;
- _stringFormatHorizontal.LineAlignment = StringAlignment.Center;
- _stringFormatHorizontal.LineAlignment = StringAlignment.Near;
- _stringFormatHorizontal.FormatFlags = StringFormatFlags.FitBlackBox
- | StringFormatFlags.NoWrap
- | StringFormatFlags.NoClip;
- //_defaultItemFont = new Font(LayoutConsts.ITEM_FONT_FAMILYNAME_DEFAULT,
- // LayoutConsts.ITEM_FONT_EMSIZE_DEFAULT);
- //float fontHeight = Convert.ToSingle(Math.Ceiling(GetMaxSizeF(_defaultItemFont).Height * 10) / 10);
- //_defaultTextItemSizeHeight = LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN + fontHeight;
- //_defaultTextItemSizeWidth = 40f;
- //Stream sysFormat = System.Reflection.Assembly.GetExecutingAssembly().
- // GetManifestResourceStream(@"Dongke.IBOSS.Framework.InvoiceLayout.Resources.SYSFormat.xml");
- //SYSFormat.ReadXml(sysFormat);
- //Stream sysDefaultValue = System.Reflection.Assembly.GetExecutingAssembly().
- // GetManifestResourceStream(@"Dongke.IBOSS.Framework.InvoiceLayout.Resources.SYSDefaultValue.xml");
- //SYSDefaultValue.ReadXml(sysDefaultValue);
- }
- #endregion 构造函数
- #region 属性
- ///// <summary>
- ///// 获取系统DPI
- ///// </summary>
- //public static Graphics ControlGraphics
- //{
- // get
- // {
- // return _graphics;
- // }
- //}
- /// <summary>
- /// 获取系统DPI
- /// </summary>
- public static float DPI
- {
- get
- {
- return _dpi;
- }
- set
- {
- _dpi = value;
- }
- }
- ///// <summary>
- ///// 默认字体
- ///// </summary>
- //public static Font DefaultItemFont
- //{
- // get
- // {
- // return (Font)_defaultItemFont.Clone();
- // }
- //}
- ///// <summary>
- ///// 选择画刷
- ///// </summary>
- //public static SolidBrush SelectedBrush
- //{
- // get
- // {
- // return _selectedBrush;
- // }
- //}
- ///// <summary>
- ///// 打印范围用绿线标识
- ///// </summary>
- //public static Pen GreedDotPen
- //{
- // get
- // {
- // return _greedDotPen;
- // }
- //}
- /// <summary>
- /// StringFormat
- /// </summary>
- public static StringFormat StringFormatHorizontal
- {
- get
- {
- return _stringFormatHorizontal;
- }
- }
- ///// <summary>
- ///// 文本Item默认宽
- ///// </summary>
- //public static float DefaultTextItemSizeWidth
- //{
- // get
- // {
- // return _defaultTextItemSizeWidth;
- // }
- //}
- ///// <summary>
- ///// 文本Item默认高
- ///// </summary>
- //public static float DefaultTextItemSizeHeight
- //{
- // get
- // {
- // return _defaultTextItemSizeHeight;
- // }
- //}
- //public static CultureInfo CultureInfoJP
- //{
- // get
- // {
- // return _cultureInfoJP;
- // }
- //}
- #endregion 属性
- #region 函数
- /// <summary>
- /// 1/100 英寸转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="pixel">1/100 英寸</param>
- /// <returns>毫米</returns>
- public static float Inch100ToMillimeter(int inch100)
- {
- float mm = inch100 * LayoutConsts.MILLIMETER_PER_INCH / 100;
- return mm;
- }
- /// <summary>
- /// 1/100 英寸转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="pixel">1/100 英寸</param>
- /// <returns>毫米</returns>
- public static float Inch100ToMillimeter(float inch100)
- {
- float mm = inch100 * LayoutConsts.MILLIMETER_PER_INCH / 100;
- return mm;
- }
- /// <summary>
- /// 磅转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="point">磅</param>
- /// <returns>毫米</returns>
- public static float PointToMillimeter(float point)
- {
- float mm = point / LayoutConsts.POINT_PER_INCH * LayoutConsts.MILLIMETER_PER_INCH;
- return mm;
- }
- /// <summary>
- /// 磅转换为像素
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="point">磅</param>
- /// <returns>像素</returns>
- public static int PointToPixel(float point)
- {
- decimal pixel = System.Convert.ToDecimal(point) /
- System.Convert.ToDecimal(LayoutConsts.POINT_PER_INCH) *
- System.Convert.ToDecimal(DPI);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- /// <summary>
- /// 毫米转换为1/100 英寸
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="mm">毫米</param>
- /// <returns>1/100 英寸</returns>
- public static int MillimeterToInch100(float mm)
- {
- float inch100 = mm / LayoutConsts.MILLIMETER_PER_INCH * 100;
- return System.Convert.ToInt32(Math.Round(inch100, 0));
- }
- /// <summary>
- /// 毫米转换为1/100 英寸
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="mm">毫米</param>
- /// <returns>1/100 英寸</returns>
- public static float MillimeterToInch100Single(float mm)
- {
- float inch100 = mm / LayoutConsts.MILLIMETER_PER_INCH * 100;
- return inch100;
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="mm">毫米</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixel(float mm)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(DPI);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- public static int MillimeterToPixel(float mm, float dpi)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(dpi);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixel(float mm, int paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(DPI) *
- System.Convert.ToDecimal(paperZoom) / 100m;
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- else
- {
- return 0;
- }
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixelDpi(float mm, float dpi)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(dpi);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixel(float mm, double paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(DPI) *
- System.Convert.ToDecimal(paperZoom);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- else
- {
- return 0;
- }
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static float MillimeterToPixelSingle(float mm, int paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(DPI) *
- System.Convert.ToDecimal(paperZoom) / 100m;
- return System.Convert.ToSingle(pixel);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="pixel">像素</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(DPI);
- return System.Convert.ToSingle(mm);
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <param name="pixel">像素</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel, int paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(DPI) /
- System.Convert.ToDecimal(paperZoom) * 100m;
- return System.Convert.ToSingle(mm);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <param name="pixel">像素</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <param name="dpi">DPI</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel, int paperZoom, float dpi)
- {
- if (0 < paperZoom)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(dpi) /
- System.Convert.ToDecimal(paperZoom) * 100m;
- return System.Convert.ToSingle(mm);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <param name="pixel">像素</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel, double paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(LayoutConsts.MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(DPI) /
- System.Convert.ToDecimal(paperZoom);
- return System.Convert.ToSingle(mm);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 截断到指定的精度。
- /// </summary>
- /// <param name="value">截断的目标</param>
- /// <param name="digits">精度</param>
- /// <returns>截断后的数值</returns>
- public static float Truncate(float value, int digits)
- {
- double v = Math.Pow(10, digits);
- return Convert.ToSingle(Math.Truncate((double)value * v) / v);
- }
- /// <summary>
- /// 截断到指定的精度。
- /// </summary>
- /// <param name="value">截断的目标</param>
- /// <param name="digits">精度</param>
- /// <returns>截断后的数值</returns>
- public static decimal Truncate(decimal value, int digits)
- {
- decimal v = (decimal)Math.Pow(10, digits);
- return Math.Truncate(value * v) / v;
- }
- ///// <summary>
- ///// 全角字符尺寸
- ///// </summary>
- ///// <param name="font">字体</param>
- ///// <returns>全角字符尺寸</returns>
- //public static SizeF GetMaxSizeF(Font font)
- //{
- // return _graphics.MeasureString(LayoutConsts.FULLSHAPE_TEXT,
- // font, PointF.Empty, _stringFormatHorizontal);
- //}
- ///// <summary>
- ///// 文字间隔计算
- ///// </summary>
- ///// <param name="width">宽</param>
- ///// <param name="charCount">文字个数</param>
- ///// <param name="font">字体</param>
- ///// <returns>文字间隔</returns>
- //public static float EqualityChars(float width, int charCount, Font font)
- //{
- // SizeF maxSizeF = GetMaxSizeF(font);
- // double equalityCharSpace;
- // if (1 < charCount)
- // {
- // equalityCharSpace =
- // ((width
- // - LayoutConsts.TEXT_MARGIN
- // - LayoutConsts.TEXT_MARGIN
- // - maxSizeF.Width)
- // / (charCount - 1))
- // - maxSizeF.Width;
- // }
- // else
- // {
- // equalityCharSpace = 0;
- // }
- // return (float)equalityCharSpace;
- //}
- ///// <summary>
- ///// 文字间隔计算
- ///// </summary>
- ///// <param name="width">宽</param>
- ///// <param name="charCount">文字个数</param>
- ///// <param name="font">字体</param>
- ///// <returns>文字间隔</returns>
- //public static decimal EqualityChars(decimal width, decimal charCount, Font font)
- //{
- // SizeF sizef = GetMaxSizeF(font);
- // decimal equalityCharSpace;
- // if (1 < charCount)
- // {
- // equalityCharSpace =
- // ((width
- // - System.Convert.ToDecimal(LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN)
- // - System.Convert.ToDecimal(sizef.Width))
- // / (charCount - 1))
- // - System.Convert.ToDecimal(sizef.Width);
- // }
- // else
- // {
- // equalityCharSpace =
- // (width
- // - System.Convert.ToDecimal(LayoutConsts.TEXT_MARGIN + LayoutConsts.TEXT_MARGIN)
- // - System.Convert.ToDecimal(sizef.Width));
- // }
- // if (equalityCharSpace < 0)
- // {
- // equalityCharSpace = 0;
- // }
- // return equalityCharSpace;
- //}
- #endregion 函数
- }
- }
|