| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467 |
-
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Globalization;
- using System.Windows.Forms;
- using Curtain.Framework.Barcode;
- using Curtain.Framework.Barcode.QRCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.QrCode;
- using ZXing.QrCode.Internal;
- namespace Dongke.WinForm.Controls.InvoiceLayout
- {
- /// <summary>
- ///
- /// </summary>
- [Serializable]
- public class TextItem : LayoutItem, IBoundItem
- {
- #region 成员变量
- private string _dataMember; // 对应数据源中的字段名
- private string _displayValue; // 编辑模式显示的值
- private object _formatType; // 格式类别
- private string _format; // 文本格式
- private Font _font; // 文本字体
- private bool _isUnderline; // 下划线
- private bool _isStrikeout; // 删除线
- private bool _wrap; // 是否自动换行
- private bool _clip; // 是否剪裁延伸到边框外侧的文本
- private Color _textColor; // 文本颜色
- private TextAlignment _textAlign; // 文本对齐方式
- private TextAlignment _textAlignVertical; // 文本垂直对齐
- private float _characterSpace; // 文字间隔
- private float _lineSpace; // 文字行间隔
- private int _characterCount; // 行文字数
- private string _defaultValue; // 文本默认值
- private string _culture; // culture
- private string _defaultItemName; // 默认项目名
- private string _textItemName; // 文本项目名
- private bool _isDataBinding; // 是否数据绑定
- [NonSerialized]
- private string _text; // 文本
- [NonSerialized]
- private object _dataValue; // 数据源中的值
- [NonSerialized]
- private object _printDataValue; // 打印数据源中的值
- [NonSerialized]
- private DataBoundField _boundField; // 数据源字段
- [NonSerialized]
- private DataBoundField _printBoundField; // 打印数据源字段
- [NonSerialized]
- private Font _fontDraw; // 绘制字体
- [NonSerialized]
- private float _lineSpaceDraw; // 绘制行间隔
- [NonSerialized]
- private float _characterSpaceDraw; // 绘制文字间隔
- /// <summary>
- ///
- /// </summary>
- [NonSerialized]
- protected RectangleF _marginRectangleFDraw; // 绘制范围
- private string _itemCode; // 项目ID
- private int _itemRefClass; // 分类
- private ItemStyle _itemStyle; // 项目类别
- private string _itemSample; // 项目示例
- /// <summary>
- /// 条码打印用
- /// </summary>
- [NonSerialized]
- private string _picCodeValue = null;
- [NonSerialized]
- private int _picCodeWidth = -1;
- [NonSerialized]
- private int _picCodeHeight = -1;
- [NonSerialized]
- private Image _picCode = null;
-
- //[NonSerialized]
- //BarcodeWriter _writer = null;
- //[NonSerialized]
- //EncodingOptions _options = null;
- #endregion 成员变量
- #region 属性
- /// <summary>
- /// 对应数据源中的字段名
- /// </summary>
- public string DataMember
- {
- get
- {
- return _dataMember;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_dataMember))
- {
- _dataMember = value;
- if (Owner != null)
- {
- if (Owner.LayoutMode == LayoutMode.Preview)
- {
- Owner.RefreshTextItemBound(this);
- }
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 编辑模式显示的值
- /// </summary>
- public string DisplayValue
- {
- get
- {
- return _displayValue;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_displayValue))
- {
- _displayValue = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 格式类别
- /// </summary>
- public object FormatType
- {
- get
- {
- return _formatType;
- }
- set
- {
- _formatType = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- /// <summary>
- /// 文本格式
- /// </summary>
- public string Format
- {
- get
- {
- return _format;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_format))
- {
- _format = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本字体
- /// </summary>
- public Font Font
- {
- get
- {
- return _font;
- }
- set
- {
- if (!_font.Equals(value))
- {
- _font = value;
- _isUnderline = _font.Underline;
- _isStrikeout = _font.Strikeout;
- float zoom = base.GetZoom();
- _fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 是否自动文本换行
- /// </summary>
- public bool Wrap
- {
- get
- {
- return _wrap;
- }
- set
- {
- if (_wrap != value)
- {
- _wrap = value;
- _clip = !_wrap;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 是否剪裁延伸到边框外侧的文本
- /// </summary>
- internal bool Clip
- {
- get
- {
- _clip = !_wrap;
- return _clip;
- }
- }
- /// <summary>
- /// 文本颜色
- /// </summary>
- public Color TextColor
- {
- get
- {
- return _textColor;
- }
- set
- {
- if (!_textColor.Equals(value))
- {
- _textColor = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本对齐方式
- /// </summary>
- public TextAlignment TextAlign
- {
- get
- {
- return _textAlign;
- }
- set
- {
- if (_textAlign != value)
- {
- _textAlign = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本垂直对齐方式
- /// </summary>
- public TextAlignment TextAlignVertical
- {
- get
- {
- return _textAlignVertical;
- }
- set
- {
- if (_textAlignVertical != value)
- {
- _textAlignVertical = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文字间隔
- /// </summary>
- public float CharacterSpace
- {
- get
- {
- return _characterSpace;
- }
- set
- {
- if (_characterSpace != value)
- {
- _characterSpace = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文字行间隔
- /// </summary>
- public float LineSpace
- {
- get
- {
- return _lineSpace;
- }
- set
- {
- if (_lineSpace != value)
- {
- _lineSpace = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 行文字数
- /// </summary>
- public int CharacterCount
- {
- get
- {
- return _characterCount;
- }
- set
- {
- if (_characterCount != value)
- {
- _characterCount = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本默认值
- /// </summary>
- public string DefaultValue
- {
- get
- {
- return _defaultValue;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_defaultValue))
- {
- _defaultValue = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// culture
- /// </summary>
- public string Culture
- {
- get
- {
- return _culture;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_culture))
- {
- _culture = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本项目名
- /// </summary>
- public string TextItemName
- {
- get
- {
- return _textItemName;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_textItemName))
- {
- _textItemName = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 默认项目名
- /// </summary>
- public string DefaultItemName
- {
- get
- {
- return _defaultItemName;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_defaultItemName))
- {
- _defaultItemName = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 数据源中的值
- /// </summary>
- internal object DataValue
- {
- get
- {
- return _dataValue;
- }
- set
- {
- if (value != _dataValue
- || (value != null && !value.Equals(_dataValue)))
- {
- _dataValue = value;
- }
- }
- }
- /// <summary>
- /// 打印数据源中的值
- /// </summary>
- internal object PrintDataValue
- {
- get
- {
- return _printDataValue;
- }
- set
- {
- if (value != _printDataValue
- || (value != null && !value.Equals(_printDataValue)))
- {
- _printDataValue = value;
- }
- }
- }
- /// <summary>
- /// 是否数据绑定
- /// </summary>
- internal bool IsDataBinding
- {
- get
- {
- return _isDataBinding;
- }
- set
- {
- _isDataBinding = value;
- }
- }
- /// <summary>
- /// 数据源字段
- /// </summary>
- internal DataBoundField BoundField
- {
- get
- {
- return _boundField;
- }
- set
- {
- _boundField = value;
- }
- }
- /// <summary>
- /// 打印数据源字段
- /// </summary>
- internal DataBoundField PrintBoundField
- {
- get
- {
- return _printBoundField;
- }
- set
- {
- _printBoundField = value;
- }
- }
- /// <summary>
- /// 绘制字体
- /// </summary>
- internal Font FontDraw
- {
- get
- {
- return _fontDraw;
- }
- }
- /// <summary>
- /// 绘制文字间隔
- /// </summary>
- internal float CharacterSpaceDraw
- {
- get
- {
- return _characterSpaceDraw;
- }
- }
- /// <summary>
- /// 绘制行间隔
- /// </summary>
- internal float LineSpaceDraw
- {
- get
- {
- return _lineSpaceDraw;
- }
- }
- /// <summary>
- /// 项目编码
- /// </summary>
- public string ItemCode
- {
- get
- {
- return this._itemCode;
- }
- set
- {
- if (this._itemCode != value)
- {
- this._itemCode = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- if (string.IsNullOrEmpty(this._itemCode))
- {
- this._isDataBinding = false;
- }
- else
- {
- this._isDataBinding = true;
- }
- }
- }
- /// <summary>
- /// 分类
- /// </summary>
- public int ItemRefClass
- {
- get
- {
- return _itemRefClass;
- }
- set
- {
- if (value != _itemRefClass)
- {
- _itemRefClass = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 项目类别
- /// </summary>
- public ItemStyle ItemStyle
- {
- get
- {
- return _itemStyle;
- }
- set
- {
- if (value != _itemStyle)
- {
- _itemStyle = value;
- this._picCode = null;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 项目示例
- /// </summary>
- public string ItemSample
- {
- get
- {
- return _itemSample;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_itemSample))
- {
- _itemSample = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public bool PicColumn
- {
- get;
- set;
- }
- /// <summary>
- ///
- /// </summary>
- public bool PicMargin
- {
- get;
- set;
- }
- /// <summary>
- ///
- /// </summary>
- public bool FixedRatio
- {
- get;
- set;
- }
- #endregion 属性
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- internal TextItem()
- : this(null)
- {
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- internal TextItem(LayoutBox box)
- : this(box, ItemType.Text)
- {
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- internal TextItem(LayoutBox box, ItemType itemType)
- : base(box, itemType)
- {
- _width = LayoutCommon.DefaultTextItemSizeWidth;
- _height = LayoutCommon.DefaultTextItemSizeHeight;
- _displayValue = string.Empty;
- _format = string.Empty;
- _defaultValue = string.Empty;
- _font = LayoutCommon.DefaultItemFont;
- _fontDraw = LayoutCommon.DefaultItemFont;
- _textColor = Color.Black;
- _textAlign = TextAlignment.LeftOrTop;
- _characterSpace = 0f;
- _lineSpace = 0f;
- _characterCount = 0;
- _dataValue = null;
- _text = string.Empty;
- _itemRefClass = 0;
- _itemStyle = ItemStyle.Other;
- _isDataBinding = false;
- _boundField = new DataBoundField();
- _boundField.IsDataBound = false;
- _boundField.BoundFieldIndex = -1;
- _printBoundField = new DataBoundField();
- _printBoundField.IsDataBound = false;
- _printBoundField.BoundFieldIndex = -1;
- _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
- _textItemName = _defaultItemName;
- }
- #endregion 构造函数
- #region 函数
- /// <summary>
- /// 格式化数据源中的值
- /// </summary>
- private string FormatDataValue(object value)
- {
- string result = null;
- if (value != null && value != DBNull.Value)
- {
- if (_itemStyle == ItemStyle.Date)
- {
- result = value.ToString();
- DateTime dateTime;
- if (DateTime.TryParse(result, out dateTime))
- {
- if (string.IsNullOrEmpty(_format))
- {
- result = dateTime.ToString(LayoutConsts.DATETIME_FRORMAT);
- }
- else
- {
- if (LayoutConsts.CULTUREINFO_JP.Equals(_culture))
- {
- result = dateTime.ToString(_format, LayoutCommon.CultureInfoJP);
- }
- else
- {
- // MMM
- if (_format.Contains(LayoutConsts.DATETIME_FRORMAT_MMM))
- {
- if (string.IsNullOrEmpty(_culture))
- {
- result = dateTime.ToString(_format).ToUpper();
- }
- else
- {
- result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture)).ToUpper();
- }
- }
- else if (string.IsNullOrEmpty(_culture))
- {
- result = dateTime.ToString(_format);
- }
- else
- {
- result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture));
- }
- }
- }
- }
- }
- else if (_itemStyle == ItemStyle.Sign)
- {
- if (string.IsNullOrEmpty(_format))
- {
- result = value.ToString();
- }
- else
- {
- result = _format;
- }
- }
- else
- {
- result = value.ToString();
- }
- }
- return result;
- }
- /// <summary>
- /// 文本内容设置
- /// </summary>
- internal object ResetTextValue(object value, bool isDataBound)
- {
- if (Owner == null)
- {
- return _displayValue;
- }
- if (Owner.LayoutMode == LayoutMode.Preview)
- {
- if (!string.IsNullOrEmpty(_dataMember))
- {
- if (isDataBound)
- {
- object result = Owner.SetItemValueFormattingArgs(this, value, _format);
- if (PicColumn)
- {
- if (result != null && result is Image)
- {
- return result;
- }
- if (value != null && value is byte[])
- {
- System.IO.MemoryStream ms = new System.IO.MemoryStream(value as byte[]);
- return Image.FromStream(ms);
- }
- return value;
- }
- if (result != null)
- {
- return result.ToString();
- }
- if (value != null && value != DBNull.Value)
- {
- return FormatDataValue(value);
- }
- return _defaultValue;
- }
- //return _defaultValue;
- return "";
- }
- return _displayValue;
- }
- else
- {
- //object result = Owner.SetItemValueFormattingArgs(this, value, _format);
- object result = Owner.SetItemValueFormattingArgs(this, _displayValue, _format);
- if (result != null)
- {
- return result.ToString();
- }
- return _displayValue;
- }
- }
- /// <summary>
- /// 显示Item属性设置画面
- /// </summary>
- /// <returns>
- /// DialogResult.OK:选中的Item有效,设置成功<br/>
- /// DialogResult.Cancel:选中的Item有效,取消设置<br/>
- /// DialogResult.None:LayoutBox不是编辑模式,或选中的Item不是一个
- /// </returns>
- protected override DialogResult ShowItemPropertyDialogInner()
- {
- using (TextItemSetting itemPropertySetting = new TextItemSetting())
- {
- itemPropertySetting.IsNew = false;
- itemPropertySetting.IsDataBinding = IsDataBinding;
- itemPropertySetting.TextLocationX = Left;
- itemPropertySetting.TextLocationY = Top;
- itemPropertySetting.TextWidth = Width;
- itemPropertySetting.TextHeight = Height;
- itemPropertySetting.Culture = Culture;
- itemPropertySetting.LineSpace = LineSpace;
- itemPropertySetting.CharacterSpace = CharacterSpace;
- itemPropertySetting.CharacterCount = CharacterCount;
- itemPropertySetting.TextAlign = TextAlign;
- itemPropertySetting.TextAlignVertical = TextAlignVertical;
- itemPropertySetting.TextPreview = DisplayValue;
- itemPropertySetting.TextColor = TextColor;
- itemPropertySetting.ItemStyle = ItemStyle;
- itemPropertySetting.ItemName = TextItemName;
- itemPropertySetting.TextFormat = Format;
- itemPropertySetting.TextFont = Font;
- itemPropertySetting.Wrap = Wrap;
- itemPropertySetting.DefaultValue = DefaultValue;
- itemPropertySetting.chkFixedRatio.Checked = FixedRatio;
- itemPropertySetting.chkM.Checked = PicMargin;
- itemPropertySetting.chkPic.Checked = PicColumn;
- itemPropertySetting.chkFixedRatio.Enabled = PicColumn;
- itemPropertySetting.chkM.Enabled = PicColumn;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- Left = itemPropertySetting.TextLocationX;
- Top = itemPropertySetting.TextLocationY;
- Width = itemPropertySetting.TextWidth;
- Height = itemPropertySetting.TextHeight;
- LineSpace = itemPropertySetting.LineSpace;
- CharacterSpace = itemPropertySetting.CharacterSpace;
- CharacterCount = itemPropertySetting.CharacterCount;
- Font = itemPropertySetting.TextFont;
- Wrap = itemPropertySetting.Wrap;
- TextAlign = itemPropertySetting.TextAlign;
- TextAlignVertical = itemPropertySetting.TextAlignVertical;
- TextColor = itemPropertySetting.TextColor;
- DisplayValue = itemPropertySetting.TextPreview;
- ItemStyle = itemPropertySetting.ItemStyle;
- TextItemName = itemPropertySetting.ItemName;
- Format = itemPropertySetting.TextFormat;
- Culture = itemPropertySetting.Culture;
- DefaultValue = itemPropertySetting.DefaultValue;
- FixedRatio = itemPropertySetting.chkFixedRatio.Checked;
- PicMargin = itemPropertySetting.chkM.Checked;
- PicColumn = itemPropertySetting.chkPic.Checked;
- ResetItemFrame();
- SetDrawProperty();
- Owner.Dirty = true;
- }
- return result;
- }
- }
- /// <summary>
- /// Item初始化
- /// </summary>
- /// <param name="box">LayoutBox</param>
- /// <param name="isNewID">是否生成新ID</param>
- /// <returns>是否成功</returns>
- internal override bool Init(LayoutBox box, bool isNewID)
- {
- if (_fontDraw == null)
- {
- if (_font == null)
- {
- _font = LayoutCommon.DefaultItemFont;
- _fontDraw = LayoutCommon.DefaultItemFont;
- }
- else
- {
- _fontDraw = new Font(_font.FontFamily, _font.Size, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
- }
- }
- _boundField.IsDataBound = false;
- _boundField.BoundFieldIndex = -1;
- _printBoundField.IsDataBound = false;
- _printBoundField.BoundFieldIndex = -1;
- bool isInit = base.Init(box, isNewID);
- if (isNewID)
- {
- _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
- _textItemName = _defaultItemName;
- }
- return isInit;
- }
- /// <summary>
- /// 初始化Item绘制用属性
- /// </summary>
- internal override float SetDrawProperty()
- {
- float zoom = base.SetDrawProperty();
- _fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _fontDraw.Style);
- _lineSpaceDraw = _lineSpace * zoom;
- if (_characterCount < 1)
- {
- _characterSpaceDraw = _characterSpace * zoom;
- }
- _marginRectangleFDraw = new RectangleF();
- _marginRectangleFDraw.X = (Left + LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Y = (Top + LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Width = (Width - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
- return zoom;
- }
- /// <summary>
- /// 初始化Item绘制用属性
- /// </summary>
- internal override float SetRectangleF()
- {
- float zoom = base.SetRectangleF();
- if (_characterCount < 1)
- {
- _characterSpaceDraw = _characterSpace * zoom;
- }
- _marginRectangleFDraw = new RectangleF();
- _marginRectangleFDraw.X = (Left + LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Y = (Top + LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Width = (Width - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
- return zoom;
- }
- /// <summary>
- /// Item图形上的OnPaint事件
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- protected override void OnPaint(object sender, PaintEventArgs e)
- {
- DrawTextItem(_dataValue, _boundField, e.Graphics, RectangleFDraw, _marginRectangleFDraw, false);
- }
- internal void DrawTextItem(object data, DataBoundField dataBoundField, Graphics graphics, RectangleF rectangleM, RectangleF rectangle, bool isPrint)
- {
- object value = ResetTextValue(data, dataBoundField.IsDataBound);
- if (value == null)
- {
- return;
- }
- try
- {
- if (this._itemStyle == ItemStyle.Barcode)
- {
- Image pic = null;
- if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
- this.SimpleShape.Height == this._picCodeHeight &&
- value.ToString() == _picCodeValue)
- {
- pic = this._picCode;
- }
- if (pic == null)
- {
- BarcodeWriter writer = new BarcodeWriter();
- EncodingOptions options = new EncodingOptions()
- {
- PureBarcode = true,
- Margin = 0,
- };
- writer.Options = options;
- writer.Format = BarcodeFormat.CODE_128;
- //writer.Options.ErrorCorrection = ErrorCorrectionLevel.H;
- writer.Options.Width = this.SimpleShape.Width;
- writer.Options.Height = this.SimpleShape.Height;
- pic = writer.Write(value.ToString());
- if (!isPrint)
- {
- this._picCode = pic;
- _picCodeValue = value.ToString();
- _picCodeWidth = this.SimpleShape.Width;
- _picCodeHeight = this.SimpleShape.Height;
- }
- }
- try
- {
- if (pic != null)
- {
- LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rectangleM, pic);
- }
- }
- catch
- {
- }
- finally
- {
- if (pic != null && isPrint)
- {
- pic.Dispose();
- }
- }
- return;
- }
- if (this._itemStyle == ItemStyle.QRCode)
- {
- Image pic = null;
- if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
- this.SimpleShape.Height == this._picCodeHeight &&
- value.ToString() == _picCodeValue)
- {
- pic = this._picCode;
- }
- if (pic == null)
- {
- if (this.Wrap)
- {
- // 缩放图片
- QRCodeEncodingOptions options = new QRCodeEncodingOptions();
- options.ECLevel = QRECLevel.H;
- QRCodeData qrd = QRCodeHelper.Encode(value.ToString(),
- this.SimpleShape.Width,
- this.SimpleShape.Height,
- BarcodeMargin.Empty, options);
- QRCodeDrawingOptions options1 = new QRCodeDrawingOptions();
- options1.DrawGraphics = graphics;
- options1.DpiX = graphics.DpiX;
- options1.DpiY = graphics.DpiY;
- //qrd.QRWidth = this.SimpleShape.Width;
- //qrd.QRHeight = this.SimpleShape.Height;
- pic = QRCodeHelper.DrawQRDataImage(qrd, options1);
- }
- else
- {
- BarcodeWriter writer = new BarcodeWriter();
- QrCodeEncodingOptions options = new QrCodeEncodingOptions();
- options.DisableECI = true;
- //设置内容编码
- //options.CharacterSet = "UTF-8";
- //options.QrVersion = 3;
- options.ErrorCorrection = ErrorCorrectionLevel.H;
- //设置二维码的宽度和高度
- options.Width = this.SimpleShape.Width;
- options.Height = this.SimpleShape.Height;
- //设置二维码的边距,单位不是固定像素
- options.Margin = 0;
- writer.Encoder = new QRCodeWriterMy();
- writer.Options = options;
- writer.Format = BarcodeFormat.QR_CODE;
- pic = writer.Write(value.ToString());
- }
- if (!isPrint)
- {
- this._picCode = pic;
- _picCodeValue = value.ToString();
- _picCodeWidth = this.SimpleShape.Width;
- _picCodeHeight = this.SimpleShape.Height;
- }
- }
- try
- {
- if (pic != null)
- {
- LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rectangleM, pic);
- }
- }
- catch
- {
- }
- finally
- {
- if (pic != null && isPrint)
- {
- pic.Dispose();
- }
- }
- return;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- if (PicColumn && value is Image)
- {
- Image image = value as Image;
- if (image != null)
- {
- RectangleF rfp = PicMargin ? rectangle : rectangleM;
- if (FixedRatio)
- {
- // 背景图片的高宽比例
- float imgWHRate = (float)image.Width / (float)image.Height;
- // 打印纸张的高宽比例
- float paperWHRate = rfp.Width / rfp.Height;
- // 背景图片打印范围
- RectangleF newImgRectangleF = RectangleF.Empty;
- // 缩放背景图片,但保持图片高宽比例
- if (imgWHRate > paperWHRate)
- {
- newImgRectangleF.Width = rfp.Width;
- newImgRectangleF.Height = rfp.Width / imgWHRate;
- newImgRectangleF.X = rfp.X;
- newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
- }
- else
- {
- newImgRectangleF.Width = rfp.Height * imgWHRate;
- newImgRectangleF.Height = rfp.Height;
- newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
- newImgRectangleF.Y = rfp.Y;
- }
- rfp = newImgRectangleF;
- }
- // 在指定的范围绘制背景图片
- LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
- }
- return;
- }
- LayoutUtility.DrawText(graphics,
- GraphicsUnit.Millimeter,
- rectangleM,
- rectangle,
- value.ToString(),
- _fontDraw,
- _textColor,
- _lineSpaceDraw,
- _characterSpaceDraw,
- _characterCount,
- _textAlign,
- _textAlignVertical,
- _wrap,
- Clip,
- Selected);
- }
- #endregion 函数
- public Image pictureProcess(Image sourceImage, int targetWidth, int targetHeight)
- {
- int width;//图片最终的宽
- int height;//图片最终的高
- try
- {
- System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat;
- Bitmap targetPicture = new Bitmap(targetWidth, targetHeight);
- Graphics g = Graphics.FromImage(targetPicture);
- g.Clear(Color.White);
- //计算缩放图片的大小
- if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight)
- {
- width = targetWidth;
- height = (width * sourceImage.Height) / sourceImage.Width;
- }
- else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight)
- {
- height = targetHeight;
- width = (height * sourceImage.Width) / sourceImage.Height;
- }
- else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight)
- {
- width = sourceImage.Width;
- height = sourceImage.Height;
- }
- else
- {
- width = targetWidth;
- height = (width * sourceImage.Height) / sourceImage.Width;
- if (height > targetHeight)
- {
- height = targetHeight;
- width = (height * sourceImage.Width) / sourceImage.Height;
- }
- }
- g.DrawImage(sourceImage, (targetWidth - width) / 2, (targetHeight - height) / 2, width, height);
- sourceImage.Dispose();
- return targetPicture;
- }
- catch// (Exception ex)
- {
- }
- return null;
- }
- }
- public class QRCodeWriterMy : Writer
- {
- private const int QUIET_ZONE_SIZE = 4;
- /// <summary>
- /// Encode a barcode using the default settings.
- /// </summary>
- /// <param name="contents">The contents to encode in the barcode</param>
- /// <param name="format">The barcode format to generate</param>
- /// <param name="width">The preferred width in pixels</param>
- /// <param name="height">The preferred height in pixels</param>
- /// <returns>
- /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
- /// </returns>
- public BitMatrix encode(string contents, BarcodeFormat format, int width, int height)
- {
- return encode(contents, format, width, height, null);
- }
- /// <summary>
- /// </summary>
- /// <param name="contents">The contents to encode in the barcode</param>
- /// <param name="format">The barcode format to generate</param>
- /// <param name="width">The preferred width in pixels</param>
- /// <param name="height">The preferred height in pixels</param>
- /// <param name="hints">Additional parameters to supply to the encoder</param>
- /// <returns>
- /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
- /// </returns>
- public BitMatrix encode(string contents,
- BarcodeFormat format,
- int width,
- int height,
- IDictionary<EncodeHintType, object> hints)
- {
- if (String.IsNullOrEmpty(contents))
- {
- throw new ArgumentException("Found empty contents");
- }
- if (format != BarcodeFormat.QR_CODE)
- {
- throw new ArgumentException("Can only encode QR_CODE, but got " + format);
- }
- if (width < 0 || height < 0)
- {
- throw new ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
- }
- var errorCorrectionLevel = ErrorCorrectionLevel.L;
- int quietZone = QUIET_ZONE_SIZE;
- if (hints != null)
- {
- if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
- {
- var requestedECLevel = hints[EncodeHintType.ERROR_CORRECTION];
- if (requestedECLevel != null)
- {
- errorCorrectionLevel = requestedECLevel as ErrorCorrectionLevel;
- if (errorCorrectionLevel == null)
- {
- switch (requestedECLevel.ToString().ToUpper())
- {
- case "L":
- errorCorrectionLevel = ErrorCorrectionLevel.L;
- break;
- case "M":
- errorCorrectionLevel = ErrorCorrectionLevel.M;
- break;
- case "Q":
- errorCorrectionLevel = ErrorCorrectionLevel.Q;
- break;
- case "H":
- errorCorrectionLevel = ErrorCorrectionLevel.H;
- break;
- default:
- errorCorrectionLevel = ErrorCorrectionLevel.L;
- break;
- }
- }
- }
- }
- if (hints.ContainsKey(EncodeHintType.MARGIN))
- {
- var quietZoneInt = hints[EncodeHintType.MARGIN];
- if (quietZoneInt != null)
- {
- quietZone = Convert.ToInt32(quietZoneInt.ToString());
- }
- }
- }
- var code = Encoder.encode(contents, errorCorrectionLevel, hints);
- BitMatrix bm = renderResult(code, width, height, quietZone);
- hints[EncodeHintType.WIDTH] = bm.Width;
- hints[EncodeHintType.HEIGHT] = bm.Height;
- return bm;
- }
- // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
- // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
- private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone)
- {
- var input = code.Matrix;
- if (input == null)
- {
- throw new InvalidOperationException();
- }
- int inputWidth = input.Width;
- int inputHeight = input.Height;
- int qrWidth = inputWidth + (quietZone << 1);
- int qrHeight = inputHeight + (quietZone << 1);
- int outputWidth = Math.Max(width, qrWidth);
- int outputHeight = Math.Max(height, qrHeight);
- int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
- // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
- // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
- // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
- // handle all the padding from 100x100 (the actual QR) up to 200x160.
- int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
- int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
- //int leftPadding = 0;//(outputWidth - (inputWidth * multiple)) / 2;
- //int topPadding = 0;//(outputHeight - (inputHeight * multiple)) / 2;
- //outputWidth = (inputWidth * multiple);
- //outputHeight = (inputHeight * multiple);
- var output = new BitMatrix(outputWidth, outputHeight);
- for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple)
- {
- // Write the contents of this row of the barcode
- for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple)
- {
- if (input[inputX, inputY] == 1)
- {
- output.setRegion(outputX, outputY, multiple, multiple);
- //int mX = outputWidth - outputX;
- //int mY = outputHeight - outputY;
- //mX = Math.Min(mX, multiple);
- //mY = Math.Min(mY, multiple);
- //output.setRegion(outputX, outputY, mX, mY);
- }
- }
- }
- return output;
- }
- }
- }
|