SearchResultControl.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  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. namespace compdfkit_tools
  18. {
  19. /// <summary>
  20. /// 搜索结果控件
  21. /// </summary>
  22. public partial class SearchResultControl : UserControl
  23. {
  24. /// <summary>
  25. /// 搜索结果列表点击选中事件
  26. /// </summary>
  27. public event EventHandler<int> SelectionChanged;
  28. public SearchResultControl()
  29. {
  30. InitializeComponent();
  31. }
  32. private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  33. {
  34. InvokeSelectionChangedEvent(ResultListControl.SelectedIndex);
  35. }
  36. /// <summary>
  37. /// 绑定搜索结果集合
  38. /// </summary>
  39. /// <param name="results"></param>
  40. public void SetSearchResult(List<BindSearchResult> results)
  41. {
  42. if(results == null || results.Count==0)
  43. {
  44. ResultListControl.ItemsSource = null;
  45. return;
  46. }
  47. List< TextBindData> bindSource=new List<TextBindData> ();
  48. foreach(BindSearchResult item in results)
  49. {
  50. bindSource.Add(new TextBindData()
  51. {
  52. BindProperty = item
  53. });
  54. }
  55. ResultListControl.ItemsSource= bindSource;
  56. }
  57. /// <summary>
  58. /// 获取选中搜索结果
  59. /// </summary>
  60. /// <returns>搜索结果</returns>
  61. public BindSearchResult GetSelectItem()
  62. {
  63. TextBindData bindData= ResultListControl.SelectedItem as TextBindData;
  64. if(bindData != null)
  65. {
  66. return bindData.BindProperty;
  67. }
  68. return null;
  69. }
  70. /// <summary>
  71. /// 清除选中结果
  72. /// </summary>
  73. public void ClearSelection()
  74. {
  75. int oldSelectionIndex=ResultListControl.SelectedIndex;
  76. ResultListControl.UnselectAll();
  77. if(oldSelectionIndex != ResultListControl.SelectedIndex)
  78. {
  79. InvokeSelectionChangedEvent(ResultListControl.SelectedIndex);
  80. }
  81. }
  82. /// <summary>
  83. /// 设置选中结果
  84. /// </summary>
  85. /// <param name="selectIndex">选中索引</param>
  86. public void SelectItem(int selectIndex)
  87. {
  88. if(ResultListControl.SelectedIndex != selectIndex)
  89. {
  90. ResultListControl.SelectedIndex = selectIndex;
  91. }
  92. }
  93. internal void InvokeSelectionChangedEvent(int selectionIndex)
  94. {
  95. SelectionChanged?.Invoke(this, selectionIndex);
  96. }
  97. }
  98. public class BindSearchResult
  99. {
  100. /// <summary>
  101. /// 页面索引
  102. /// </summary>
  103. public int PageIndex { get; set; }
  104. /// <summary>
  105. /// 搜索文字结果
  106. /// </summary>
  107. public string TextContent { get; set; }
  108. /// <summary>
  109. /// 搜索高亮颜色
  110. /// </summary>
  111. public Color HighLightColor { get; set; }
  112. /// <summary>
  113. /// 搜索关键字
  114. /// </summary>
  115. public string SearchWord { get; set; }
  116. /// <summary>
  117. /// 搜索结果范围区域
  118. /// </summary>
  119. public Rect TextRect { get; set; }
  120. /// <summary>
  121. /// 页面旋转角度
  122. /// </summary>
  123. public int PageRotate { get; set; }
  124. }
  125. internal class TextBindData
  126. {
  127. public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
  128. public BindSearchResult BindProperty { get; set; }
  129. public TextBindData()
  130. {
  131. BindProperty = new BindSearchResult();
  132. }
  133. }
  134. internal class SearchResultBindHelper : DependencyObject
  135. {
  136. public static FlowDocument GetDocumentBind(DependencyObject obj)
  137. {
  138. return (FlowDocument)obj.GetValue(DocumentBindProperty);
  139. }
  140. public static void SetDocumentBind(DependencyObject obj, FlowDocument value)
  141. {
  142. obj.SetValue(DocumentBindProperty, value);
  143. }
  144. public static readonly DependencyProperty DocumentBindProperty =
  145. DependencyProperty.RegisterAttached("DocumentBind",
  146. typeof(TextBindData),
  147. typeof(SearchResultBindHelper),
  148. new FrameworkPropertyMetadata
  149. {
  150. BindsTwoWayByDefault = true,
  151. PropertyChangedCallback = (obj, e) =>
  152. {
  153. RichTextBox richTextBox = obj as RichTextBox;
  154. BindSearchResult bindItem = e.NewValue as BindSearchResult;
  155. if (richTextBox != null && bindItem != null)
  156. {
  157. richTextBox.Document = GetFlowDocument(bindItem.TextContent.Trim(), bindItem.SearchWord, bindItem.HighLightColor);
  158. }
  159. }
  160. });
  161. /// <summary>
  162. /// 获取文档流数据
  163. /// </summary>
  164. /// <param name="content">搜索文字结果</param>
  165. /// <param name="keyword">搜索关键字</param>
  166. /// <param name="textColor">高亮文字颜色</param>
  167. /// <returns>文档流数据</returns>
  168. public static FlowDocument GetFlowDocument(string content, string keyword, Color textColor)
  169. {
  170. FlowDocument Document = new FlowDocument();
  171. Paragraph textPara = new Paragraph();
  172. Document.Blocks.Add(textPara);
  173. List<int> indexList = new List<int>();
  174. content = Regex.Replace(content, "[\r\n]", " ");
  175. if (keyword.Length > 0)
  176. {
  177. for (int i = 0; i < content.Length && i >= 0;)
  178. {
  179. i = content.IndexOf(keyword, i, StringComparison.OrdinalIgnoreCase);
  180. if (i == -1)
  181. {
  182. break;
  183. }
  184. if (indexList.Contains(i) == false)
  185. {
  186. indexList.Add(i);
  187. }
  188. i += keyword.Length;
  189. }
  190. }
  191. List<string> splitList = new List<string>();
  192. int lastIndex = -1;
  193. foreach (int index in indexList)
  194. {
  195. string prevStr = string.Empty;
  196. if (lastIndex == -1)
  197. {
  198. prevStr = content.Substring(0, index);
  199. }
  200. else
  201. {
  202. prevStr = content.Substring(lastIndex + keyword.Length, index - lastIndex - 1);
  203. }
  204. if (prevStr != string.Empty)
  205. {
  206. splitList.Add(prevStr);
  207. }
  208. splitList.Add(content.Substring(index, keyword.Length));
  209. lastIndex = index;
  210. }
  211. if (indexList.Count > 0)
  212. {
  213. lastIndex = indexList[indexList.Count - 1];
  214. if (content.Length > lastIndex + keyword.Length)
  215. {
  216. splitList.Add(content.Substring(lastIndex + keyword.Length));
  217. }
  218. }
  219. TextBlock addBlock = new TextBlock();
  220. foreach (string textappend in splitList)
  221. {
  222. Run textRun = new Run(textappend);
  223. if (textappend.Equals(keyword, StringComparison.OrdinalIgnoreCase))
  224. {
  225. textRun.Background = new SolidColorBrush(textColor);
  226. }
  227. addBlock.Inlines.Add(textRun);
  228. }
  229. addBlock.TextTrimming = TextTrimming.CharacterEllipsis;
  230. textPara.Inlines.Add(addBlock);
  231. return Document;
  232. }
  233. }
  234. }