|
@@ -230,6 +230,55 @@ namespace MeterVision.Dlg
|
|
|
ThisApp.ExportAiLog(logPath);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private TextPointer lastSearchPosition = null;
|
|
|
+
|
|
|
+ private void SearchButton_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ string searchText = txtSearchBox.Text;
|
|
|
+ if (string.IsNullOrEmpty(searchText))
|
|
|
+ return;
|
|
|
+
|
|
|
+ TextRange documentRange = new TextRange(RichTxtContentBox.Document.ContentStart, RichTxtContentBox.Document.ContentEnd);
|
|
|
+ documentRange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Transparent); // 清除旧高亮
|
|
|
+
|
|
|
+ TextPointer searchStart = lastSearchPosition ?? RichTxtContentBox.Document.ContentStart;
|
|
|
+
|
|
|
+ TextRange foundRange = FindTextInRichTextBox(searchStart, searchText);
|
|
|
+
|
|
|
+ if (foundRange != null)
|
|
|
+ {
|
|
|
+ foundRange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Yellow);
|
|
|
+ RichTxtContentBox.Selection.Select(foundRange.Start, foundRange.End);
|
|
|
+ RichTxtContentBox.Focus();
|
|
|
+ lastSearchPosition = foundRange.End;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ MessageBox.Show("未找到更多内容", "搜索", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ lastSearchPosition = null; // 重置
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private TextRange FindTextInRichTextBox(TextPointer start, string searchText)
|
|
|
+ {
|
|
|
+ while (start != null && start.CompareTo(RichTxtContentBox.Document.ContentEnd) < 0)
|
|
|
+ {
|
|
|
+ if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
|
|
|
+ {
|
|
|
+ string textRun = start.GetTextInRun(LogicalDirection.Forward);
|
|
|
+ int index = textRun.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
|
|
|
+ if (index >= 0)
|
|
|
+ {
|
|
|
+ TextPointer startPos = start.GetPositionAtOffset(index);
|
|
|
+ TextPointer endPos = startPos?.GetPositionAtOffset(searchText.Length);
|
|
|
+ return new TextRange(startPos, endPos);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ start = start.GetNextContextPosition(LogicalDirection.Forward);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
//--------------------------------
|
|
|
}
|
|
|
}
|