TextItem.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Globalization;
  6. using System.Windows.Forms;
  7. using Curtain.Framework.Barcode;
  8. using Curtain.Framework.Barcode.QRCode;
  9. using ZXing;
  10. using ZXing.Common;
  11. using ZXing.QrCode;
  12. using ZXing.QrCode.Internal;
  13. namespace Dongke.WinForm.Controls.InvoiceLayout
  14. {
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. [Serializable]
  19. public class TextItem : LayoutItem, IBoundItem
  20. {
  21. #region 成员变量
  22. private string _dataMember; // 对应数据源中的字段名
  23. private string _displayValue; // 编辑模式显示的值
  24. private object _formatType; // 格式类别
  25. private string _format; // 文本格式
  26. private Font _font; // 文本字体
  27. private bool _isUnderline; // 下划线
  28. private bool _isStrikeout; // 删除线
  29. private bool _wrap; // 是否自动换行
  30. private bool _clip; // 是否剪裁延伸到边框外侧的文本
  31. private Color _textColor; // 文本颜色
  32. private TextAlignment _textAlign; // 文本对齐方式
  33. private TextAlignment _textAlignVertical; // 文本垂直对齐
  34. private float _characterSpace; // 文字间隔
  35. private float _lineSpace; // 文字行间隔
  36. private int _characterCount; // 行文字数
  37. private string _defaultValue; // 文本默认值
  38. private string _culture; // culture
  39. private string _defaultItemName; // 默认项目名
  40. private string _textItemName; // 文本项目名
  41. private bool _isDataBinding; // 是否数据绑定
  42. [NonSerialized]
  43. private string _text; // 文本
  44. [NonSerialized]
  45. private object _dataValue; // 数据源中的值
  46. [NonSerialized]
  47. private object _printDataValue; // 打印数据源中的值
  48. [NonSerialized]
  49. private DataBoundField _boundField; // 数据源字段
  50. [NonSerialized]
  51. private DataBoundField _printBoundField; // 打印数据源字段
  52. [NonSerialized]
  53. private Font _fontDraw; // 绘制字体
  54. [NonSerialized]
  55. private float _lineSpaceDraw; // 绘制行间隔
  56. [NonSerialized]
  57. private float _characterSpaceDraw; // 绘制文字间隔
  58. /// <summary>
  59. ///
  60. /// </summary>
  61. [NonSerialized]
  62. protected RectangleF _marginRectangleFDraw; // 绘制范围
  63. private string _itemCode; // 项目ID
  64. private int _itemRefClass; // 分类
  65. private ItemStyle _itemStyle; // 项目类别
  66. private string _itemSample; // 项目示例
  67. /// <summary>
  68. /// 条码打印用
  69. /// </summary>
  70. [NonSerialized]
  71. private string _picCodeValue = null;
  72. [NonSerialized]
  73. private int _picCodeWidth = -1;
  74. [NonSerialized]
  75. private int _picCodeHeight = -1;
  76. [NonSerialized]
  77. private Image _picCode = null;
  78. //[NonSerialized]
  79. //BarcodeWriter _writer = null;
  80. //[NonSerialized]
  81. //EncodingOptions _options = null;
  82. #endregion 成员变量
  83. #region 属性
  84. /// <summary>
  85. /// 对应数据源中的字段名
  86. /// </summary>
  87. public string DataMember
  88. {
  89. get
  90. {
  91. return _dataMember;
  92. }
  93. set
  94. {
  95. if (string.IsNullOrEmpty(value))
  96. {
  97. value = string.Empty;
  98. }
  99. if (!value.Equals(_dataMember))
  100. {
  101. _dataMember = value;
  102. if (Owner != null)
  103. {
  104. if (Owner.LayoutMode == LayoutMode.Preview)
  105. {
  106. Owner.RefreshTextItemBound(this);
  107. }
  108. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  109. }
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 编辑模式显示的值
  115. /// </summary>
  116. public string DisplayValue
  117. {
  118. get
  119. {
  120. return _displayValue;
  121. }
  122. set
  123. {
  124. if (string.IsNullOrEmpty(value))
  125. {
  126. value = string.Empty;
  127. }
  128. if (!value.Equals(_displayValue))
  129. {
  130. _displayValue = value;
  131. if (Owner != null)
  132. {
  133. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  134. }
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// 格式类别
  140. /// </summary>
  141. public object FormatType
  142. {
  143. get
  144. {
  145. return _formatType;
  146. }
  147. set
  148. {
  149. _formatType = value;
  150. if (Owner != null)
  151. {
  152. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// 文本格式
  158. /// </summary>
  159. public string Format
  160. {
  161. get
  162. {
  163. return _format;
  164. }
  165. set
  166. {
  167. if (string.IsNullOrEmpty(value))
  168. {
  169. value = string.Empty;
  170. }
  171. if (!value.Equals(_format))
  172. {
  173. _format = value;
  174. if (Owner != null)
  175. {
  176. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  177. }
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// 文本字体
  183. /// </summary>
  184. public Font Font
  185. {
  186. get
  187. {
  188. return _font;
  189. }
  190. set
  191. {
  192. if (!_font.Equals(value))
  193. {
  194. _font = value;
  195. _isUnderline = _font.Underline;
  196. _isStrikeout = _font.Strikeout;
  197. float zoom = base.GetZoom();
  198. _fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
  199. if (Owner != null)
  200. {
  201. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  202. }
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// 是否自动文本换行
  208. /// </summary>
  209. public bool Wrap
  210. {
  211. get
  212. {
  213. return _wrap;
  214. }
  215. set
  216. {
  217. if (_wrap != value)
  218. {
  219. _wrap = value;
  220. _clip = !_wrap;
  221. if (Owner != null)
  222. {
  223. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  224. }
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// 是否剪裁延伸到边框外侧的文本
  230. /// </summary>
  231. internal bool Clip
  232. {
  233. get
  234. {
  235. _clip = !_wrap;
  236. return _clip;
  237. }
  238. }
  239. /// <summary>
  240. /// 文本颜色
  241. /// </summary>
  242. public Color TextColor
  243. {
  244. get
  245. {
  246. return _textColor;
  247. }
  248. set
  249. {
  250. if (!_textColor.Equals(value))
  251. {
  252. _textColor = value;
  253. if (Owner != null)
  254. {
  255. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  256. }
  257. }
  258. }
  259. }
  260. /// <summary>
  261. /// 文本对齐方式
  262. /// </summary>
  263. public TextAlignment TextAlign
  264. {
  265. get
  266. {
  267. return _textAlign;
  268. }
  269. set
  270. {
  271. if (_textAlign != value)
  272. {
  273. _textAlign = value;
  274. if (Owner != null)
  275. {
  276. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  277. }
  278. }
  279. }
  280. }
  281. /// <summary>
  282. /// 文本垂直对齐方式
  283. /// </summary>
  284. public TextAlignment TextAlignVertical
  285. {
  286. get
  287. {
  288. return _textAlignVertical;
  289. }
  290. set
  291. {
  292. if (_textAlignVertical != value)
  293. {
  294. _textAlignVertical = value;
  295. if (Owner != null)
  296. {
  297. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  298. }
  299. }
  300. }
  301. }
  302. /// <summary>
  303. /// 文字间隔
  304. /// </summary>
  305. public float CharacterSpace
  306. {
  307. get
  308. {
  309. return _characterSpace;
  310. }
  311. set
  312. {
  313. if (_characterSpace != value)
  314. {
  315. _characterSpace = value;
  316. if (Owner != null)
  317. {
  318. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  319. }
  320. }
  321. }
  322. }
  323. /// <summary>
  324. /// 文字行间隔
  325. /// </summary>
  326. public float LineSpace
  327. {
  328. get
  329. {
  330. return _lineSpace;
  331. }
  332. set
  333. {
  334. if (_lineSpace != value)
  335. {
  336. _lineSpace = value;
  337. if (Owner != null)
  338. {
  339. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  340. }
  341. }
  342. }
  343. }
  344. /// <summary>
  345. /// 行文字数
  346. /// </summary>
  347. public int CharacterCount
  348. {
  349. get
  350. {
  351. return _characterCount;
  352. }
  353. set
  354. {
  355. if (_characterCount != value)
  356. {
  357. _characterCount = value;
  358. if (Owner != null)
  359. {
  360. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  361. }
  362. }
  363. }
  364. }
  365. /// <summary>
  366. /// 文本默认值
  367. /// </summary>
  368. public string DefaultValue
  369. {
  370. get
  371. {
  372. return _defaultValue;
  373. }
  374. set
  375. {
  376. if (string.IsNullOrEmpty(value))
  377. {
  378. value = string.Empty;
  379. }
  380. if (!value.Equals(_defaultValue))
  381. {
  382. _defaultValue = value;
  383. if (Owner != null)
  384. {
  385. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  386. }
  387. }
  388. }
  389. }
  390. /// <summary>
  391. /// culture
  392. /// </summary>
  393. public string Culture
  394. {
  395. get
  396. {
  397. return _culture;
  398. }
  399. set
  400. {
  401. if (string.IsNullOrEmpty(value))
  402. {
  403. value = string.Empty;
  404. }
  405. if (!value.Equals(_culture))
  406. {
  407. _culture = value;
  408. if (Owner != null)
  409. {
  410. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  411. }
  412. }
  413. }
  414. }
  415. /// <summary>
  416. /// 文本项目名
  417. /// </summary>
  418. public string TextItemName
  419. {
  420. get
  421. {
  422. return _textItemName;
  423. }
  424. set
  425. {
  426. if (string.IsNullOrEmpty(value))
  427. {
  428. value = string.Empty;
  429. }
  430. if (!value.Equals(_textItemName))
  431. {
  432. _textItemName = value;
  433. if (Owner != null)
  434. {
  435. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  436. }
  437. }
  438. }
  439. }
  440. /// <summary>
  441. /// 默认项目名
  442. /// </summary>
  443. public string DefaultItemName
  444. {
  445. get
  446. {
  447. return _defaultItemName;
  448. }
  449. set
  450. {
  451. if (string.IsNullOrEmpty(value))
  452. {
  453. value = string.Empty;
  454. }
  455. if (!value.Equals(_defaultItemName))
  456. {
  457. _defaultItemName = value;
  458. if (Owner != null)
  459. {
  460. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  461. }
  462. }
  463. }
  464. }
  465. /// <summary>
  466. /// 数据源中的值
  467. /// </summary>
  468. internal object DataValue
  469. {
  470. get
  471. {
  472. return _dataValue;
  473. }
  474. set
  475. {
  476. if (value != _dataValue
  477. || (value != null && !value.Equals(_dataValue)))
  478. {
  479. _dataValue = value;
  480. }
  481. }
  482. }
  483. /// <summary>
  484. /// 打印数据源中的值
  485. /// </summary>
  486. internal object PrintDataValue
  487. {
  488. get
  489. {
  490. return _printDataValue;
  491. }
  492. set
  493. {
  494. if (value != _printDataValue
  495. || (value != null && !value.Equals(_printDataValue)))
  496. {
  497. _printDataValue = value;
  498. }
  499. }
  500. }
  501. /// <summary>
  502. /// 是否数据绑定
  503. /// </summary>
  504. internal bool IsDataBinding
  505. {
  506. get
  507. {
  508. return _isDataBinding;
  509. }
  510. set
  511. {
  512. _isDataBinding = value;
  513. }
  514. }
  515. /// <summary>
  516. /// 数据源字段
  517. /// </summary>
  518. internal DataBoundField BoundField
  519. {
  520. get
  521. {
  522. return _boundField;
  523. }
  524. set
  525. {
  526. _boundField = value;
  527. }
  528. }
  529. /// <summary>
  530. /// 打印数据源字段
  531. /// </summary>
  532. internal DataBoundField PrintBoundField
  533. {
  534. get
  535. {
  536. return _printBoundField;
  537. }
  538. set
  539. {
  540. _printBoundField = value;
  541. }
  542. }
  543. /// <summary>
  544. /// 绘制字体
  545. /// </summary>
  546. internal Font FontDraw
  547. {
  548. get
  549. {
  550. return _fontDraw;
  551. }
  552. }
  553. /// <summary>
  554. /// 绘制文字间隔
  555. /// </summary>
  556. internal float CharacterSpaceDraw
  557. {
  558. get
  559. {
  560. return _characterSpaceDraw;
  561. }
  562. }
  563. /// <summary>
  564. /// 绘制行间隔
  565. /// </summary>
  566. internal float LineSpaceDraw
  567. {
  568. get
  569. {
  570. return _lineSpaceDraw;
  571. }
  572. }
  573. /// <summary>
  574. /// 项目编码
  575. /// </summary>
  576. public string ItemCode
  577. {
  578. get
  579. {
  580. return this._itemCode;
  581. }
  582. set
  583. {
  584. if (this._itemCode != value)
  585. {
  586. this._itemCode = value;
  587. if (Owner != null)
  588. {
  589. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  590. }
  591. }
  592. if (string.IsNullOrEmpty(this._itemCode))
  593. {
  594. this._isDataBinding = false;
  595. }
  596. else
  597. {
  598. this._isDataBinding = true;
  599. }
  600. }
  601. }
  602. /// <summary>
  603. /// 分类
  604. /// </summary>
  605. public int ItemRefClass
  606. {
  607. get
  608. {
  609. return _itemRefClass;
  610. }
  611. set
  612. {
  613. if (value != _itemRefClass)
  614. {
  615. _itemRefClass = value;
  616. if (Owner != null)
  617. {
  618. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  619. }
  620. }
  621. }
  622. }
  623. /// <summary>
  624. /// 项目类别
  625. /// </summary>
  626. public ItemStyle ItemStyle
  627. {
  628. get
  629. {
  630. return _itemStyle;
  631. }
  632. set
  633. {
  634. if (value != _itemStyle)
  635. {
  636. _itemStyle = value;
  637. this._picCode = null;
  638. if (Owner != null)
  639. {
  640. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  641. }
  642. }
  643. }
  644. }
  645. /// <summary>
  646. /// 项目示例
  647. /// </summary>
  648. public string ItemSample
  649. {
  650. get
  651. {
  652. return _itemSample;
  653. }
  654. set
  655. {
  656. if (string.IsNullOrEmpty(value))
  657. {
  658. value = string.Empty;
  659. }
  660. if (!value.Equals(_itemSample))
  661. {
  662. _itemSample = value;
  663. if (Owner != null)
  664. {
  665. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  666. }
  667. }
  668. }
  669. }
  670. /// <summary>
  671. ///
  672. /// </summary>
  673. public bool PicColumn
  674. {
  675. get;
  676. set;
  677. }
  678. /// <summary>
  679. ///
  680. /// </summary>
  681. public bool PicMargin
  682. {
  683. get;
  684. set;
  685. }
  686. /// <summary>
  687. ///
  688. /// </summary>
  689. public bool FixedRatio
  690. {
  691. get;
  692. set;
  693. }
  694. #endregion 属性
  695. #region 构造函数
  696. /// <summary>
  697. /// 构造函数
  698. /// </summary>
  699. internal TextItem()
  700. : this(null)
  701. {
  702. }
  703. /// <summary>
  704. /// 构造函数
  705. /// </summary>
  706. internal TextItem(LayoutBox box)
  707. : this(box, ItemType.Text)
  708. {
  709. }
  710. /// <summary>
  711. /// 构造函数
  712. /// </summary>
  713. internal TextItem(LayoutBox box, ItemType itemType)
  714. : base(box, itemType)
  715. {
  716. _width = LayoutCommon.DefaultTextItemSizeWidth;
  717. _height = LayoutCommon.DefaultTextItemSizeHeight;
  718. _displayValue = string.Empty;
  719. _format = string.Empty;
  720. _defaultValue = string.Empty;
  721. _font = LayoutCommon.DefaultItemFont;
  722. _fontDraw = LayoutCommon.DefaultItemFont;
  723. _textColor = Color.Black;
  724. _textAlign = TextAlignment.LeftOrTop;
  725. _characterSpace = 0f;
  726. _lineSpace = 0f;
  727. _characterCount = 0;
  728. _dataValue = null;
  729. _text = string.Empty;
  730. _itemRefClass = 0;
  731. _itemStyle = ItemStyle.Other;
  732. _isDataBinding = false;
  733. _boundField = new DataBoundField();
  734. _boundField.IsDataBound = false;
  735. _boundField.BoundFieldIndex = -1;
  736. _printBoundField = new DataBoundField();
  737. _printBoundField.IsDataBound = false;
  738. _printBoundField.BoundFieldIndex = -1;
  739. _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
  740. _textItemName = _defaultItemName;
  741. }
  742. #endregion 构造函数
  743. #region 函数
  744. /// <summary>
  745. /// 格式化数据源中的值
  746. /// </summary>
  747. private string FormatDataValue(object value)
  748. {
  749. string result = null;
  750. if (value != null && value != DBNull.Value)
  751. {
  752. if (_itemStyle == ItemStyle.Date)
  753. {
  754. result = value.ToString();
  755. DateTime dateTime;
  756. if (DateTime.TryParse(result, out dateTime))
  757. {
  758. if (string.IsNullOrEmpty(_format))
  759. {
  760. result = dateTime.ToString(LayoutConsts.DATETIME_FRORMAT);
  761. }
  762. else
  763. {
  764. if (LayoutConsts.CULTUREINFO_JP.Equals(_culture))
  765. {
  766. result = dateTime.ToString(_format, LayoutCommon.CultureInfoJP);
  767. }
  768. else
  769. {
  770. // MMM
  771. if (_format.Contains(LayoutConsts.DATETIME_FRORMAT_MMM))
  772. {
  773. if (string.IsNullOrEmpty(_culture))
  774. {
  775. result = dateTime.ToString(_format).ToUpper();
  776. }
  777. else
  778. {
  779. result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture)).ToUpper();
  780. }
  781. }
  782. else if (string.IsNullOrEmpty(_culture))
  783. {
  784. result = dateTime.ToString(_format);
  785. }
  786. else
  787. {
  788. result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture));
  789. }
  790. }
  791. }
  792. }
  793. }
  794. else if (_itemStyle == ItemStyle.Sign)
  795. {
  796. if (string.IsNullOrEmpty(_format))
  797. {
  798. result = value.ToString();
  799. }
  800. else
  801. {
  802. result = _format;
  803. }
  804. }
  805. else
  806. {
  807. result = value.ToString();
  808. }
  809. }
  810. return result;
  811. }
  812. /// <summary>
  813. /// 文本内容设置
  814. /// </summary>
  815. internal object ResetTextValue(object value, bool isDataBound)
  816. {
  817. if (Owner == null)
  818. {
  819. return _displayValue;
  820. }
  821. if (Owner.LayoutMode == LayoutMode.Preview)
  822. {
  823. if (!string.IsNullOrEmpty(_dataMember))
  824. {
  825. if (isDataBound)
  826. {
  827. object result = Owner.SetItemValueFormattingArgs(this, value, _format);
  828. if (PicColumn)
  829. {
  830. if (result != null && result is Image)
  831. {
  832. return result;
  833. }
  834. if (value != null && value is byte[])
  835. {
  836. System.IO.MemoryStream ms = new System.IO.MemoryStream(value as byte[]);
  837. return Image.FromStream(ms);
  838. }
  839. return value;
  840. }
  841. if (result != null)
  842. {
  843. return result.ToString();
  844. }
  845. if (value != null && value != DBNull.Value)
  846. {
  847. return FormatDataValue(value);
  848. }
  849. return _defaultValue;
  850. }
  851. //return _defaultValue;
  852. return "";
  853. }
  854. return _displayValue;
  855. }
  856. else
  857. {
  858. //object result = Owner.SetItemValueFormattingArgs(this, value, _format);
  859. object result = Owner.SetItemValueFormattingArgs(this, _displayValue, _format);
  860. if (result != null)
  861. {
  862. return result.ToString();
  863. }
  864. return _displayValue;
  865. }
  866. }
  867. /// <summary>
  868. /// 显示Item属性设置画面
  869. /// </summary>
  870. /// <returns>
  871. /// DialogResult.OK:选中的Item有效,设置成功<br/>
  872. /// DialogResult.Cancel:选中的Item有效,取消设置<br/>
  873. /// DialogResult.None:LayoutBox不是编辑模式,或选中的Item不是一个
  874. /// </returns>
  875. protected override DialogResult ShowItemPropertyDialogInner()
  876. {
  877. using (TextItemSetting itemPropertySetting = new TextItemSetting())
  878. {
  879. itemPropertySetting.IsNew = false;
  880. itemPropertySetting.IsDataBinding = IsDataBinding;
  881. itemPropertySetting.TextLocationX = Left;
  882. itemPropertySetting.TextLocationY = Top;
  883. itemPropertySetting.TextWidth = Width;
  884. itemPropertySetting.TextHeight = Height;
  885. itemPropertySetting.Culture = Culture;
  886. itemPropertySetting.LineSpace = LineSpace;
  887. itemPropertySetting.CharacterSpace = CharacterSpace;
  888. itemPropertySetting.CharacterCount = CharacterCount;
  889. itemPropertySetting.TextAlign = TextAlign;
  890. itemPropertySetting.TextAlignVertical = TextAlignVertical;
  891. itemPropertySetting.TextPreview = DisplayValue;
  892. itemPropertySetting.TextColor = TextColor;
  893. itemPropertySetting.ItemStyle = ItemStyle;
  894. itemPropertySetting.ItemName = TextItemName;
  895. itemPropertySetting.TextFormat = Format;
  896. itemPropertySetting.TextFont = Font;
  897. itemPropertySetting.Wrap = Wrap;
  898. itemPropertySetting.DefaultValue = DefaultValue;
  899. itemPropertySetting.chkFixedRatio.Checked = FixedRatio;
  900. itemPropertySetting.chkM.Checked = PicMargin;
  901. itemPropertySetting.chkPic.Checked = PicColumn;
  902. itemPropertySetting.chkFixedRatio.Enabled = PicColumn;
  903. itemPropertySetting.chkM.Enabled = PicColumn;
  904. DialogResult result = itemPropertySetting.ShowDialog();
  905. if (result == DialogResult.OK)
  906. {
  907. Left = itemPropertySetting.TextLocationX;
  908. Top = itemPropertySetting.TextLocationY;
  909. Width = itemPropertySetting.TextWidth;
  910. Height = itemPropertySetting.TextHeight;
  911. LineSpace = itemPropertySetting.LineSpace;
  912. CharacterSpace = itemPropertySetting.CharacterSpace;
  913. CharacterCount = itemPropertySetting.CharacterCount;
  914. Font = itemPropertySetting.TextFont;
  915. Wrap = itemPropertySetting.Wrap;
  916. TextAlign = itemPropertySetting.TextAlign;
  917. TextAlignVertical = itemPropertySetting.TextAlignVertical;
  918. TextColor = itemPropertySetting.TextColor;
  919. DisplayValue = itemPropertySetting.TextPreview;
  920. ItemStyle = itemPropertySetting.ItemStyle;
  921. TextItemName = itemPropertySetting.ItemName;
  922. Format = itemPropertySetting.TextFormat;
  923. Culture = itemPropertySetting.Culture;
  924. DefaultValue = itemPropertySetting.DefaultValue;
  925. FixedRatio = itemPropertySetting.chkFixedRatio.Checked;
  926. PicMargin = itemPropertySetting.chkM.Checked;
  927. PicColumn = itemPropertySetting.chkPic.Checked;
  928. ResetItemFrame();
  929. SetDrawProperty();
  930. Owner.Dirty = true;
  931. }
  932. return result;
  933. }
  934. }
  935. /// <summary>
  936. /// Item初始化
  937. /// </summary>
  938. /// <param name="box">LayoutBox</param>
  939. /// <param name="isNewID">是否生成新ID</param>
  940. /// <returns>是否成功</returns>
  941. internal override bool Init(LayoutBox box, bool isNewID)
  942. {
  943. if (_fontDraw == null)
  944. {
  945. if (_font == null)
  946. {
  947. _font = LayoutCommon.DefaultItemFont;
  948. _fontDraw = LayoutCommon.DefaultItemFont;
  949. }
  950. else
  951. {
  952. _fontDraw = new Font(_font.FontFamily, _font.Size, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
  953. }
  954. }
  955. _boundField.IsDataBound = false;
  956. _boundField.BoundFieldIndex = -1;
  957. _printBoundField.IsDataBound = false;
  958. _printBoundField.BoundFieldIndex = -1;
  959. bool isInit = base.Init(box, isNewID);
  960. if (isNewID)
  961. {
  962. _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
  963. _textItemName = _defaultItemName;
  964. }
  965. return isInit;
  966. }
  967. /// <summary>
  968. /// 初始化Item绘制用属性
  969. /// </summary>
  970. internal override float SetDrawProperty()
  971. {
  972. float zoom = base.SetDrawProperty();
  973. _fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _fontDraw.Style);
  974. _lineSpaceDraw = _lineSpace * zoom;
  975. if (_characterCount < 1)
  976. {
  977. _characterSpaceDraw = _characterSpace * zoom;
  978. }
  979. _marginRectangleFDraw = new RectangleF();
  980. _marginRectangleFDraw.X = (Left + LayoutConsts.TEXT_MARGIN) * zoom;
  981. _marginRectangleFDraw.Y = (Top + LayoutConsts.TEXT_MARGIN) * zoom;
  982. _marginRectangleFDraw.Width = (Width - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
  983. _marginRectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
  984. return zoom;
  985. }
  986. /// <summary>
  987. /// 初始化Item绘制用属性
  988. /// </summary>
  989. internal override float SetRectangleF()
  990. {
  991. float zoom = base.SetRectangleF();
  992. if (_characterCount < 1)
  993. {
  994. _characterSpaceDraw = _characterSpace * zoom;
  995. }
  996. _marginRectangleFDraw = new RectangleF();
  997. _marginRectangleFDraw.X = (Left + LayoutConsts.TEXT_MARGIN) * zoom;
  998. _marginRectangleFDraw.Y = (Top + LayoutConsts.TEXT_MARGIN) * zoom;
  999. _marginRectangleFDraw.Width = (Width - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
  1000. _marginRectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
  1001. return zoom;
  1002. }
  1003. /// <summary>
  1004. /// Item图形上的OnPaint事件
  1005. /// </summary>
  1006. /// <param name="sender">指定的对象</param>
  1007. /// <param name="e">提供的事件数据</param>
  1008. protected override void OnPaint(object sender, PaintEventArgs e)
  1009. {
  1010. DrawTextItem(_dataValue, _boundField, e.Graphics, RectangleFDraw, _marginRectangleFDraw, false);
  1011. }
  1012. internal void DrawTextItem(object data, DataBoundField dataBoundField, Graphics graphics, RectangleF rectangleM, RectangleF rectangle, bool isPrint)
  1013. {
  1014. object value = ResetTextValue(data, dataBoundField.IsDataBound);
  1015. if (value == null)
  1016. {
  1017. return;
  1018. }
  1019. try
  1020. {
  1021. if (this._itemStyle == ItemStyle.Barcode)
  1022. {
  1023. Image pic = null;
  1024. if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
  1025. this.SimpleShape.Height == this._picCodeHeight &&
  1026. value.ToString() == _picCodeValue)
  1027. {
  1028. pic = this._picCode;
  1029. }
  1030. if (pic == null)
  1031. {
  1032. BarcodeWriter writer = new BarcodeWriter();
  1033. EncodingOptions options = new EncodingOptions()
  1034. {
  1035. PureBarcode = true,
  1036. Margin = 0,
  1037. };
  1038. writer.Options = options;
  1039. writer.Format = BarcodeFormat.CODE_128;
  1040. //writer.Options.ErrorCorrection = ErrorCorrectionLevel.H;
  1041. writer.Options.Width = this.SimpleShape.Width;
  1042. writer.Options.Height = this.SimpleShape.Height;
  1043. pic = writer.Write(value.ToString());
  1044. if (!isPrint)
  1045. {
  1046. this._picCode = pic;
  1047. _picCodeValue = value.ToString();
  1048. _picCodeWidth = this.SimpleShape.Width;
  1049. _picCodeHeight = this.SimpleShape.Height;
  1050. }
  1051. }
  1052. try
  1053. {
  1054. if (pic != null)
  1055. {
  1056. LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rectangleM, pic);
  1057. }
  1058. }
  1059. catch
  1060. {
  1061. }
  1062. finally
  1063. {
  1064. if (pic != null && isPrint)
  1065. {
  1066. pic.Dispose();
  1067. }
  1068. }
  1069. return;
  1070. }
  1071. if (this._itemStyle == ItemStyle.QRCode)
  1072. {
  1073. Image pic = null;
  1074. if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
  1075. this.SimpleShape.Height == this._picCodeHeight &&
  1076. value.ToString() == _picCodeValue)
  1077. {
  1078. pic = this._picCode;
  1079. }
  1080. if (pic == null)
  1081. {
  1082. if (this.Wrap)
  1083. {
  1084. // 缩放图片
  1085. QRCodeEncodingOptions options = new QRCodeEncodingOptions();
  1086. options.ECLevel = QRECLevel.H;
  1087. QRCodeData qrd = QRCodeHelper.Encode(value.ToString(),
  1088. this.SimpleShape.Width,
  1089. this.SimpleShape.Height,
  1090. BarcodeMargin.Empty, options);
  1091. QRCodeDrawingOptions options1 = new QRCodeDrawingOptions();
  1092. options1.DrawGraphics = graphics;
  1093. options1.DpiX = graphics.DpiX;
  1094. options1.DpiY = graphics.DpiY;
  1095. //qrd.QRWidth = this.SimpleShape.Width;
  1096. //qrd.QRHeight = this.SimpleShape.Height;
  1097. pic = QRCodeHelper.DrawQRDataImage(qrd, options1);
  1098. }
  1099. else
  1100. {
  1101. BarcodeWriter writer = new BarcodeWriter();
  1102. QrCodeEncodingOptions options = new QrCodeEncodingOptions();
  1103. options.DisableECI = true;
  1104. //设置内容编码
  1105. //options.CharacterSet = "UTF-8";
  1106. //options.QrVersion = 3;
  1107. options.ErrorCorrection = ErrorCorrectionLevel.H;
  1108. //设置二维码的宽度和高度
  1109. options.Width = this.SimpleShape.Width;
  1110. options.Height = this.SimpleShape.Height;
  1111. //设置二维码的边距,单位不是固定像素
  1112. options.Margin = 0;
  1113. writer.Encoder = new QRCodeWriterMy();
  1114. writer.Options = options;
  1115. writer.Format = BarcodeFormat.QR_CODE;
  1116. pic = writer.Write(value.ToString());
  1117. }
  1118. if (!isPrint)
  1119. {
  1120. this._picCode = pic;
  1121. _picCodeValue = value.ToString();
  1122. _picCodeWidth = this.SimpleShape.Width;
  1123. _picCodeHeight = this.SimpleShape.Height;
  1124. }
  1125. }
  1126. try
  1127. {
  1128. if (pic != null)
  1129. {
  1130. LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rectangleM, pic);
  1131. }
  1132. }
  1133. catch
  1134. {
  1135. }
  1136. finally
  1137. {
  1138. if (pic != null && isPrint)
  1139. {
  1140. pic.Dispose();
  1141. }
  1142. }
  1143. return;
  1144. }
  1145. }
  1146. catch (Exception ex)
  1147. {
  1148. throw ex;
  1149. }
  1150. if (PicColumn && value is Image)
  1151. {
  1152. Image image = value as Image;
  1153. if (image != null)
  1154. {
  1155. RectangleF rfp = PicMargin ? rectangle : rectangleM;
  1156. if (FixedRatio)
  1157. {
  1158. // 背景图片的高宽比例
  1159. float imgWHRate = (float)image.Width / (float)image.Height;
  1160. // 打印纸张的高宽比例
  1161. float paperWHRate = rfp.Width / rfp.Height;
  1162. // 背景图片打印范围
  1163. RectangleF newImgRectangleF = RectangleF.Empty;
  1164. // 缩放背景图片,但保持图片高宽比例
  1165. if (imgWHRate > paperWHRate)
  1166. {
  1167. newImgRectangleF.Width = rfp.Width;
  1168. newImgRectangleF.Height = rfp.Width / imgWHRate;
  1169. newImgRectangleF.X = rfp.X;
  1170. newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
  1171. }
  1172. else
  1173. {
  1174. newImgRectangleF.Width = rfp.Height * imgWHRate;
  1175. newImgRectangleF.Height = rfp.Height;
  1176. newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
  1177. newImgRectangleF.Y = rfp.Y;
  1178. }
  1179. rfp = newImgRectangleF;
  1180. }
  1181. // 在指定的范围绘制背景图片
  1182. LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
  1183. }
  1184. return;
  1185. }
  1186. LayoutUtility.DrawText(graphics,
  1187. GraphicsUnit.Millimeter,
  1188. rectangleM,
  1189. rectangle,
  1190. value.ToString(),
  1191. _fontDraw,
  1192. _textColor,
  1193. _lineSpaceDraw,
  1194. _characterSpaceDraw,
  1195. _characterCount,
  1196. _textAlign,
  1197. _textAlignVertical,
  1198. _wrap,
  1199. Clip,
  1200. Selected);
  1201. }
  1202. #endregion 函数
  1203. public Image pictureProcess(Image sourceImage, int targetWidth, int targetHeight)
  1204. {
  1205. int width;//图片最终的宽
  1206. int height;//图片最终的高
  1207. try
  1208. {
  1209. System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat;
  1210. Bitmap targetPicture = new Bitmap(targetWidth, targetHeight);
  1211. Graphics g = Graphics.FromImage(targetPicture);
  1212. g.Clear(Color.White);
  1213. //计算缩放图片的大小
  1214. if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight)
  1215. {
  1216. width = targetWidth;
  1217. height = (width * sourceImage.Height) / sourceImage.Width;
  1218. }
  1219. else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight)
  1220. {
  1221. height = targetHeight;
  1222. width = (height * sourceImage.Width) / sourceImage.Height;
  1223. }
  1224. else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight)
  1225. {
  1226. width = sourceImage.Width;
  1227. height = sourceImage.Height;
  1228. }
  1229. else
  1230. {
  1231. width = targetWidth;
  1232. height = (width * sourceImage.Height) / sourceImage.Width;
  1233. if (height > targetHeight)
  1234. {
  1235. height = targetHeight;
  1236. width = (height * sourceImage.Width) / sourceImage.Height;
  1237. }
  1238. }
  1239. g.DrawImage(sourceImage, (targetWidth - width) / 2, (targetHeight - height) / 2, width, height);
  1240. sourceImage.Dispose();
  1241. return targetPicture;
  1242. }
  1243. catch// (Exception ex)
  1244. {
  1245. }
  1246. return null;
  1247. }
  1248. }
  1249. public class QRCodeWriterMy : Writer
  1250. {
  1251. private const int QUIET_ZONE_SIZE = 4;
  1252. /// <summary>
  1253. /// Encode a barcode using the default settings.
  1254. /// </summary>
  1255. /// <param name="contents">The contents to encode in the barcode</param>
  1256. /// <param name="format">The barcode format to generate</param>
  1257. /// <param name="width">The preferred width in pixels</param>
  1258. /// <param name="height">The preferred height in pixels</param>
  1259. /// <returns>
  1260. /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
  1261. /// </returns>
  1262. public BitMatrix encode(string contents, BarcodeFormat format, int width, int height)
  1263. {
  1264. return encode(contents, format, width, height, null);
  1265. }
  1266. /// <summary>
  1267. /// </summary>
  1268. /// <param name="contents">The contents to encode in the barcode</param>
  1269. /// <param name="format">The barcode format to generate</param>
  1270. /// <param name="width">The preferred width in pixels</param>
  1271. /// <param name="height">The preferred height in pixels</param>
  1272. /// <param name="hints">Additional parameters to supply to the encoder</param>
  1273. /// <returns>
  1274. /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
  1275. /// </returns>
  1276. public BitMatrix encode(string contents,
  1277. BarcodeFormat format,
  1278. int width,
  1279. int height,
  1280. IDictionary<EncodeHintType, object> hints)
  1281. {
  1282. if (String.IsNullOrEmpty(contents))
  1283. {
  1284. throw new ArgumentException("Found empty contents");
  1285. }
  1286. if (format != BarcodeFormat.QR_CODE)
  1287. {
  1288. throw new ArgumentException("Can only encode QR_CODE, but got " + format);
  1289. }
  1290. if (width < 0 || height < 0)
  1291. {
  1292. throw new ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
  1293. }
  1294. var errorCorrectionLevel = ErrorCorrectionLevel.L;
  1295. int quietZone = QUIET_ZONE_SIZE;
  1296. if (hints != null)
  1297. {
  1298. if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
  1299. {
  1300. var requestedECLevel = hints[EncodeHintType.ERROR_CORRECTION];
  1301. if (requestedECLevel != null)
  1302. {
  1303. errorCorrectionLevel = requestedECLevel as ErrorCorrectionLevel;
  1304. if (errorCorrectionLevel == null)
  1305. {
  1306. switch (requestedECLevel.ToString().ToUpper())
  1307. {
  1308. case "L":
  1309. errorCorrectionLevel = ErrorCorrectionLevel.L;
  1310. break;
  1311. case "M":
  1312. errorCorrectionLevel = ErrorCorrectionLevel.M;
  1313. break;
  1314. case "Q":
  1315. errorCorrectionLevel = ErrorCorrectionLevel.Q;
  1316. break;
  1317. case "H":
  1318. errorCorrectionLevel = ErrorCorrectionLevel.H;
  1319. break;
  1320. default:
  1321. errorCorrectionLevel = ErrorCorrectionLevel.L;
  1322. break;
  1323. }
  1324. }
  1325. }
  1326. }
  1327. if (hints.ContainsKey(EncodeHintType.MARGIN))
  1328. {
  1329. var quietZoneInt = hints[EncodeHintType.MARGIN];
  1330. if (quietZoneInt != null)
  1331. {
  1332. quietZone = Convert.ToInt32(quietZoneInt.ToString());
  1333. }
  1334. }
  1335. }
  1336. var code = Encoder.encode(contents, errorCorrectionLevel, hints);
  1337. BitMatrix bm = renderResult(code, width, height, quietZone);
  1338. hints[EncodeHintType.WIDTH] = bm.Width;
  1339. hints[EncodeHintType.HEIGHT] = bm.Height;
  1340. return bm;
  1341. }
  1342. // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
  1343. // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  1344. private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone)
  1345. {
  1346. var input = code.Matrix;
  1347. if (input == null)
  1348. {
  1349. throw new InvalidOperationException();
  1350. }
  1351. int inputWidth = input.Width;
  1352. int inputHeight = input.Height;
  1353. int qrWidth = inputWidth + (quietZone << 1);
  1354. int qrHeight = inputHeight + (quietZone << 1);
  1355. int outputWidth = Math.Max(width, qrWidth);
  1356. int outputHeight = Math.Max(height, qrHeight);
  1357. int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
  1358. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  1359. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  1360. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  1361. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  1362. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  1363. int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
  1364. //int leftPadding = 0;//(outputWidth - (inputWidth * multiple)) / 2;
  1365. //int topPadding = 0;//(outputHeight - (inputHeight * multiple)) / 2;
  1366. //outputWidth = (inputWidth * multiple);
  1367. //outputHeight = (inputHeight * multiple);
  1368. var output = new BitMatrix(outputWidth, outputHeight);
  1369. for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple)
  1370. {
  1371. // Write the contents of this row of the barcode
  1372. for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple)
  1373. {
  1374. if (input[inputX, inputY] == 1)
  1375. {
  1376. output.setRegion(outputX, outputY, multiple, multiple);
  1377. //int mX = outputWidth - outputX;
  1378. //int mY = outputHeight - outputY;
  1379. //mX = Math.Min(mX, multiple);
  1380. //mY = Math.Min(mY, multiple);
  1381. //output.setRegion(outputX, outputY, mX, mY);
  1382. }
  1383. }
  1384. }
  1385. return output;
  1386. }
  1387. }
  1388. }