using System;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
namespace Dongke.WinForm.Controls.InvoiceLayout
{
/// ====================================================================
/// 类名:LayoutUtility
///
/// Layout共通方法
///
/// ====================================================================
internal static class LayoutCommon
{
#region 成员变量
// DPI用
private static readonly Control _control;
// 图形
private static readonly Graphics _graphics;
private static float _dpiX = -1;
// 默认字体
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 构造函数
///
/// 构造函数
///
static LayoutCommon()
{
_control = new Control();
_graphics = _control.CreateGraphics();
_graphics.PageUnit = GraphicsUnit.Millimeter;
_dpiX = _graphics.DpiX;
_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.WinForm.Controls.InvoiceLayout.Resources.SYSFormat.xml");
SYSFormat.ReadXml(sysFormat);
Stream sysDefaultValue = System.Reflection.Assembly.GetExecutingAssembly().
GetManifestResourceStream(@"Dongke.WinForm.Controls.InvoiceLayout.Resources.SYSDefaultValue.xml");
SYSDefaultValue.ReadXml(sysDefaultValue);
}
#endregion 构造函数
#region 属性
///
/// 获取系统DPI
///
public static Graphics ControlGraphics
{
get
{
return _graphics;
}
}
///
/// 获取系统DPI
///
public static float DPI
{
get
{
if (_dpiX < 0)
{
_dpiX = _graphics.DpiX;
}
return _dpiX;
}
}
///
/// 默认字体
///
public static Font DefaultItemFont
{
get
{
return (Font)_defaultItemFont.Clone();
}
}
///
/// 选择画刷
///
public static SolidBrush SelectedBrush
{
get
{
return _selectedBrush;
}
}
///
/// 打印范围用绿线标识
///
public static Pen GreedDotPen
{
get
{
return _greedDotPen;
}
}
///
/// StringFormat
///
public static StringFormat StringFormatHorizontal
{
get
{
return _stringFormatHorizontal;
}
}
///
/// 文本Item默认宽
///
public static float DefaultTextItemSizeWidth
{
get
{
return _defaultTextItemSizeWidth;
}
}
///
/// 文本Item默认高
///
public static float DefaultTextItemSizeHeight
{
get
{
return _defaultTextItemSizeHeight;
}
}
public static CultureInfo CultureInfoJP
{
get
{
return _cultureInfoJP;
}
}
#endregion 属性
#region 函数
///
/// 1/100 英寸转换为毫米
///
///
/// 表示倍率 100%
///
/// 1/100 英寸
/// 毫米
public static float Inch100ToMillimeter(int inch100)
{
float mm = inch100 * LayoutConsts.MILLIMETER_PER_INCH / 100;
return mm;
}
///
/// 1/100 英寸转换为毫米
///
///
/// 表示倍率 100%
///
/// 1/100 英寸
/// 毫米
public static float Inch100ToMillimeter(float inch100)
{
float mm = inch100 * LayoutConsts.MILLIMETER_PER_INCH / 100;
return mm;
}
///
/// 磅转换为毫米
///
///
/// 表示倍率 100%
///
/// 磅
/// 毫米
public static float PointToMillimeter(float point)
{
float mm = point / LayoutConsts.POINT_PER_INCH * LayoutConsts.MILLIMETER_PER_INCH;
return mm;
}
///
/// 磅转换为像素
///
///
/// 表示倍率 100%
///
/// 磅
/// 像素
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));
}
///
/// 毫米转换为1/100 英寸
///
///
/// 表示倍率 100%
///
/// 毫米
/// 1/100 英寸
public static int MillimeterToInch100(float mm)
{
float inch100 = mm / LayoutConsts.MILLIMETER_PER_INCH * 100;
return System.Convert.ToInt32(Math.Round(inch100, 0));
}
///
/// 毫米转换为1/100 英寸
///
///
/// 表示倍率 100%
///
/// 毫米
/// 1/100 英寸
public static float MillimeterToInch100Single(float mm)
{
float inch100 = mm / LayoutConsts.MILLIMETER_PER_INCH * 100;
return inch100;
}
///
/// 毫米转换为像素
///
///
/// 表示倍率 100%
///
/// 毫米
/// 像素
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, 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;
}
}
///
/// 毫米转换为像素
///
/// 毫米
/// 表示倍率%
/// 像素
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));
}
///
/// 毫米转换为像素
///
/// 毫米
/// 表示倍率%
/// 像素
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;
}
}
///
/// 毫米转换为像素
///
/// 毫米
/// 表示倍率%
/// 像素
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;
}
}
///
/// 像素转换为毫米
///
///
/// 表示倍率 100%
///
/// 像素
/// 毫米
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);
}
///
/// 像素转换为毫米
///
/// 像素
/// 表示倍率%
/// 毫米
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;
}
}
///
/// 像素转换为毫米
///
/// 像素
/// 表示倍率%
/// DPI
/// 毫米
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;
}
}
///
/// 像素转换为毫米
///
/// 像素
/// 表示倍率%
/// 毫米
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;
}
}
///
/// 截断到指定的精度。
///
/// 截断的目标
/// 精度
/// 截断后的数值
public static float Truncate(float value, int digits)
{
double v = Math.Pow(10, digits);
return Convert.ToSingle(Math.Truncate((double)value * v) / v);
}
///
/// 截断到指定的精度。
///
/// 截断的目标
/// 精度
/// 截断后的数值
public static decimal Truncate(decimal value, int digits)
{
decimal v = (decimal)Math.Pow(10, digits);
return Math.Truncate(value * v) / v;
}
///
/// 全角字符尺寸
///
/// 字体
/// 全角字符尺寸
public static SizeF GetMaxSizeF(Font font)
{
return _graphics.MeasureString(LayoutConsts.FULLSHAPE_TEXT,
font, PointF.Empty, _stringFormatHorizontal);
}
///
/// 文字间隔计算
///
/// 宽
/// 文字个数
/// 字体
/// 文字间隔
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;
}
///
/// 文字间隔计算
///
/// 宽
/// 文字个数
/// 字体
/// 文字间隔
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 函数
}
}