CPdfThumbnailUI.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. namespace Compdfkit_Tools.PDFControlUI
  7. {
  8. public partial class CPDFThumbnailUI : UserControl
  9. {
  10. /// <summary>
  11. /// Click to select the event in the thumbnail list
  12. /// </summary>
  13. public event EventHandler<int> SelectionChanged;
  14. /// <summary>
  15. /// Scroll state change event
  16. /// </summary>
  17. public event EventHandler<ScrollChangedEventArgs> ViewChanged;
  18. /// <summary>
  19. /// A list of thumbnail results
  20. /// </summary>
  21. private List<ThumbnailItem> thumbResultList=new List<ThumbnailItem>();
  22. public CPDFThumbnailUI()
  23. {
  24. InitializeComponent();
  25. }
  26. /// <summary>
  27. /// Select Result Change event
  28. /// </summary>
  29. private void ThumbListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  30. {
  31. SelectionChanged?.Invoke(this, ThumbListBox.SelectedIndex);
  32. }
  33. /// <summary>
  34. /// Set the thumbnail list
  35. /// </summary>
  36. /// <param name="thumbList"></param>
  37. public void SetThumbResult(List<ThumbnailItem> thumbList)
  38. {
  39. thumbResultList?.Clear();
  40. ThumbListBox.ItemsSource = null;
  41. if (thumbList == null || thumbList.Count == 0)
  42. {
  43. return;
  44. }
  45. thumbResultList.AddRange(thumbList);
  46. ThumbListBox.ItemsSource = thumbResultList;
  47. ThumbListBox.UpdateLayout();
  48. }
  49. /// <summary>
  50. /// Content scrolling events
  51. /// </summary>
  52. private void ThumbListBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
  53. {
  54. ViewChanged?.Invoke(this, e);
  55. }
  56. /// <summary>
  57. /// Determines whether the specified object is visible
  58. /// </summary>
  59. public bool IsItemVisible(ThumbnailItem checkItem)
  60. {
  61. if (ThumbListBox == null || thumbResultList == null || thumbResultList.Count == 0)
  62. {
  63. return false;
  64. }
  65. try
  66. {
  67. int index = thumbResultList.IndexOf(checkItem);
  68. if (index != -1)
  69. {
  70. ListBoxItem itemContainer = (ListBoxItem)ThumbListBox.ItemContainerGenerator.ContainerFromIndex(index);
  71. if (itemContainer==null || itemContainer.IsVisible == false || itemContainer.Visibility != Visibility.Visible)
  72. {
  73. return false;
  74. }
  75. GeneralTransform transform = itemContainer.TransformToAncestor(ThumbListBox);
  76. Rect visualRect= transform.TransformBounds(new Rect(
  77. 0,
  78. 0,
  79. itemContainer.ActualWidth,
  80. itemContainer.ActualHeight
  81. ));
  82. Rect containerRect=new Rect(
  83. 0,
  84. 0,
  85. ThumbListBox.ActualWidth,
  86. ThumbListBox.ActualHeight);
  87. containerRect.Intersect(visualRect);
  88. if (containerRect.Width>1 && containerRect.Height>1)
  89. {
  90. return true;
  91. }
  92. }
  93. }
  94. catch(Exception ex)
  95. {
  96. }
  97. return false;
  98. }
  99. /// <summary>
  100. /// Select an object
  101. /// </summary>
  102. public void SelectItem(int checkIndex)
  103. {
  104. if(ThumbListBox!=null && thumbResultList != null && thumbResultList.Count>checkIndex && checkIndex>=0)
  105. {
  106. ThumbnailItem thumbItem= thumbResultList[checkIndex];
  107. if(IsItemVisible(thumbItem)==false)
  108. {
  109. ThumbListBox.ScrollIntoView(thumbItem);
  110. }
  111. ThumbListBox.SelectedIndex = checkIndex;
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// Thumbnail object
  117. /// </summary>
  118. public class ThumbnailItem
  119. {
  120. /// <summary>
  121. /// Image width
  122. /// </summary>
  123. public int ImageWidth { get; set; }
  124. /// <summary>
  125. /// Image height
  126. /// </summary>
  127. public int ImageHeight { get; set; }
  128. /// <summary>
  129. ///Thumbnail width
  130. /// </summary>
  131. public int ThumbnailWidth { get; set; }
  132. /// <summary>
  133. /// Thumbnail height
  134. /// </summary>
  135. public int ThumbnailHeight { get; set; }
  136. /// <summary>
  137. /// Display page numbers
  138. /// </summary>
  139. public string ShowPageText
  140. {
  141. get
  142. {
  143. if (PageIndex >= 0)
  144. {
  145. return (PageIndex + 1).ToString();
  146. }
  147. return string.Empty;
  148. }
  149. }
  150. /// <summary>
  151. /// Page index (starts with 0)
  152. /// </summary>
  153. public int PageIndex { get; set; } = -1;
  154. /// <summary>
  155. /// Thumbnails are like content
  156. /// </summary>
  157. public Image ImageData { get; set; } = new Image();
  158. }
  159. }