CPdfThumbnailUI.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Xml.Linq;
  18. namespace compdfkit_tools.PDFControlUI
  19. {
  20. public partial class CPDFThumbnailUI : UserControl
  21. {
  22. /// <summary>
  23. /// 缩略图列表点击选中事件
  24. /// </summary>
  25. public event EventHandler<int> SelectionChanged;
  26. /// <summary>
  27. /// 滚动状态改变事件
  28. /// </summary>
  29. public event EventHandler<ScrollChangedEventArgs> ViewChanged;
  30. /// <summary>
  31. /// 缩略图结果列表
  32. /// </summary>
  33. private List<ThumbnailItem> thumbResultList=new List<ThumbnailItem>();
  34. public CPDFThumbnailUI()
  35. {
  36. InitializeComponent();
  37. }
  38. /// <summary>
  39. /// 选中结果改变事件
  40. /// </summary>
  41. private void ThumbListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  42. {
  43. SelectionChanged?.Invoke(this, ThumbListBox.SelectedIndex);
  44. }
  45. /// <summary>
  46. /// 设置缩略图列表
  47. /// </summary>
  48. /// <param name="thumbList"></param>
  49. public void SetThumbResult(List<ThumbnailItem> thumbList)
  50. {
  51. thumbResultList?.Clear();
  52. ThumbListBox.ItemsSource = null;
  53. if (thumbList == null || thumbList.Count == 0)
  54. {
  55. return;
  56. }
  57. thumbResultList.AddRange(thumbList);
  58. ThumbListBox.ItemsSource = thumbResultList;
  59. ThumbListBox.UpdateLayout();
  60. }
  61. /// <summary>
  62. /// 内容滚动事件
  63. /// </summary>
  64. private void ThumbListBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
  65. {
  66. ViewChanged?.Invoke(this, e);
  67. }
  68. /// <summary>
  69. /// 判断指定对象是否可见
  70. /// </summary>
  71. public bool IsItemVisible(ThumbnailItem checkItem)
  72. {
  73. if (ThumbListBox == null || thumbResultList == null || thumbResultList.Count == 0)
  74. {
  75. return false;
  76. }
  77. try
  78. {
  79. int index = thumbResultList.IndexOf(checkItem);
  80. if (index != -1)
  81. {
  82. ListBoxItem itemContainer = (ListBoxItem)ThumbListBox.ItemContainerGenerator.ContainerFromIndex(index);
  83. if (itemContainer==null || itemContainer.IsVisible == false || itemContainer.Visibility != Visibility.Visible)
  84. {
  85. return false;
  86. }
  87. GeneralTransform transform = itemContainer.TransformToAncestor(ThumbListBox);
  88. Rect visualRect= transform.TransformBounds(new Rect(
  89. 0,
  90. 0,
  91. itemContainer.ActualWidth,
  92. itemContainer.ActualHeight
  93. ));
  94. Rect containerRect=new Rect(
  95. 0,
  96. 0,
  97. ThumbListBox.ActualWidth,
  98. ThumbListBox.ActualHeight);
  99. containerRect.Intersect(visualRect);
  100. if (containerRect.Width>1 && containerRect.Height>1)
  101. {
  102. return true;
  103. }
  104. }
  105. }
  106. catch(Exception ex)
  107. {
  108. }
  109. return false;
  110. }
  111. /// <summary>
  112. /// 选中某个对象
  113. /// </summary>
  114. public void SelectItem(int checkIndex)
  115. {
  116. if(ThumbListBox!=null && thumbResultList != null && thumbResultList.Count>checkIndex && checkIndex>=0)
  117. {
  118. ThumbnailItem thumbItem= thumbResultList[checkIndex];
  119. if(IsItemVisible(thumbItem)==false)
  120. {
  121. ThumbListBox.ScrollIntoView(thumbItem);
  122. }
  123. ThumbListBox.SelectedIndex = checkIndex;
  124. }
  125. }
  126. }
  127. /// <summary>
  128. /// 缩略图对象
  129. /// </summary>
  130. public class ThumbnailItem
  131. {
  132. /// <summary>
  133. /// 图像宽度
  134. /// </summary>
  135. public int ImageWidth { get; set; }
  136. /// <summary>
  137. /// 图像高度
  138. /// </summary>
  139. public int ImageHeight { get; set; }
  140. /// <summary>
  141. ///缩略图宽度
  142. /// </summary>
  143. public int ThumbnailWidth { get; set; }
  144. /// <summary>
  145. /// 缩略图高度
  146. /// </summary>
  147. public int ThumbnailHeight { get; set; }
  148. /// <summary>
  149. /// 展示页码
  150. /// </summary>
  151. public string ShowPageText
  152. {
  153. get
  154. {
  155. if (PageIndex >= 0)
  156. {
  157. return (PageIndex + 1).ToString();
  158. }
  159. return string.Empty;
  160. }
  161. }
  162. /// <summary>
  163. /// 页面索引(以0开始)
  164. /// </summary>
  165. public int PageIndex { get; set; } = -1;
  166. /// <summary>
  167. /// 缩略图像内容
  168. /// </summary>
  169. public Image ImageData { get; set; } = new Image();
  170. }
  171. }