ImageNameStrip.cs 45 KB

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