LayoutItem.cs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. 
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using Microsoft.VisualBasic.PowerPacks;
  6. namespace Dongke.WinForm.Controls.InvoiceLayout
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. [Serializable]
  12. public abstract class LayoutItem : ICloneable, IDisposable
  13. {
  14. #region 成员变量
  15. private int _id; // ID
  16. private object _tag; // 扩展
  17. private readonly ItemType _type; // Item类别
  18. // 位置
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. protected float _top; // 上位置X (mm単位)
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. protected float _left; // 左位置Y (mm単位)
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. protected float _width; // 宽 (mm単位)
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. protected float _height; // 高 (mm単位)
  35. /// <summary>
  36. ///
  37. /// </summary>
  38. protected float _right; // 右位置 (mm単位)
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. protected float _bottom; // 下位置 (mm単位)
  43. /// <summary>
  44. ///
  45. /// </summary>
  46. protected bool _sideRatioFixed = false; // 横纵比例是否固定
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. private ItemLock _lock = ItemLock.None; // Item锁定
  51. /// <summary>
  52. ///
  53. /// </summary>
  54. [NonSerialized]
  55. protected Shape _shape; // 图形对象
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. [NonSerialized]
  60. protected SimpleShape _simpleShape;
  61. /// <summary>
  62. ///
  63. /// </summary>
  64. [NonSerialized]
  65. protected LineShape _lineShape;
  66. /// <summary>
  67. ///
  68. /// </summary>
  69. [NonSerialized]
  70. protected LayoutBox _owner; // LayoutBox
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. [NonSerialized]
  75. private ItemFrame _frame; // Item选择框
  76. /// <summary>
  77. ///
  78. /// </summary>
  79. [NonSerialized]
  80. private bool _isSelected; // Item是否选择
  81. /// <summary>
  82. ///
  83. /// </summary>
  84. [NonSerialized]
  85. private bool _isNew; // 新追加的Item
  86. /// <summary>
  87. ///
  88. /// </summary>
  89. [NonSerialized]
  90. private RectangleF _rectangleFDraw; // Item的范围 - 绘制用 (mm単位)
  91. #endregion 成员变量
  92. #region 属性
  93. /// <summary>
  94. /// LayoutBox内使用的Item标识。
  95. /// </summary>
  96. /// <remarks>
  97. /// </remarks>
  98. public int ID
  99. {
  100. get
  101. {
  102. return _id;
  103. }
  104. }
  105. /// <summary>
  106. /// object扩展
  107. /// </summary>
  108. /// <remarks>
  109. /// 等同于 System.Windows.Forms.Control.Tag ,可以参考 MSDN的Control.Tag
  110. /// </remarks>
  111. public object Tag
  112. {
  113. get
  114. {
  115. return _tag;
  116. }
  117. set
  118. {
  119. _tag = value;
  120. if (_owner != null)
  121. {
  122. _owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// 获取或设置Item类别。
  128. /// </summary>
  129. public ItemType ItemType
  130. {
  131. get
  132. {
  133. return _type;
  134. }
  135. }
  136. /// <summary>
  137. /// 获取或设置Item在LayoutBox内的X坐标 mm单位。
  138. /// </summary>
  139. public float Top
  140. {
  141. get
  142. {
  143. return _top;
  144. }
  145. set
  146. {
  147. if (value != _top)
  148. {
  149. if (SetTop(value) && _owner != null)
  150. {
  151. _owner.SetItemChangedArgs(this, ItemChangeType.Moved);
  152. }
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// 获取或设置Item在LayoutBox内的Y坐标 mm单位。
  158. /// </summary>
  159. public float Left
  160. {
  161. get
  162. {
  163. return _left;
  164. }
  165. set
  166. {
  167. if (value != _left)
  168. {
  169. if (SetLeft(value) && _owner != null)
  170. {
  171. _owner.SetItemChangedArgs(this, ItemChangeType.Moved);
  172. }
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// 获取或设置Item在LayoutBox内的宽 mm单位。
  178. /// </summary>
  179. public float Width
  180. {
  181. get
  182. {
  183. return _width;
  184. }
  185. set
  186. {
  187. if (value != _width)
  188. {
  189. if (SetWidth(value) && _owner != null)
  190. {
  191. _owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  192. }
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// 获取或设置Item在LayoutBox内的高 mm单位。
  198. /// </summary>
  199. public float Height
  200. {
  201. get
  202. {
  203. return _height;
  204. }
  205. set
  206. {
  207. if (value != _height)
  208. {
  209. if (SetHeight(value) && _owner != null)
  210. {
  211. _owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  212. }
  213. }
  214. }
  215. }
  216. /// <summary>
  217. /// 获取Item在LayoutBox内的右位置 mm单位。
  218. /// </summary>
  219. public float Right
  220. {
  221. get
  222. {
  223. return _left + _width;
  224. //return _right;
  225. }
  226. //set
  227. //{
  228. // if (value != _right)
  229. // {
  230. // if (SetRight(value) && _owner != null)
  231. // {
  232. // _owner.SetItemChangedArgs(this, ItemChangeType.Change);
  233. // }
  234. // }
  235. //}
  236. }
  237. /// <summary>
  238. /// 获取Item在LayoutBox内的下位置 mm单位。
  239. /// </summary>
  240. public float Bottom
  241. {
  242. get
  243. {
  244. return _top + _height;
  245. //return _bottom;
  246. }
  247. //set
  248. //{
  249. // if (value != _bottom)
  250. // {
  251. // if (SetBottom(value) && _owner != null)
  252. // {
  253. // _owner.SetItemChangedArgs(this, ItemChangeType.Change);
  254. // }
  255. // }
  256. //}
  257. }
  258. /// <summary>
  259. /// 获取或设置图片横纵比例是否固定
  260. /// </summary>
  261. public bool SideRatioFixed
  262. {
  263. get
  264. {
  265. return _sideRatioFixed;
  266. }
  267. set
  268. {
  269. if (value != _sideRatioFixed)
  270. {
  271. _sideRatioFixed = value;
  272. if (Owner != null)
  273. {
  274. Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  275. }
  276. }
  277. }
  278. }
  279. /// <summary>
  280. /// 获取或设置Item锁定
  281. /// </summary>
  282. internal ItemLock ItemLockType
  283. {
  284. get
  285. {
  286. return _lock;
  287. }
  288. set
  289. {
  290. if (value != _lock)
  291. {
  292. _lock = value;
  293. ResetItemFrame();
  294. }
  295. }
  296. }
  297. /// <summary>
  298. /// 获取Item是否是选择状态。
  299. /// </summary>
  300. public bool Selected
  301. {
  302. get
  303. {
  304. return _isSelected;
  305. }
  306. internal set
  307. {
  308. if (value != _isSelected)
  309. {
  310. _isSelected = value;
  311. }
  312. }
  313. }
  314. /// <summary>
  315. /// 获取或设置是否是新追加的Item。
  316. /// </summary>
  317. internal bool IsNew
  318. {
  319. get
  320. {
  321. return _isNew;
  322. }
  323. set
  324. {
  325. _isNew = value;
  326. }
  327. }
  328. /// <summary>
  329. /// Item所属的LayoutBox
  330. /// </summary>
  331. internal LayoutBox Owner
  332. {
  333. get
  334. {
  335. return _owner;
  336. }
  337. set
  338. {
  339. if (_owner != value)
  340. {
  341. if (_owner != null)
  342. {
  343. _owner.Items.Remove(this);
  344. //if (_control != null &&
  345. // _owner.PaperArea.Controls.Contains(_control))
  346. //{
  347. // _owner.PaperArea.Controls.Remove(_control);
  348. //}
  349. if (_shape != null && _owner.ItemShapes.Contains(_shape))
  350. {
  351. _owner.ItemShapes.Remove(_shape);
  352. }
  353. }
  354. _owner = value;
  355. if (_owner != null)
  356. {
  357. _owner.Items.Add(this);
  358. //if (_control != null &&
  359. // !_owner.PaperArea.Controls.Contains(_control))
  360. //{
  361. // _owner.PaperArea.Controls.Add(_control);
  362. // int count = value.SelectedItems.Count;
  363. // _owner.PaperArea.Controls.SetChildIndex(_control, count);
  364. //}
  365. if (_shape != null && !_owner.ItemShapes.Contains(_shape))
  366. {
  367. _owner.ItemShapes.Add(_shape);
  368. _shape.BringToFront();
  369. }
  370. }
  371. }
  372. }
  373. }
  374. /// <summary>
  375. /// Item对应的图形
  376. /// </summary>
  377. internal Shape Shape
  378. {
  379. get
  380. {
  381. return _shape;
  382. }
  383. private set
  384. {
  385. if (_shape != value)
  386. {
  387. if (_shape != null)
  388. {
  389. _shape.Dispose();
  390. }
  391. _shape = value;
  392. if (_shape != null)
  393. {
  394. if (_shape is SimpleShape)
  395. {
  396. _simpleShape = _shape as SimpleShape;
  397. }
  398. else if (_shape is LineShape)
  399. {
  400. _lineShape = _shape as LineShape;
  401. }
  402. SetShapeProperty(GetZoom());
  403. if (_owner != null && !_owner.ItemShapes.Contains(_shape))
  404. {
  405. _owner.ItemShapes.Add(_shape);
  406. _shape.BringToFront();
  407. }
  408. _shape.DoubleClick += new EventHandler(OnDoubleClickControl);
  409. _shape.MouseDown += new MouseEventHandler(OnMouseDownControl);
  410. _shape.PreviewKeyDown += new PreviewKeyDownEventHandler(OnPreviewKeyDown);
  411. _shape.MouseMove += new MouseEventHandler(OnMouseMove);
  412. _shape.Paint += new PaintEventHandler(OnPaint);
  413. ResetShapeLocationAndSize(false);
  414. }
  415. }
  416. }
  417. }
  418. /// <summary>
  419. ///
  420. /// </summary>
  421. protected SimpleShape SimpleShape
  422. {
  423. get
  424. {
  425. return _simpleShape;
  426. }
  427. }
  428. /// <summary>
  429. ///
  430. /// </summary>
  431. protected LineShape LineShape
  432. {
  433. get
  434. {
  435. return _lineShape;
  436. }
  437. }
  438. /// <summary>
  439. /// 图形范围
  440. /// </summary>
  441. internal protected virtual Rectangle ShapeBounds
  442. {
  443. get
  444. {
  445. return _simpleShape != null ? _simpleShape.Bounds : Rectangle.Empty;
  446. }
  447. }
  448. /// <summary>
  449. ///
  450. /// </summary>
  451. internal protected virtual Rectangle ShapeVirtualBounds
  452. {
  453. get
  454. {
  455. return ShapeBounds;
  456. }
  457. }
  458. /// <summary>
  459. /// 获取或设置Item的选中边框
  460. /// </summary>
  461. internal ItemFrame Frame
  462. {
  463. get
  464. {
  465. return _frame;
  466. }
  467. set
  468. {
  469. if (_frame != value)
  470. {
  471. _frame = value;
  472. }
  473. }
  474. }
  475. /// <summary>
  476. /// 获取或设置Item的范围 - 绘制用 (mm単位)
  477. /// </summary>
  478. protected virtual RectangleF RectangleFDraw
  479. {
  480. get
  481. {
  482. return _rectangleFDraw;
  483. }
  484. set
  485. {
  486. _rectangleFDraw = value;
  487. }
  488. }
  489. #endregion 属性
  490. #region 构造函数
  491. /// <summary>
  492. /// 构造函数
  493. /// </summary>
  494. /// <param name="box">Layoutbox</param>
  495. /// <param name="itemType">item类别</param>
  496. internal LayoutItem(LayoutBox box, ItemType itemType)
  497. {
  498. _rectangleFDraw = RectangleF.Empty;
  499. _top = LayoutConsts.ITEM_LOCATION_DEF;
  500. _left = LayoutConsts.ITEM_LOCATION_DEF;
  501. _width = 0f;
  502. _height = 0f;
  503. _right = _left + _width;
  504. _bottom = _top + _height;
  505. _id = 0;
  506. _type = itemType;
  507. Owner = box;
  508. if (_owner != null)
  509. {
  510. _id = _owner.NewItemID;
  511. Shape = LayoutUtility.GetNewShape(this);
  512. //SetShapeProperty();
  513. SetDrawProperty();
  514. }
  515. }
  516. #endregion 构造函数
  517. #region 事件处理
  518. /// <summary>
  519. /// Item图形上的OnMouseMove事件
  520. /// </summary>
  521. /// <param name="sender">指定的对象</param>
  522. /// <param name="e">提供的事件数据</param>
  523. private void OnMouseMove(object sender, MouseEventArgs e)
  524. {
  525. // TODO Item范围内移动
  526. if (_owner == null || _owner.LayoutMode != LayoutMode.Edit)
  527. {
  528. Cursor.Current = Cursors.Default;
  529. }
  530. else
  531. {
  532. if (_lock == ItemLock.None)
  533. {
  534. Cursor.Current = Cursors.SizeAll;
  535. }
  536. else
  537. {
  538. Cursor.Current = Cursors.Default;
  539. }
  540. }
  541. }
  542. /// <summary>
  543. /// Item图形上的OnPreviewKeyDown事件
  544. /// </summary>
  545. /// <param name="sender">指定的对象</param>
  546. /// <param name="e">提供的事件数据</param>
  547. private void OnPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  548. {
  549. if (_owner == null || _owner.LayoutMode != LayoutMode.Edit
  550. || _owner.SelectedItems == null || _owner.SelectedItems.Count == 0)
  551. {
  552. return;
  553. }
  554. bool isInputKey = false;
  555. bool isDirty = false;
  556. if (e.KeyCode == Keys.Left)
  557. {
  558. isInputKey = true;
  559. // 选择的Item向左移动1像素
  560. foreach (LayoutItem item in _owner.SelectedItems)
  561. {
  562. isDirty = item.LeftItem(1) ? true : isDirty;
  563. }
  564. }
  565. else if (e.KeyCode == Keys.Up)
  566. {
  567. isInputKey = true;
  568. // 选择的Item向上移动1像素
  569. foreach (LayoutItem item in _owner.SelectedItems)
  570. {
  571. isDirty = item.UpItem(1) ? true : isDirty;
  572. }
  573. }
  574. else if (e.KeyCode == Keys.Right)
  575. {
  576. isInputKey = true;
  577. // 选择的Item向右移动1像素
  578. foreach (LayoutItem item in _owner.SelectedItems)
  579. {
  580. isDirty = item.RightItem(1) ? true : isDirty;
  581. }
  582. }
  583. else if (e.KeyCode == Keys.Down)
  584. {
  585. isInputKey = true;
  586. // 选择的Item向下移动1像素
  587. foreach (LayoutItem item in _owner.SelectedItems)
  588. {
  589. isDirty = item.DownItem(1) ? true : isDirty;
  590. }
  591. }
  592. if (isInputKey)
  593. {
  594. e.IsInputKey = isInputKey;
  595. if (isDirty && _owner != null)
  596. {
  597. _owner.Dirty = true;
  598. }
  599. }
  600. }
  601. /// <summary>
  602. /// Item图形上的OnDoubleClickControl事件
  603. /// </summary>
  604. /// <param name="sender">指定的对象</param>
  605. /// <param name="e">提供的事件数据</param>
  606. private void OnDoubleClickControl(object sender, EventArgs e)
  607. {
  608. if (_isSelected
  609. && _owner != null
  610. && _owner.LayoutMode == LayoutMode.Edit
  611. && _owner.SelectedItems != null
  612. && _owner.SelectedItems.Count == 1)
  613. {
  614. // 显示Item的属性设置画面
  615. ShowItemPropertyDialog();
  616. }
  617. }
  618. /// <summary>
  619. /// Item图形上的OnMouseDownControl事件
  620. /// </summary>
  621. /// <param name="sender">指定的对象</param>
  622. /// <param name="e">提供的事件数据</param>
  623. private void OnMouseDownControl(object sender, MouseEventArgs e)
  624. {
  625. // 选中Item
  626. if (_owner == null || _owner.LayoutMode != LayoutMode.Edit)
  627. {
  628. return;
  629. }
  630. if (_owner != null)
  631. {
  632. // Item的选择方式
  633. if (Control.ModifierKeys == Keys.Control
  634. || Control.ModifierKeys == Keys.Shift)
  635. {
  636. _owner.SelectItemMultiple(this, !_isSelected);
  637. }
  638. else
  639. {
  640. if (!_isSelected)
  641. {
  642. _owner.SelectItemSingle(this, true);
  643. }
  644. }
  645. }
  646. }
  647. /// <summary>
  648. /// Item图形上的OnPaint事件
  649. /// </summary>
  650. /// <param name="sender">指定的对象</param>
  651. /// <param name="e">提供的事件数据</param>
  652. protected virtual void OnPaint(object sender, PaintEventArgs e)
  653. {
  654. }
  655. #endregion 事件处理
  656. #region 函数
  657. /// <summary>
  658. /// Item初始化
  659. /// </summary>
  660. /// <param name="box">LayoutBox</param>
  661. /// <param name="isNewID">是否生成新ID</param>
  662. /// <returns>是否成功</returns>
  663. internal virtual bool Init(LayoutBox box, bool isNewID)
  664. {
  665. Owner = box;
  666. if (_owner == null)
  667. {
  668. return false;
  669. }
  670. if (isNewID)
  671. {
  672. _id = _owner.NewItemID;
  673. }
  674. Shape = LayoutUtility.GetNewShape(this);
  675. //SetShapeProperty();
  676. SetDrawProperty();
  677. return true;
  678. }
  679. /// <summary>
  680. /// 刷新显示
  681. /// </summary>
  682. public virtual void Refresh()
  683. {
  684. if (_shape != null)
  685. {
  686. _shape.Refresh();
  687. }
  688. }
  689. /// <summary>
  690. /// 显示Item属性设置画面
  691. /// </summary>
  692. /// <returns>
  693. /// DialogResult.OK:选中的Item有效,设置成功<br/>
  694. /// DialogResult.Cancel:选中的Item有效,取消设置<br/>
  695. /// DialogResult.None:LayoutBox不是编辑模式,或选中的Item不是一个
  696. /// </returns>
  697. public DialogResult ShowItemPropertyDialog()
  698. {
  699. if (_owner != null && _owner.LayoutMode == LayoutMode.Edit)
  700. {
  701. // 显示Item属性设置画面
  702. DialogResult dialogResult = ShowItemPropertyDialogInner();
  703. if (dialogResult == DialogResult.OK)
  704. {
  705. //_owner.PaperArea.Refresh();
  706. }
  707. return dialogResult;
  708. }
  709. return DialogResult.None;
  710. }
  711. /// <summary>
  712. /// 显示Item属性设置画面 - 子类实例化用
  713. /// </summary>
  714. /// <returns>
  715. /// DialogResult.OK:选中的Item有效,设置成功<br/>
  716. /// DialogResult.Cancel:选中的Item有效,取消设置<br/>
  717. /// DialogResult.None:LayoutBox不是编辑模式,或选中的Item不是一个
  718. /// </returns>
  719. protected virtual DialogResult ShowItemPropertyDialogInner()
  720. {
  721. throw new NotImplementedException();
  722. }
  723. /// <summary>
  724. /// 改变Item的选择状态
  725. /// </summary>
  726. /// <param name="value">选中:true,取消选择:false</param>
  727. public void Select(bool value)
  728. {
  729. if (_owner.LayoutMode != LayoutMode.Edit)
  730. {
  731. return;
  732. }
  733. if (_owner != null)
  734. {
  735. _owner.SelectItemMultiple(this, value);
  736. }
  737. }
  738. /// <summary>
  739. /// 刷新显示Item选择边框
  740. /// </summary>
  741. internal void ResetItemFrame()
  742. {
  743. if (_isSelected && _frame != null)
  744. {
  745. _frame.CreateFrame();
  746. }
  747. }
  748. /// <summary>
  749. /// 改变Item图形的位置和大小
  750. /// </summary>
  751. protected virtual bool ResetShapeLocationAndSize(int left, int top, int width, int height)
  752. {
  753. if (_simpleShape != null)
  754. {
  755. _simpleShape.Location = new Point(left, top);
  756. _simpleShape.Size = new Size(width, height);
  757. return true;
  758. }
  759. return false;
  760. }
  761. /// <summary>
  762. ///
  763. /// </summary>
  764. protected virtual bool ResetShapeLocation(int left, int top)
  765. {
  766. if (_simpleShape != null)
  767. {
  768. _simpleShape.Location = new Point(left, top);
  769. return true;
  770. }
  771. return false;
  772. }
  773. /// <summary>
  774. ///
  775. /// </summary>
  776. protected virtual bool ResetShapeSize(int width, int height)
  777. {
  778. if (_simpleShape != null)
  779. {
  780. _simpleShape.Size = new Size(width, height);
  781. return true;
  782. }
  783. return false;
  784. }
  785. /// <summary>
  786. /// 根据zoom改变Item与Item图形的位置和大小
  787. /// </summary>
  788. protected virtual bool ResetItemLocationAndSize(int left, int top, int width, int height)
  789. {
  790. bool l = ResetItemLocation(left, top);
  791. bool s = ResetItemSize(width, height);
  792. return l && s;
  793. }
  794. /// <summary>
  795. ///
  796. /// </summary>
  797. protected virtual bool ResetItemLocation(int left, int top)
  798. {
  799. if (_lock == ItemLock.LockLocationAndSize)
  800. {
  801. return false;
  802. }
  803. int zoom = 100;
  804. if (_owner != null)
  805. {
  806. zoom = _owner.Zoom;
  807. }
  808. if (_simpleShape != null && 0 < zoom)
  809. {
  810. if (_simpleShape.Top != top || _simpleShape.Left != left)
  811. {
  812. if (_simpleShape.Top != top)
  813. {
  814. _top = LayoutCommon.PixelToMillimeter(top, zoom);
  815. _bottom = _top + _height;
  816. }
  817. if (_simpleShape.Left != left)
  818. {
  819. _left = LayoutCommon.PixelToMillimeter(left, zoom);
  820. _right = _left + _width;
  821. }
  822. _simpleShape.Location = new Point(left, top);
  823. SetRectangleF();
  824. if (_owner != null)
  825. {
  826. _owner.SetItemChangedArgs(this, ItemChangeType.Moved);
  827. }
  828. }
  829. }
  830. return true;
  831. }
  832. /// <summary>
  833. ///
  834. /// </summary>
  835. protected virtual bool ResetItemSize(int width, int height)
  836. {
  837. if (_lock != ItemLock.None)
  838. {
  839. return false;
  840. }
  841. int zoom = 100;
  842. if (_owner != null)
  843. {
  844. zoom = _owner.Zoom;
  845. }
  846. if (_simpleShape != null && 0 < zoom)
  847. {
  848. if (_simpleShape.Width != width || _simpleShape.Height != height)
  849. {
  850. if (_simpleShape.Width != width)
  851. {
  852. _width = LayoutCommon.PixelToMillimeter(width, zoom);
  853. _right = _left + _width;
  854. }
  855. if (_simpleShape.Height != height)
  856. {
  857. _height = LayoutCommon.PixelToMillimeter(height, zoom);
  858. _bottom = _top + _height;
  859. }
  860. _simpleShape.Size = new Size(width, height);
  861. SetRectangleF();
  862. if (_owner != null)
  863. {
  864. _owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  865. }
  866. }
  867. }
  868. return true;
  869. }
  870. /// <summary>
  871. /// 根据zoom与Item图形的位置和大小,改变Item的位置和大小
  872. /// </summary>
  873. protected virtual bool ResetItemLocationAndSize()
  874. {
  875. bool l = ResetItemLocation();
  876. bool s = ResetItemSize();
  877. return l && s;
  878. }
  879. /// <summary>
  880. ///
  881. /// </summary>
  882. protected virtual bool ResetItemLocation()
  883. {
  884. if (_lock == ItemLock.LockLocationAndSize)
  885. {
  886. return false;
  887. }
  888. int zoom = 100;
  889. if (_owner != null)
  890. {
  891. zoom = _owner.Zoom;
  892. }
  893. if (_simpleShape != null && 0 < zoom)
  894. {
  895. float top = LayoutCommon.PixelToMillimeter(_simpleShape.Top, zoom);
  896. float left = LayoutCommon.PixelToMillimeter(_simpleShape.Left, zoom);
  897. if (_top != top || _left != left)
  898. {
  899. if (_top != top)
  900. {
  901. _top = top;
  902. _bottom = _top + _height;
  903. }
  904. if (_left != left)
  905. {
  906. _left = left;
  907. _right = _left + _width;
  908. }
  909. SetRectangleF();
  910. if (_owner != null)
  911. {
  912. _owner.SetItemChangedArgs(this, ItemChangeType.Moved);
  913. }
  914. }
  915. }
  916. return true;
  917. }
  918. /// <summary>
  919. ///
  920. /// </summary>
  921. protected virtual bool ResetItemSize()
  922. {
  923. if (_lock != ItemLock.None)
  924. {
  925. return false;
  926. }
  927. int zoom = 100;
  928. if (_owner != null)
  929. {
  930. zoom = _owner.Zoom;
  931. }
  932. if (_simpleShape != null && 0 < zoom)
  933. {
  934. float width = LayoutCommon.PixelToMillimeter(_simpleShape.Width, zoom);
  935. float height = LayoutCommon.PixelToMillimeter(_simpleShape.Height, zoom);
  936. if (_width != width || _height != height)
  937. {
  938. if (_width != width)
  939. {
  940. _width = width;
  941. _right = _left + _width;
  942. }
  943. if (_height != height)
  944. {
  945. _height = height;
  946. _bottom = _top + _height;
  947. }
  948. SetRectangleF();
  949. if (_owner != null)
  950. {
  951. _owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  952. }
  953. }
  954. }
  955. return true;
  956. }
  957. /// <summary>
  958. /// 设置Item的X坐标
  959. /// </summary>
  960. /// <param name="value">X坐标</param>
  961. protected virtual bool SetTop(float value)
  962. {
  963. if (_lock == ItemLock.LockLocationAndSize)
  964. {
  965. return false;
  966. }
  967. if (value < LayoutConsts.ITEM_LOCATION_MIN
  968. || LayoutConsts.ITEM_LOCATION_MAX < value
  969. || _owner.PaperHeight <= value)
  970. {
  971. return false;
  972. }
  973. _top = value;
  974. _bottom = _top + _height;
  975. SetRectangleF();
  976. if (_simpleShape != null && _owner != null)
  977. {
  978. _simpleShape.Top = LayoutCommon.MillimeterToPixel(_top, _owner.Zoom);
  979. ResetItemFrame();
  980. }
  981. return true;
  982. }
  983. /// <summary>
  984. /// 设置Item的Y坐标
  985. /// </summary>
  986. /// <param name="value">Y坐标</param>
  987. protected virtual bool SetLeft(float value)
  988. {
  989. if (_lock == ItemLock.LockLocationAndSize)
  990. {
  991. return false;
  992. }
  993. if (value < LayoutConsts.ITEM_LOCATION_MIN
  994. || LayoutConsts.ITEM_LOCATION_MAX < value
  995. || _owner.PaperWidth <= value)
  996. {
  997. return false;
  998. }
  999. _left = value;
  1000. _right = _left + _width;
  1001. SetRectangleF();
  1002. if (_simpleShape != null && _owner != null)
  1003. {
  1004. _simpleShape.Left = LayoutCommon.MillimeterToPixel(_left, _owner.Zoom);
  1005. ResetItemFrame();
  1006. }
  1007. return true;
  1008. }
  1009. /// <summary>
  1010. /// 设置Item的宽度
  1011. /// </summary>
  1012. /// <param name="value">宽</param>
  1013. protected virtual bool SetWidth(float value)
  1014. {
  1015. if (_lock != ItemLock.None)
  1016. {
  1017. return false;
  1018. }
  1019. if (value < LayoutConsts.ITEM_SIZE_MIN
  1020. || LayoutConsts.ITEM_SIZE_WIDTH_MAX < value)
  1021. {
  1022. return false;
  1023. }
  1024. float hValue = _height;
  1025. if (_sideRatioFixed)
  1026. {
  1027. hValue = _height / _width * value;
  1028. if (hValue < LayoutConsts.ITEM_SIZE_MIN
  1029. || LayoutConsts.ITEM_SIZE_WIDTH_MAX < hValue)
  1030. {
  1031. return false;
  1032. }
  1033. _height = hValue;
  1034. _bottom = _top + _height;
  1035. }
  1036. _width = value;
  1037. _right = _left + _width;
  1038. SetRectangleF();
  1039. if (_simpleShape != null && _owner != null)
  1040. {
  1041. if (_sideRatioFixed)
  1042. {
  1043. _simpleShape.Size = new Size(
  1044. LayoutCommon.MillimeterToPixel(_width, _owner.Zoom),
  1045. LayoutCommon.MillimeterToPixel(_height, _owner.Zoom));
  1046. }
  1047. else
  1048. {
  1049. _simpleShape.Width = LayoutCommon.MillimeterToPixel(_width, _owner.Zoom);
  1050. }
  1051. ResetItemFrame();
  1052. }
  1053. return true;
  1054. }
  1055. /// <summary>
  1056. /// 设置Item的高度
  1057. /// </summary>
  1058. /// <param name="value">高</param>
  1059. protected virtual bool SetHeight(float value)
  1060. {
  1061. if (_lock != ItemLock.None)
  1062. {
  1063. return false;
  1064. }
  1065. if (value < LayoutConsts.ITEM_SIZE_MIN
  1066. || LayoutConsts.ITEM_SIZE_HEIGHT_MAX < value)
  1067. {
  1068. return false;
  1069. }
  1070. float wValue = _height;
  1071. if (_sideRatioFixed)
  1072. {
  1073. wValue = _width / _height * value;
  1074. if (wValue < LayoutConsts.ITEM_SIZE_MIN
  1075. || LayoutConsts.ITEM_SIZE_WIDTH_MAX < wValue)
  1076. {
  1077. return false;
  1078. }
  1079. _width = wValue;
  1080. _right = _left + _width;
  1081. }
  1082. _height = value;
  1083. _bottom = _top + _height;
  1084. SetRectangleF();
  1085. if (_simpleShape != null && _owner != null)
  1086. {
  1087. if (_sideRatioFixed)
  1088. {
  1089. _simpleShape.Size = new Size(
  1090. LayoutCommon.MillimeterToPixel(_width, _owner.Zoom),
  1091. LayoutCommon.MillimeterToPixel(_height, _owner.Zoom));
  1092. }
  1093. else
  1094. {
  1095. _simpleShape.Height = LayoutCommon.MillimeterToPixel(_height, _owner.Zoom);
  1096. }
  1097. ResetItemFrame();
  1098. }
  1099. return true;
  1100. }
  1101. /// <summary>
  1102. /// 移动Item
  1103. /// </summary>
  1104. public virtual bool LeftItem(int value)
  1105. {
  1106. if (_lock == ItemLock.LockLocationAndSize)
  1107. {
  1108. return false;
  1109. }
  1110. if (_simpleShape != null && value <= _simpleShape.Left)
  1111. {
  1112. int left = _simpleShape.Left - value;
  1113. if (_frame != null)
  1114. {
  1115. _frame.Left -= value;
  1116. }
  1117. ResetItemLocation(left, _simpleShape.Top);
  1118. return true;
  1119. }
  1120. return false;
  1121. }
  1122. /// <summary>
  1123. ///
  1124. /// </summary>
  1125. public virtual bool UpItem(int value)
  1126. {
  1127. if (_lock == ItemLock.LockLocationAndSize)
  1128. {
  1129. return false;
  1130. }
  1131. if (_simpleShape != null && value <= _simpleShape.Top)
  1132. {
  1133. int top = _simpleShape.Top - value;
  1134. if (_frame != null)
  1135. {
  1136. _frame.Top -= value;
  1137. }
  1138. ResetItemLocation(_simpleShape.Left, top);
  1139. return true;
  1140. }
  1141. return false;
  1142. }
  1143. /// <summary>
  1144. ///
  1145. /// </summary>
  1146. public virtual bool RightItem(int value)
  1147. {
  1148. if (_lock == ItemLock.LockLocationAndSize)
  1149. {
  1150. return false;
  1151. }
  1152. if (_simpleShape != null)
  1153. {
  1154. int left = _simpleShape.Left + value;
  1155. if (_owner != null
  1156. && left < _owner.PaperArea.Width
  1157. && left <= _owner.MaxItemLeft)
  1158. {
  1159. if (_frame != null)
  1160. {
  1161. _frame.Left += value;
  1162. }
  1163. ResetItemLocation(left, _simpleShape.Top);
  1164. return true;
  1165. }
  1166. }
  1167. return false;
  1168. }
  1169. /// <summary>
  1170. ///
  1171. /// </summary>
  1172. public virtual bool DownItem(int value)
  1173. {
  1174. if (_lock == ItemLock.LockLocationAndSize)
  1175. {
  1176. return false;
  1177. }
  1178. if (_simpleShape != null)
  1179. {
  1180. int top = _simpleShape.Top + value;
  1181. if (_owner != null
  1182. && top < _owner.PaperArea.Height
  1183. && top <= _owner.MaxItemTop)
  1184. {
  1185. if (_frame != null)
  1186. {
  1187. _frame.Top += value;
  1188. }
  1189. ResetItemLocation(_simpleShape.Left, top);
  1190. return true;
  1191. }
  1192. }
  1193. return false;
  1194. }
  1195. /// <summary>
  1196. ///
  1197. /// </summary>
  1198. public virtual bool MoveItem(int left, int top)
  1199. {
  1200. if (_lock == ItemLock.LockLocationAndSize)
  1201. {
  1202. return false;
  1203. }
  1204. return ResetItemLocation(left, top);
  1205. }
  1206. /// <summary>
  1207. ///
  1208. /// </summary>
  1209. public virtual bool ChangeItemSize(int width, int height)
  1210. {
  1211. if (_lock != ItemLock.None)
  1212. {
  1213. return false;
  1214. }
  1215. return ResetItemSize(width, height);
  1216. }
  1217. /// <summary>
  1218. /// 根据zom与Item,改变Item图形的位置和大小
  1219. /// </summary>
  1220. internal virtual bool ResetShapeLocationAndSize(bool isCheck)
  1221. {
  1222. int zoom = 100;
  1223. if (_owner != null)
  1224. {
  1225. zoom = _owner.Zoom;
  1226. }
  1227. if (_shape != null)
  1228. {
  1229. int top = LayoutCommon.MillimeterToPixel(_top, zoom);
  1230. int left = LayoutCommon.MillimeterToPixel(_left, zoom);
  1231. int width = LayoutCommon.MillimeterToPixel(_width, zoom);
  1232. int height = LayoutCommon.MillimeterToPixel(_height, zoom);
  1233. ResetShapeLocationAndSize(left, top, width, height);
  1234. ResetItemFrame();
  1235. return true;
  1236. }
  1237. return false;
  1238. }
  1239. /// <summary>
  1240. /// 初始化Item图形属性
  1241. /// </summary>
  1242. internal virtual void SetShapeProperty(float zoom)
  1243. {
  1244. }
  1245. internal virtual float GetZoom()
  1246. {
  1247. float zoom = 1;
  1248. if (Owner != null)
  1249. {
  1250. zoom = Owner.Zoom / 100f;
  1251. }
  1252. return (0 < zoom) ? zoom : 1;
  1253. }
  1254. /// <summary>
  1255. /// 初始化Item绘制用属性
  1256. /// </summary>
  1257. internal virtual float SetDrawProperty()
  1258. {
  1259. float zoom = GetZoom();
  1260. _rectangleFDraw.X = Left * zoom;
  1261. _rectangleFDraw.Y = Top * zoom;
  1262. _rectangleFDraw.Width = Width * zoom;
  1263. _rectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN) * zoom;
  1264. return zoom;
  1265. }
  1266. /// <summary>
  1267. /// 初始化Item绘制用范围属性
  1268. /// </summary>
  1269. internal virtual float SetRectangleF()
  1270. {
  1271. float zoom = GetZoom();
  1272. _rectangleFDraw.X = Left * zoom;
  1273. _rectangleFDraw.Y = Top * zoom;
  1274. _rectangleFDraw.Width = Width * zoom;
  1275. _rectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN) * zoom;
  1276. return zoom;
  1277. }
  1278. /// <summary>
  1279. /// Item图形是否被此范围包含
  1280. /// </summary>
  1281. /// <param name="rect"></param>
  1282. /// <returns></returns>
  1283. internal protected virtual bool BoundsContainsBy(Rectangle rect)
  1284. {
  1285. return _simpleShape != null ? rect.Contains(_simpleShape.Bounds) : false;
  1286. }
  1287. /// <summary>
  1288. ///
  1289. /// </summary>
  1290. internal protected virtual bool BoundsContains(Rectangle rect)
  1291. {
  1292. return _simpleShape != null ? _simpleShape.Bounds.Contains(rect) : false;
  1293. }
  1294. /// <summary>
  1295. ///
  1296. /// </summary>
  1297. internal protected virtual bool IntersectsWith(Rectangle rect)
  1298. {
  1299. return _simpleShape != null ? _simpleShape.Bounds.IntersectsWith(rect) : false;
  1300. }
  1301. /// <summary>
  1302. ///
  1303. /// </summary>
  1304. internal protected virtual bool BoundsContains(Point pt)
  1305. {
  1306. return _simpleShape != null ? _simpleShape.Bounds.Contains(pt) : false;
  1307. }
  1308. #region ICloneable
  1309. /// <summary>
  1310. /// Creates a new object that is a copy of the current instance.
  1311. /// </summary>
  1312. /// <returns>A new object that is a copy of this instance.</returns>
  1313. public virtual object Clone()
  1314. {
  1315. return MemberwiseClone();
  1316. }
  1317. #endregion ICloneable
  1318. #region IDisposable
  1319. /// <summary>
  1320. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  1321. /// </summary>
  1322. public void Dispose()
  1323. {
  1324. Dispose(true);
  1325. GC.SuppressFinalize(this);
  1326. }
  1327. /// <summary>
  1328. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  1329. /// </summary>
  1330. /// <param name="disposing"></param>
  1331. protected virtual void Dispose(bool disposing)
  1332. {
  1333. if (disposing)
  1334. {
  1335. lock (this)
  1336. {
  1337. if (_shape != null)
  1338. {
  1339. _shape.Dispose();
  1340. _shape = null;
  1341. }
  1342. _owner = null;
  1343. }
  1344. }
  1345. }
  1346. #endregion IDisposable
  1347. #endregion 函数
  1348. }
  1349. }