IBottomNameStrip.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. using Resources;
  2. using SmartCoalApplication.Base;
  3. using SmartCoalApplication.SystemLayer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using System.Windows.Forms.VisualStyles;
  14. namespace SmartCoalApplication.Core
  15. {
  16. public class IBottomNameStrip : Control
  17. {
  18. public enum ItemPart
  19. {
  20. None,
  21. Image,
  22. CloseButton
  23. }
  24. public sealed class Item
  25. {
  26. private Rectangle rectangle;
  27. private PushButtonState imageRenderState;
  28. /// <summary>
  29. /// 图片
  30. /// </summary>
  31. private Image image;
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. private string name;
  36. /// <summary>
  37. /// 文字颜色
  38. /// </summary>
  39. private Color nColor;
  40. private bool selected;
  41. private PushButtonState checkRenderState;
  42. private CheckState checkState;
  43. private PushButtonState closeRenderState;
  44. private bool dirty;
  45. private bool lockedDirtyValue;
  46. private int dirtyValueLockCount = 0;
  47. /// <summary>
  48. /// Tag
  49. /// </summary>
  50. private object tag;
  51. public event EventHandler Changed;
  52. private void OnChanged()
  53. {
  54. if (Changed != null)
  55. {
  56. Changed(this, EventArgs.Empty);
  57. }
  58. }
  59. /// <summary>
  60. /// checkbox的被选中/取消选中事件
  61. /// 实际上没有用checkbox,而是自己画的,自己控制的逻辑
  62. /// </summary>
  63. public event EventHandler CheckChanged;
  64. private void OnCheckChanged()
  65. {
  66. if (CheckChanged != null)
  67. {
  68. CheckChanged(this, EventArgs.Empty);
  69. }
  70. }
  71. public Rectangle Rectangle
  72. {
  73. get
  74. {
  75. return this.rectangle;
  76. }
  77. set
  78. {
  79. this.rectangle = value;
  80. }
  81. }
  82. public string Name
  83. {
  84. get
  85. {
  86. return this.name;
  87. }
  88. set
  89. {
  90. this.name = value;
  91. OnChanged();
  92. }
  93. }
  94. public Color NColor
  95. {
  96. get
  97. {
  98. return this.nColor;
  99. }
  100. set
  101. {
  102. this.nColor = value;
  103. OnChanged();
  104. }
  105. }
  106. public Image Image
  107. {
  108. get
  109. {
  110. return this.image;
  111. }
  112. set
  113. {
  114. this.image = value;
  115. OnChanged();
  116. }
  117. }
  118. public PushButtonState ImageRenderState
  119. {
  120. get
  121. {
  122. return this.imageRenderState;
  123. }
  124. set
  125. {
  126. if (this.imageRenderState != value)
  127. {
  128. this.imageRenderState = value;
  129. OnChanged();
  130. }
  131. }
  132. }
  133. public bool Selected
  134. {
  135. get
  136. {
  137. return this.selected;
  138. }
  139. set
  140. {
  141. if (this.selected != value)
  142. {
  143. this.selected = value;
  144. OnChanged();
  145. }
  146. }
  147. }
  148. public bool Dirty
  149. {
  150. get
  151. {
  152. if (this.dirtyValueLockCount > 0)
  153. {
  154. return this.lockedDirtyValue;
  155. }
  156. else
  157. {
  158. return this.dirty;
  159. }
  160. }
  161. set
  162. {
  163. if (this.dirty != value)
  164. {
  165. this.dirty = value;
  166. if (this.dirtyValueLockCount <= 0)
  167. {
  168. OnChanged();
  169. }
  170. }
  171. }
  172. }
  173. public void LockDirtyValue(bool forceValue)
  174. {
  175. ++this.dirtyValueLockCount;
  176. if (this.dirtyValueLockCount == 1)
  177. {
  178. this.lockedDirtyValue = forceValue;
  179. }
  180. }
  181. public void UnlockDirtyValue()
  182. {
  183. --this.dirtyValueLockCount;
  184. if (this.dirtyValueLockCount == 0)
  185. {
  186. OnChanged();
  187. }
  188. else if (this.dirtyValueLockCount < 0)
  189. {
  190. throw new InvalidOperationException("Calls to UnlockDirtyValue() must be matched by a preceding call to LockDirtyValue()");
  191. }
  192. }
  193. public bool Checked
  194. {
  195. get
  196. {
  197. return (CheckState == CheckState.Checked);
  198. }
  199. set
  200. {
  201. if (value)
  202. {
  203. CheckState = CheckState.Checked;
  204. }
  205. else
  206. {
  207. CheckState = CheckState.Unchecked;
  208. }
  209. }
  210. }
  211. public CheckState CheckState
  212. {
  213. get
  214. {
  215. return this.checkState;
  216. }
  217. set
  218. {
  219. if (this.checkState != value)
  220. {
  221. this.checkState = value;
  222. OnChanged();
  223. OnCheckChanged();
  224. }
  225. }
  226. }
  227. public PushButtonState CheckRenderState
  228. {
  229. get
  230. {
  231. return this.checkRenderState;
  232. }
  233. set
  234. {
  235. if (this.checkRenderState != value)
  236. {
  237. this.checkRenderState = value;
  238. OnChanged();
  239. }
  240. }
  241. }
  242. public PushButtonState CloseRenderState
  243. {
  244. get
  245. {
  246. return this.closeRenderState;
  247. }
  248. set
  249. {
  250. if (this.closeRenderState != value)
  251. {
  252. this.closeRenderState = value;
  253. OnChanged();
  254. }
  255. }
  256. }
  257. public void SetPartRenderState(ItemPart itemPart, PushButtonState renderState)
  258. {
  259. switch (itemPart)
  260. {
  261. case ItemPart.None:
  262. break;
  263. case ItemPart.CloseButton:
  264. CloseRenderState = renderState;
  265. break;
  266. case ItemPart.Image:
  267. ImageRenderState = renderState;
  268. break;
  269. default:
  270. throw new InvalidEnumArgumentException();
  271. }
  272. }
  273. public object Tag
  274. {
  275. get
  276. {
  277. return this.tag;
  278. }
  279. set
  280. {
  281. this.tag = value;
  282. OnChanged();
  283. }
  284. }
  285. public void Update()
  286. {
  287. OnChanged();
  288. }
  289. public Item()
  290. {
  291. }
  292. public Item(Image image)
  293. {
  294. this.image = image;
  295. }
  296. }
  297. private bool managedFocus = false;
  298. private bool showScrollButtons = true;//false;
  299. private bool allowMultiChoise = true;//模式是多选模式
  300. private ArrowButton leftScrollButton;
  301. private ArrowButton rightScrollButton;
  302. private int scrollOffset = 0;
  303. private bool showCheckButtons = false;
  304. private bool showCloseButtons = false;
  305. private const int closeButtonLength = 13;
  306. private int imagePadding = 2;
  307. private int closeButtonPadding = 2;
  308. private int mouseOverIndex = -1;
  309. private ItemPart mouseOverItemPart = ItemPart.None;
  310. private bool mouseOverApplyRendering = false;
  311. private int mouseDownIndex = -1;
  312. private MouseButtons mouseDownButton = MouseButtons.None;
  313. private ItemPart mouseDownItemPart = ItemPart.None;
  314. private bool mouseDownApplyRendering = false;
  315. private bool showCheckedColor = true;
  316. private bool drawDirtyOverlay = true;
  317. public bool ShowCheckedColor
  318. {
  319. get
  320. {
  321. return this.showCheckedColor;
  322. }
  323. set
  324. {
  325. if (this.showCheckedColor != value)
  326. {
  327. this.showCheckedColor = value;
  328. Refresh();
  329. }
  330. }
  331. }
  332. public bool DrawDirtyOverlay
  333. {
  334. get
  335. {
  336. return this.drawDirtyOverlay;
  337. }
  338. set
  339. {
  340. if (this.drawDirtyOverlay != value)
  341. {
  342. this.drawDirtyOverlay = value;
  343. Refresh();
  344. }
  345. }
  346. }
  347. // This is done as an optimization: otherwise we're getting flooded with MouseMove events
  348. // and constantly refreshing our rendering. So CPU usage goes to heck.
  349. private Point lastMouseMovePt = new Point(-32000, -32000);
  350. public List<Item> items = new List<Item>();
  351. protected ArrowButton LeftScrollButton
  352. {
  353. get
  354. {
  355. return this.leftScrollButton;
  356. }
  357. }
  358. protected ArrowButton RightScrollButton
  359. {
  360. get
  361. {
  362. return this.rightScrollButton;
  363. }
  364. }
  365. private void MouseStatesToItemStates()
  366. {
  367. UI.SuspendControlPainting(this);
  368. for (int i = 0; i < this.items.Count; ++i)
  369. {
  370. this.items[i].CheckRenderState = PushButtonState.Normal;
  371. this.items[i].CloseRenderState = PushButtonState.Normal;
  372. this.items[i].ImageRenderState = PushButtonState.Normal;
  373. this.items[i].Selected = false;
  374. }
  375. if (this.mouseDownApplyRendering)
  376. {
  377. if (this.mouseDownIndex < 0 || this.mouseDownIndex >= this.items.Count)
  378. {
  379. this.mouseDownApplyRendering = false;
  380. }
  381. else
  382. {
  383. this.items[this.mouseDownIndex].SetPartRenderState(this.mouseDownItemPart, PushButtonState.Pressed);
  384. this.items[this.mouseDownIndex].Selected = true;
  385. }
  386. }
  387. else if (this.mouseOverApplyRendering)
  388. {
  389. if (this.mouseOverIndex < 0 || this.mouseOverIndex >= this.items.Count)
  390. {
  391. this.mouseOverApplyRendering = false;
  392. }
  393. else
  394. {
  395. this.items[this.mouseOverIndex].SetPartRenderState(this.mouseOverItemPart, PushButtonState.Hot);
  396. this.items[this.mouseOverIndex].Selected = true;
  397. }
  398. }
  399. UI.ResumeControlPainting(this);
  400. Invalidate();
  401. }
  402. public IBottomNameStrip()
  403. {
  404. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
  405. SetStyle(ControlStyles.Selectable, false);
  406. DoubleBuffered = true;
  407. ResizeRedraw = true;
  408. InitializeComponent();
  409. }
  410. private void InitializeComponent()
  411. {
  412. this.leftScrollButton = new ArrowButton();
  413. this.rightScrollButton = new ArrowButton();
  414. SuspendLayout();
  415. //
  416. // leftScrollButton
  417. //
  418. this.leftScrollButton.Name = "leftScrollButton";
  419. this.leftScrollButton.drawTransparentBackColor = false;
  420. this.leftScrollButton.ArrowDirection = ArrowDirection.Left;
  421. this.leftScrollButton.ArrowOutlineWidth = 1.0f;
  422. this.leftScrollButton.Click += new EventHandler(LeftScrollButton_Click);
  423. this.leftScrollButton.DrawWithGradient = true;
  424. //
  425. // rightScrollButton
  426. //
  427. this.rightScrollButton.Name = "rightScrollButton";
  428. this.rightScrollButton.drawTransparentBackColor = false;
  429. this.rightScrollButton.ArrowDirection = ArrowDirection.Right;
  430. this.rightScrollButton.ArrowOutlineWidth = 1.0f;
  431. this.rightScrollButton.Click += new EventHandler(RightScrollButton_Click);
  432. this.rightScrollButton.DrawWithGradient = true;
  433. //
  434. // ImageStrip
  435. //
  436. this.Name = "ImageStrip";
  437. this.TabStop = false;
  438. this.Controls.Add(this.leftScrollButton);
  439. this.Controls.Add(this.rightScrollButton);
  440. ResumeLayout();
  441. PerformLayout();
  442. }
  443. public event EventHandler<EventArgs<ArrowDirection>> ScrollArrowClicked;
  444. protected virtual void OnScrollArrowClicked(ArrowDirection arrowDirection)
  445. {
  446. if (ScrollArrowClicked != null)
  447. {
  448. ScrollArrowClicked(this, new EventArgs<ArrowDirection>(arrowDirection));
  449. }
  450. }
  451. private void LeftScrollButton_Click(object sender, EventArgs e)
  452. {
  453. Focus();
  454. OnScrollArrowClicked(ArrowDirection.Left);
  455. }
  456. private void RightScrollButton_Click(object sender, EventArgs e)
  457. {
  458. Focus();
  459. OnScrollArrowClicked(ArrowDirection.Right);
  460. }
  461. /// <summary>
  462. /// This event is raised when this control wishes to relinquish focus.
  463. /// </summary>
  464. public event EventHandler RelinquishFocus;
  465. private void OnRelinquishFocus()
  466. {
  467. if (RelinquishFocus != null)
  468. {
  469. RelinquishFocus(this, EventArgs.Empty);
  470. }
  471. }
  472. /// <summary>
  473. /// Gets or sets whether the control manages focus.
  474. /// </summary>
  475. /// <remarks>
  476. /// If this is true, the toolstrip will capture focus when the mouse enters its client area. It will then
  477. /// relinquish focus (via the RelinquishFocus event) when the mouse leaves. It will not capture or
  478. /// attempt to relinquish focus if MenuStripEx.IsAnyMenuActive returns true.
  479. /// </remarks>
  480. public bool ManagedFocus
  481. {
  482. get
  483. {
  484. return this.managedFocus;
  485. }
  486. set
  487. {
  488. this.managedFocus = value;
  489. }
  490. }
  491. public void AddItem(Item newItem)
  492. {
  493. if (this.items.Contains(newItem))
  494. {
  495. throw new ArgumentException("newItem was already added to this control");
  496. }
  497. //在这里计算名称的宽度开始 zyh
  498. //需要保证名称不能为空
  499. Size textSize = TextRenderer.MeasureText(newItem.Name, new Font(SystemFonts.DefaultFont.Name, 10, FontStyle.Regular));
  500. newItem.Rectangle = new Rectangle(0, 0, textSize.Width + 33, ClientSize.Height);
  501. //在这里计算名称的宽度结束 zyh
  502. newItem.Changed += Item_Changed;
  503. this.items.Add(newItem);
  504. PerformLayout();
  505. Invalidate();
  506. }
  507. public void RemoveItem(Item item)
  508. {
  509. if (!this.items.Contains(item))
  510. {
  511. throw new ArgumentException("item was never added to this control");
  512. }
  513. item.Changed -= Item_Changed;
  514. this.items.Remove(item);
  515. PerformLayout();
  516. Invalidate();
  517. }
  518. public void ClearItems()
  519. {
  520. SuspendLayout();
  521. UI.SuspendControlPainting(this);
  522. while (this.items.Count > 0)
  523. {
  524. RemoveItem(this.items[this.items.Count - 1]);
  525. }
  526. UI.ResumeControlPainting(this);
  527. ResumeLayout(true);
  528. Invalidate();
  529. }
  530. private void Item_Changed(object sender, EventArgs e)
  531. {
  532. Invalidate();
  533. }
  534. /// <summary>
  535. /// Raised when an item is clicked on.
  536. /// </summary>
  537. /// <remarks>
  538. /// e.Data.First is a reference to the Item.
  539. /// e.Data.Second is the ItemPart.
  540. /// e.Data.Third is the MouseButtons that was used to click on the ItemPart.
  541. /// </remarks>
  542. public event EventHandler<EventArgs<Triple<Item, ItemPart, MouseButtons>>> ItemClicked;
  543. protected virtual void OnItemClicked(Item item, ItemPart itemPart, MouseButtons mouseButtons)
  544. {
  545. if (ItemClicked != null)
  546. {
  547. ItemClicked(this, new EventArgs<Triple<Item, ItemPart, MouseButtons>>(
  548. Triple.Create(item, itemPart, mouseButtons)));
  549. }
  550. }
  551. public void PerformItemClick(int itemIndex, ItemPart itemPart, MouseButtons mouseButtons)
  552. {
  553. PerformItemClick(this.items[itemIndex], itemPart, mouseButtons);
  554. }
  555. public void PerformItemClick(Item item, ItemPart itemPart, MouseButtons mouseButtons)
  556. {
  557. OnItemClicked(item, itemPart, mouseButtons);
  558. }
  559. public Item[] Items
  560. {
  561. get
  562. {
  563. return this.items.ToArray();
  564. }
  565. }
  566. public int ItemCount
  567. {
  568. get
  569. {
  570. return this.items.Count;
  571. }
  572. }
  573. public bool AllowMultiChoise
  574. {
  575. get
  576. {
  577. return this.allowMultiChoise;
  578. }
  579. set
  580. {
  581. if (this.allowMultiChoise != value)
  582. {
  583. this.allowMultiChoise = value;
  584. PerformLayout();
  585. Invalidate(true);
  586. }
  587. }
  588. }
  589. public int MinScrollOffset
  590. {
  591. get
  592. {
  593. return 0;
  594. }
  595. }
  596. public int MaxScrollOffset
  597. {
  598. get
  599. {
  600. int itemsLength = GetAllItemWidth();//itemSize.Width * this.items.Count;
  601. int viewLength = itemsLength - ClientSize.Width;
  602. int maxScrollOffset = Math.Max(0, viewLength);
  603. return maxScrollOffset;
  604. }
  605. }
  606. public int ScrollOffset
  607. {
  608. get
  609. {
  610. return this.scrollOffset;
  611. }
  612. set
  613. {
  614. int clampedValue = Utility.Clamp(value, MinScrollOffset, MaxScrollOffset);
  615. if (this.scrollOffset != clampedValue)
  616. {
  617. this.scrollOffset = clampedValue;
  618. OnScrollOffsetChanged();
  619. Invalidate(true);
  620. }
  621. }
  622. }
  623. public event EventHandler ScrollOffsetChanged;
  624. protected virtual void OnScrollOffsetChanged()
  625. {
  626. PerformLayout();
  627. if (ScrollOffsetChanged != null)
  628. {
  629. ScrollOffsetChanged(this, EventArgs.Empty);
  630. }
  631. }
  632. /// <summary>
  633. /// 获取所有item的宽度和,
  634. /// 因为每个item和原来不一样了
  635. /// 原本item是等宽
  636. /// </summary>
  637. /// <returns></returns>
  638. public int GetAllItemWidth()
  639. {
  640. int widths = 0;
  641. for (int y = 0; y < items.Count; y++)
  642. {
  643. widths += items[y].Rectangle.Width;
  644. }
  645. return widths;
  646. }
  647. /// <summary>
  648. /// Gets the viewable area, in View coordinate space.
  649. /// </summary>
  650. public Rectangle ViewRectangle
  651. {
  652. get
  653. {
  654. Size itemSize = ItemSize;
  655. return new Rectangle(0, 0, GetAllItemWidth(), itemSize.Height);
  656. }
  657. }
  658. protected override void OnLayout(LayoutEventArgs levent)
  659. {
  660. int arrowWidth = UI.ScaleWidth(16);
  661. ScrollOffset = Utility.Clamp(this.scrollOffset, MinScrollOffset, MaxScrollOffset);
  662. // Determine arrow visibility / position
  663. this.leftScrollButton.Size = new Size(arrowWidth, ClientSize.Height);
  664. this.leftScrollButton.Location = new Point(0, 0);
  665. this.rightScrollButton.Size = new Size(arrowWidth, ClientSize.Height);
  666. this.rightScrollButton.Location = new Point(ClientSize.Width - this.rightScrollButton.Width, 0);
  667. bool showEitherButton = this.showScrollButtons && (this.ViewRectangle.Width > ClientRectangle.Width);
  668. bool showRightButton = (this.scrollOffset < MaxScrollOffset) && showEitherButton;
  669. bool showLeftButton = (this.scrollOffset > MinScrollOffset) && showEitherButton;
  670. this.rightScrollButton.Enabled = showRightButton;
  671. this.rightScrollButton.Visible = showRightButton;
  672. this.leftScrollButton.Enabled = showLeftButton;
  673. this.leftScrollButton.Visible = showLeftButton;
  674. base.OnLayout(levent);
  675. }
  676. public bool ShowCheckButtons
  677. {
  678. get
  679. {
  680. return this.showCheckButtons;
  681. }
  682. set
  683. {
  684. if (this.showCheckButtons != value)
  685. {
  686. this.showCheckButtons = value;
  687. PerformLayout();
  688. Invalidate();
  689. }
  690. }
  691. }
  692. public bool ShowCloseButtons
  693. {
  694. get
  695. {
  696. return this.showCloseButtons;
  697. }
  698. set
  699. {
  700. if (this.showCloseButtons != value)
  701. {
  702. this.showCloseButtons = value;
  703. PerformLayout();
  704. Invalidate();
  705. }
  706. }
  707. }
  708. public int PreferredMinClientWidth
  709. {
  710. get
  711. {
  712. if (this.items.Count == 0)
  713. {
  714. return 0;
  715. }
  716. int minWidth = ItemSize.Width;
  717. if (this.leftScrollButton.Visible || this.rightScrollButton.Visible)
  718. {
  719. minWidth += this.leftScrollButton.Width;
  720. minWidth += this.rightScrollButton.Width;
  721. }
  722. minWidth = Math.Min(minWidth, ViewRectangle.Width);
  723. return minWidth;
  724. }
  725. }
  726. public Size PreferredImageSize
  727. {
  728. get
  729. {
  730. Rectangle itemRect;
  731. MeasureItemPartRectangles(out itemRect, null);
  732. return new Size(itemRect.Width - imagePadding * 2, itemRect.Height - imagePadding * 2);
  733. }
  734. }
  735. public Size ItemSize
  736. {
  737. get
  738. {
  739. Rectangle itemRect;
  740. MeasureItemPartRectangles(out itemRect, null);
  741. return itemRect.Size;
  742. }
  743. }
  744. protected virtual void DrawItemBackground(Graphics graphics, Item item, Rectangle itemRect)
  745. {
  746. }
  747. protected virtual void DrawItemHighlight(
  748. Graphics graphics,
  749. Item item,
  750. Rectangle itemRect,
  751. Rectangle highlightRect)
  752. {
  753. Color backFillColor;
  754. Color outlineColor;
  755. Color textColor;
  756. if (item.Checked && this.showCheckedColor)
  757. {
  758. backFillColor = Color.FromArgb(192, SystemColors.Highlight);
  759. outlineColor = backFillColor;
  760. textColor = Color.White;
  761. }
  762. else if (item.Selected)
  763. {
  764. backFillColor = Color.FromArgb(64, SystemColors.HotTrack);
  765. outlineColor = Color.FromArgb(64, SystemColors.HotTrack);
  766. textColor = Color.Black;
  767. }
  768. else
  769. {
  770. backFillColor = Color.Transparent;
  771. outlineColor = Color.Transparent;
  772. textColor = Color.Black;
  773. }
  774. using (SolidBrush backFillBrush = new SolidBrush(backFillColor))
  775. {
  776. graphics.FillRectangle(backFillBrush, highlightRect);
  777. }
  778. using (Pen outlinePen = new Pen(outlineColor))
  779. {
  780. graphics.DrawRectangle(outlinePen, highlightRect.X, highlightRect.Y, highlightRect.Width - 1, highlightRect.Height - 1);
  781. }
  782. CheckBoxRenderer.DrawCheckBox(graphics, new Point(itemRect.X + 10, itemRect.Y + 10), (item.Checked && !this.showCheckedColor) ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal);
  783. graphics.DrawString(item.Name, new Font(SystemFonts.DefaultFont.Name, 10, FontStyle.Regular), new SolidBrush(item.NColor != null ? item.NColor : textColor), new PointF(itemRect.X + 24, itemRect.Y + 10));
  784. graphics.DrawLine(new Pen(Color.FromArgb(189, 189, 189)), itemRect.X + itemRect.Width - 1, itemRect.Y, itemRect.X + itemRect.Width - 1, itemRect.Y + 25);
  785. }
  786. protected virtual void DrawItemCloseButton(
  787. Graphics graphics,
  788. Item item,
  789. Rectangle itemRect,
  790. Rectangle closeButtonRect)
  791. {
  792. if (item.Checked && item.Selected)
  793. {
  794. const string resourceNamePrefix = "Images.ImageStrip.CloseButton.";
  795. const string resourceNameSuffix = ".png";
  796. string resourceNameInfix = item.CloseRenderState.ToString();
  797. string resourceName = resourceNamePrefix + resourceNameInfix + resourceNameSuffix;
  798. ImageResource imageResource = PdnResources.GetImageResource(resourceName);
  799. Image image = imageResource.Reference;
  800. graphics.SmoothingMode = SmoothingMode.AntiAlias;
  801. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  802. graphics.DrawImage(image, closeButtonRect, new Rectangle(0, 0, image.Width, image.Width), GraphicsUnit.Pixel);
  803. }
  804. }
  805. protected virtual void DrawItemDirtyOverlay(
  806. Graphics graphics,
  807. Item item,
  808. Rectangle itemRect,
  809. Rectangle dirtyOverlayRect)
  810. {
  811. Color outerPenColor = Color.White;
  812. Color innerPenColor = Color.Orange;
  813. const int xInset = 2;
  814. int scaledXInset = UI.ScaleWidth(xInset);
  815. const float outerPenWidth = 4.0f;
  816. const float innerPenWidth = 2.0f;
  817. float scaledOuterPenWidth = UI.ScaleWidth(outerPenWidth);
  818. float scaledInnerPenWidth = UI.ScaleWidth(innerPenWidth);
  819. SmoothingMode oldSM = graphics.SmoothingMode;
  820. graphics.SmoothingMode = SmoothingMode.AntiAlias;
  821. int left = dirtyOverlayRect.Left + scaledXInset;
  822. int top = dirtyOverlayRect.Top + scaledXInset;
  823. int right = dirtyOverlayRect.Right - scaledXInset;
  824. int bottom = dirtyOverlayRect.Bottom - scaledXInset;
  825. float r = Math.Min((right - left) / 2.0f, (bottom - top) / 2.0f);
  826. PointF centerPt = new PointF((left + right) / 2.0f, (top + bottom) / 2.0f);
  827. float twoPiOver5 = (float)(Math.PI * 0.4);
  828. PointF a = new PointF(centerPt.X + r * (float)Math.Sin(twoPiOver5), centerPt.Y - r * (float)Math.Cos(twoPiOver5));
  829. PointF b = new PointF(centerPt.X + r * (float)Math.Sin(2 * twoPiOver5), centerPt.Y - r * (float)Math.Cos(2 * twoPiOver5));
  830. PointF c = new PointF(centerPt.X + r * (float)Math.Sin(3 * twoPiOver5), centerPt.Y - r * (float)Math.Cos(3 * twoPiOver5));
  831. PointF d = new PointF(centerPt.X + r * (float)Math.Sin(4 * twoPiOver5), centerPt.Y - r * (float)Math.Cos(4 * twoPiOver5));
  832. PointF e = new PointF(centerPt.X + r * (float)Math.Sin(5 * twoPiOver5), centerPt.Y - r * (float)Math.Cos(5 * twoPiOver5));
  833. PointF[] lines =
  834. new PointF[]
  835. {
  836. centerPt, a,
  837. centerPt, b,
  838. centerPt, c,
  839. centerPt, d,
  840. centerPt, e
  841. };
  842. using (Pen outerPen = new Pen(outerPenColor, scaledOuterPenWidth))
  843. {
  844. for (int i = 0; i < lines.Length; i += 2)
  845. {
  846. graphics.DrawLine(outerPen, lines[i], lines[i + 1]);
  847. }
  848. }
  849. using (Pen innerPen = new Pen(innerPenColor, scaledInnerPenWidth))
  850. {
  851. for (int i = 0; i < lines.Length; i += 2)
  852. {
  853. graphics.DrawLine(innerPen, lines[i], lines[i + 1]);
  854. }
  855. }
  856. graphics.SmoothingMode = oldSM;
  857. }
  858. private void DrawItem(Graphics g, Item item, Point offset)
  859. {
  860. Rectangle itemRect;
  861. //Rectangle checkButtonRect;
  862. Rectangle closeButtonRect;
  863. Rectangle dirtyOverlayRect;
  864. MeasureItemPartRectangles(
  865. item,
  866. out itemRect,
  867. out closeButtonRect,
  868. out dirtyOverlayRect);
  869. itemRect.X += offset.X;
  870. itemRect.Y += offset.Y;
  871. closeButtonRect.X += offset.X;
  872. closeButtonRect.Y += offset.Y;
  873. dirtyOverlayRect.X += offset.X;
  874. dirtyOverlayRect.Y += offset.Y;
  875. DrawItemBackground(g, item, itemRect);
  876. Rectangle highlightRect = itemRect;
  877. DrawItemHighlight(g, item, itemRect, highlightRect);
  878. if (this.showCloseButtons)
  879. {
  880. DrawItemCloseButton(g, item, itemRect, closeButtonRect);
  881. }
  882. if (this.drawDirtyOverlay && item.Dirty)
  883. {
  884. DrawItemDirtyOverlay(g, item, itemRect, dirtyOverlayRect);
  885. }
  886. }
  887. public Point ClientPointToViewPoint(Point clientPt)
  888. {
  889. int viewX = clientPt.X + this.scrollOffset;
  890. return new Point(viewX, clientPt.Y);
  891. }
  892. public Rectangle ClientRectangleToViewRectangle(Rectangle clientRect)
  893. {
  894. Point viewPt = ClientPointToViewPoint(clientRect.Location);
  895. return new Rectangle(viewPt, clientRect.Size);
  896. }
  897. public Point ViewPointToClientPoint(Point viewPt)
  898. {
  899. int clientX = viewPt.X - this.scrollOffset;
  900. return new Point(clientX, viewPt.Y);
  901. }
  902. public Rectangle ViewRectangleToClientRectangle(Rectangle viewRect)
  903. {
  904. Point clientPt = ViewPointToClientPoint(viewRect.Location);
  905. return new Rectangle(clientPt, viewRect.Size);
  906. }
  907. private Point ViewPointToItemPoint(int itemIndex, Point viewPt)
  908. {
  909. Rectangle itemRect = ItemIndexToItemViewRectangle(itemIndex);
  910. Point itemPt = new Point(viewPt.X - itemRect.X, viewPt.Y);
  911. return itemPt;
  912. }
  913. private Rectangle ItemIndexToItemViewRectangle(int itemIndex)
  914. {
  915. return items[itemIndex].Rectangle;
  916. //Size itemSize = ItemSize;
  917. //return new Rectangle(itemSize.Width * itemIndex, itemSize.Height, itemSize.Width, itemSize.Height);
  918. }
  919. public int ViewPointToItemIndex(Point viewPt)
  920. {
  921. if (!ViewRectangle.Contains(viewPt))
  922. {
  923. return -1;
  924. }
  925. int index = -1;// viewPt.X / itemSize.Width;
  926. int width = 0;
  927. //循环所有item,判断是第几个
  928. for (int i = 0; i < items.Count; i++)
  929. {
  930. if (viewPt.X >= items[i].Rectangle.X + width &&
  931. viewPt.X < items[i].Rectangle.X + width + items[i].Rectangle.Width)
  932. {
  933. index = i;
  934. break;
  935. }
  936. width += items[i].Rectangle.Width;
  937. }
  938. return index;
  939. }
  940. private void MeasureItemPartRectangles(
  941. out Rectangle itemRect,
  942. Item item)
  943. {
  944. //CheckBoxRenderer
  945. Size textSize = new Size(0, 0);
  946. if (item != null)
  947. textSize = /*CheckBoxRenderer*/TextRenderer.MeasureText(item.Name, new Font(SystemFonts.DefaultFont.Name, 10, FontStyle.Regular));
  948. itemRect = new Rectangle(
  949. 0,
  950. 0,
  951. textSize.Width == 0 ? 120 : textSize.Width + 33,//120,//ClientSize.Height
  952. ClientSize.Height);
  953. if (item != null)
  954. item.Rectangle = itemRect;
  955. }
  956. /// <summary>
  957. /// 计算位置,关闭按钮、修改标记
  958. /// </summary>
  959. /// <param name="item"></param>
  960. /// <param name="itemRect"></param>
  961. /// <param name="closeButtonRect"></param>
  962. /// <param name="dirtyOverlayRect"></param>
  963. private void MeasureItemPartRectangles(
  964. Item item,
  965. out Rectangle itemRect,
  966. out Rectangle closeButtonRect,
  967. out Rectangle dirtyOverlayRect)
  968. {
  969. MeasureItemPartRectangles(out itemRect, item);
  970. int scaledCloseButtonLength = UI.ScaleWidth(closeButtonLength);
  971. int scaledCloseButtonPadding = UI.ScaleWidth(closeButtonPadding);
  972. closeButtonRect = new Rectangle(
  973. itemRect.Width - scaledCloseButtonLength - scaledCloseButtonPadding,
  974. scaledCloseButtonPadding,
  975. scaledCloseButtonLength,
  976. scaledCloseButtonLength);
  977. dirtyOverlayRect = new Rectangle(
  978. scaledCloseButtonPadding,
  979. scaledCloseButtonPadding,
  980. scaledCloseButtonLength,
  981. scaledCloseButtonLength);
  982. }
  983. private ItemPart ItemPointToItemPart(Item item, Point pt)
  984. {
  985. Rectangle itemRect;
  986. Rectangle closeButtonRect;
  987. Rectangle dirtyOverlayRect;
  988. MeasureItemPartRectangles(
  989. item,
  990. out itemRect,
  991. out closeButtonRect,
  992. out dirtyOverlayRect);
  993. //判断是否是点击了关闭按钮
  994. int dhud = 0;
  995. int index = items.FindIndex(a => a == item);
  996. if (index > 0)
  997. {
  998. dhud = GetZeroToIndexWidth(index - 1);
  999. }
  1000. closeButtonRect.X += dhud;
  1001. if (closeButtonRect.Contains(pt))
  1002. {
  1003. return ItemPart.CloseButton;
  1004. }
  1005. int width = 0;
  1006. bool isPart = false;
  1007. //循环所有item,判断是第几个
  1008. for (int i = 0; i < items.Count; i++)
  1009. {
  1010. if (pt.X >= items[i].Rectangle.X + width &&
  1011. pt.X < items[i].Rectangle.X + width + items[i].Rectangle.Width)
  1012. {
  1013. isPart = true;
  1014. break;
  1015. }
  1016. width += items[i].Rectangle.Width;
  1017. }
  1018. if (isPart)
  1019. {
  1020. return ItemPart.Image;
  1021. }
  1022. return ItemPart.None;
  1023. }
  1024. private int GetZeroToIndexWidth(int index)
  1025. {
  1026. int m = 0;
  1027. for (int i = 0; i < items.Count; i++)
  1028. {
  1029. if (i <= index)
  1030. m += items[i].Rectangle.Width;
  1031. }
  1032. return m;
  1033. }
  1034. private Rectangle ItemIndexToClientRect(int itemIndex)
  1035. {
  1036. Size itemSize = ItemSize;
  1037. Rectangle clientRect = new Rectangle(
  1038. GetZeroToIndexWidth(itemIndex),//itemSize.Width * itemIndex,
  1039. 0,
  1040. itemIndex < 0 ? itemSize.Width : items[itemIndex].Rectangle.Width,//itemSize.Width,
  1041. itemSize.Height);
  1042. return clientRect;
  1043. }
  1044. /// <summary>
  1045. /// 计算偏移量
  1046. /// </summary>
  1047. /// <param name="itemIndex">下标</param>
  1048. /// <param name="minOffset">最小偏移</param>
  1049. /// <param name="maxOffset">最大偏移</param>
  1050. /// <param name="minFullyShownOffset"></param>
  1051. /// <param name="maxFullyShownOffset"></param>
  1052. private void CalculateVisibleScrollOffsets(
  1053. int itemIndex,
  1054. out int minOffset,
  1055. out int maxOffset,
  1056. out int minFullyShownOffset,
  1057. out int maxFullyShownOffset)
  1058. {
  1059. Rectangle itemClientRect = ItemIndexToClientRect(itemIndex);
  1060. minOffset = itemClientRect.Left + 1 - ClientSize.Width;
  1061. maxOffset = itemClientRect.Right - 1;
  1062. minFullyShownOffset = itemClientRect.Right - ClientSize.Width;
  1063. maxFullyShownOffset = itemClientRect.Left;
  1064. if (this.leftScrollButton.Visible)
  1065. {
  1066. maxOffset -= this.leftScrollButton.Width;
  1067. maxFullyShownOffset -= this.leftScrollButton.Width;
  1068. }
  1069. if (this.rightScrollButton.Visible)
  1070. {
  1071. minOffset += this.rightScrollButton.Width;
  1072. minFullyShownOffset += this.rightScrollButton.Width;
  1073. }
  1074. }
  1075. public Rectangle ScrolledViewRect
  1076. {
  1077. get
  1078. {
  1079. return new Rectangle(this.scrollOffset, 0, ClientSize.Width, ClientSize.Height);
  1080. }
  1081. }
  1082. /// <summary>
  1083. /// 判断item是否显示
  1084. /// </summary>
  1085. /// <param name="index"></param>
  1086. /// <returns></returns>
  1087. public bool IsItemVisible(int index)
  1088. {
  1089. Rectangle itemRect = ItemIndexToClientRect(index);
  1090. Rectangle intersect = Rectangle.Intersect(itemRect, ScrolledViewRect);
  1091. return (intersect.Width > 0 || intersect.Height > 0);
  1092. }
  1093. public bool IsItemFullyVisible(int index)
  1094. {
  1095. Rectangle itemRect = ItemIndexToClientRect(index);
  1096. Rectangle svRect = ScrolledViewRect;
  1097. if (this.leftScrollButton.Visible)
  1098. {
  1099. svRect.X += this.leftScrollButton.Width;
  1100. svRect.Width -= this.leftScrollButton.Width;
  1101. }
  1102. if (this.rightScrollButton.Visible)
  1103. {
  1104. svRect.Width -= this.rightScrollButton.Width;
  1105. }
  1106. Rectangle intersect = Rectangle.Intersect(itemRect, svRect);
  1107. return (intersect == itemRect);
  1108. }
  1109. public void EnsureItemFullyVisible(Item item)
  1110. {
  1111. int index = this.items.IndexOf(item);
  1112. EnsureItemFullyVisible(index);
  1113. }
  1114. public void EnsureItemFullyVisible(int index)
  1115. {
  1116. if (IsItemFullyVisible(index))
  1117. {
  1118. return;
  1119. }
  1120. int minOffset;
  1121. int maxOffset;
  1122. int minFullyShownOffset;
  1123. int maxFullyShownOffset;
  1124. CalculateVisibleScrollOffsets(index, out minOffset, out maxOffset,
  1125. out minFullyShownOffset, out maxFullyShownOffset);
  1126. // Pick the offset that moves the image the fewest number of pixels
  1127. int oldOffset = this.scrollOffset;
  1128. int dxMin = Math.Abs(oldOffset - minFullyShownOffset);
  1129. int dxMax = Math.Abs(oldOffset - maxFullyShownOffset);
  1130. if (dxMin <= dxMax)
  1131. {
  1132. this.ScrollOffset = minFullyShownOffset;
  1133. }
  1134. else
  1135. {
  1136. this.ScrollOffset = maxFullyShownOffset;
  1137. }
  1138. }
  1139. private void ForceMouseMove()
  1140. {
  1141. Point clientPt = PointToClient(Control.MousePosition);
  1142. this.lastMouseMovePt = new Point(this.lastMouseMovePt.X + 1, this.lastMouseMovePt.Y + 1);
  1143. MouseEventArgs me = new MouseEventArgs(MouseButtons.None, 0, clientPt.X, clientPt.Y, 0);
  1144. OnMouseMove(me);
  1145. }
  1146. private void GetFocus()
  1147. {
  1148. if (this.managedFocus && !MenuStripEx.IsAnyMenuActive && UI.IsOurAppActive)
  1149. {
  1150. this.Focus();
  1151. }
  1152. }
  1153. #region 绘制、鼠标进入、离开、按下、移动、抬起、滚轮事件
  1154. protected override void OnPaint(PaintEventArgs e)
  1155. {
  1156. if (UI.IsControlPaintingEnabled(this))
  1157. {
  1158. if (this.items != null && this.items.Count > 0)
  1159. {
  1160. Size itemSize = ItemSize;
  1161. Rectangle firstItemRect = new Rectangle(-this.scrollOffset, 0, items[0].Rectangle.Width, itemSize.Height);
  1162. for (int i = 0; i < this.items.Count; ++i)
  1163. {
  1164. //if (IsItemVisible(i)) //暂时注释掉,在观察
  1165. {
  1166. Point itemOffset = new Point(firstItemRect.X + GetZeroToIndexWidth(i - 1), firstItemRect.Y);
  1167. //Point itemOffset = new Point(firstItemRect.X + itemSize.Width * i, firstItemRect.Y);
  1168. DrawItem(e.Graphics, this.items[i], itemOffset);
  1169. }
  1170. }
  1171. }
  1172. }
  1173. base.OnPaint(e);
  1174. }
  1175. protected override void OnMouseDown(MouseEventArgs e)
  1176. {
  1177. if (this.mouseDownButton == MouseButtons.None)
  1178. {
  1179. Point clientPt = new Point(e.X, e.Y);
  1180. Point viewPt = ClientPointToViewPoint(clientPt);
  1181. int itemIndex = ViewPointToItemIndex(viewPt);
  1182. if (itemIndex >= 0 && itemIndex < this.items.Count)
  1183. {
  1184. Item item = this.items[itemIndex];
  1185. Point itemPt = ViewPointToItemPoint(itemIndex, viewPt);
  1186. ItemPart itemPart = ItemPointToItemPart(item, itemPt);
  1187. if (itemPart == ItemPart.Image)
  1188. {
  1189. OnItemClicked(item, itemPart, e.Button);
  1190. this.mouseDownApplyRendering = false;
  1191. this.mouseOverIndex = itemIndex;
  1192. this.mouseOverItemPart = itemPart;
  1193. this.mouseOverApplyRendering = true;
  1194. }
  1195. else
  1196. {
  1197. this.mouseDownIndex = itemIndex;
  1198. this.mouseDownItemPart = itemPart;
  1199. this.mouseDownButton = e.Button;
  1200. this.mouseDownApplyRendering = true;
  1201. this.mouseOverApplyRendering = false;
  1202. }
  1203. MouseStatesToItemStates();
  1204. Refresh();
  1205. }
  1206. }
  1207. base.OnMouseDown(e);
  1208. }
  1209. protected override void OnMouseMove(MouseEventArgs e)
  1210. {
  1211. GetFocus();
  1212. Point clientPt = new Point(e.X, e.Y);
  1213. if (clientPt != this.lastMouseMovePt)
  1214. {
  1215. Point viewPt = ClientPointToViewPoint(clientPt);
  1216. int itemIndex = ViewPointToItemIndex(viewPt);
  1217. if (this.mouseDownButton == MouseButtons.None)
  1218. {
  1219. if (itemIndex >= 0 && itemIndex < this.items.Count)
  1220. {
  1221. Item item = this.items[itemIndex];
  1222. Point itemPt = ViewPointToItemPoint(itemIndex, viewPt);
  1223. ItemPart itemPart = ItemPointToItemPart(item, itemPt);
  1224. this.mouseOverIndex = itemIndex;
  1225. this.mouseOverItemPart = itemPart;
  1226. this.mouseOverApplyRendering = true;
  1227. }
  1228. else
  1229. {
  1230. this.mouseOverApplyRendering = false;
  1231. }
  1232. }
  1233. else
  1234. {
  1235. this.mouseOverApplyRendering = false;
  1236. if (itemIndex != this.mouseDownIndex)
  1237. {
  1238. this.mouseDownApplyRendering = false;
  1239. }
  1240. else if (itemIndex < 0 || itemIndex >= this.items.Count)
  1241. {
  1242. this.mouseDownApplyRendering = false;
  1243. }
  1244. else
  1245. {
  1246. Item item = this.Items[itemIndex];
  1247. Point itemPt = ViewPointToItemPoint(itemIndex, viewPt);
  1248. ItemPart itemPart = ItemPointToItemPart(item, itemPt);
  1249. if (itemPart != this.mouseDownItemPart)
  1250. {
  1251. this.mouseDownApplyRendering = false;
  1252. }
  1253. }
  1254. }
  1255. MouseStatesToItemStates();
  1256. Refresh();
  1257. }
  1258. this.lastMouseMovePt = clientPt;
  1259. base.OnMouseMove(e);
  1260. }
  1261. protected override void OnMouseUp(MouseEventArgs e)
  1262. {
  1263. bool raisedClickEvent = false;
  1264. if (this.mouseDownButton == e.Button)
  1265. {
  1266. Point clientPt = new Point(e.X, e.Y);
  1267. Point viewPt = ClientPointToViewPoint(clientPt);
  1268. int itemIndex = ViewPointToItemIndex(viewPt);
  1269. if (itemIndex >= 0 && itemIndex < this.items.Count)
  1270. {
  1271. Item item = this.items[itemIndex];
  1272. Point itemPt = ViewPointToItemPoint(itemIndex, viewPt);
  1273. ItemPart itemPart = ItemPointToItemPart(item, itemPt);
  1274. if (itemIndex == this.mouseDownIndex && itemPart == this.mouseDownItemPart)
  1275. {
  1276. if (itemPart == ItemPart.CloseButton && !item.Checked)
  1277. {
  1278. // Can only close 'checked' images, just like how tab switching+closing works in IE7
  1279. itemPart = ItemPart.Image;
  1280. }
  1281. OnItemClicked(item, itemPart, this.mouseDownButton);
  1282. raisedClickEvent = true;
  1283. }
  1284. this.mouseOverApplyRendering = true;
  1285. this.mouseOverItemPart = itemPart;
  1286. this.mouseOverIndex = itemIndex;
  1287. }
  1288. this.mouseDownApplyRendering = false;
  1289. this.mouseDownButton = MouseButtons.None;
  1290. MouseStatesToItemStates();
  1291. Refresh();
  1292. }
  1293. if (raisedClickEvent)
  1294. {
  1295. ForceMouseMove();
  1296. }
  1297. base.OnMouseUp(e);
  1298. }
  1299. protected override void OnMouseWheel(MouseEventArgs e)
  1300. {
  1301. float count = (float)e.Delta / SystemInformation.MouseWheelScrollDelta;
  1302. int pixels = GetAllItemWidth();//(int)(count * ItemSize.Width);
  1303. int newSO = ScrollOffset - pixels;
  1304. ScrollOffset = newSO;
  1305. ForceMouseMove();
  1306. base.OnMouseWheel(e);
  1307. }
  1308. protected override void OnMouseEnter(EventArgs e)
  1309. {
  1310. GetFocus();
  1311. base.OnMouseEnter(e);
  1312. }
  1313. protected override void OnMouseLeave(EventArgs e)
  1314. {
  1315. this.mouseDownApplyRendering = false;
  1316. this.mouseOverApplyRendering = false;
  1317. MouseStatesToItemStates();
  1318. Refresh();
  1319. if (this.managedFocus && !MenuStripEx.IsAnyMenuActive && UI.IsOurAppActive)
  1320. {
  1321. OnRelinquishFocus();
  1322. }
  1323. base.OnMouseLeave(e);
  1324. }
  1325. #endregion
  1326. }
  1327. }