MainWindow.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  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.Controls.Primitives;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using viewer_ctrl_demo.Bota;
  19. namespace viewer_ctrl_demo
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. public CPDFViewer pdfViewer;
  27. public MainWindow()
  28. {
  29. InitializeComponent();
  30. AddPreViewModes();
  31. AddMenuItemToLogoButton();
  32. }
  33. public void AddMenuItemToLogoButton()
  34. {
  35. // 创建三个 MenuItem,并设置 Header 属性
  36. MenuItem openFileItem = new MenuItem();
  37. openFileItem.Header = "打开文件";
  38. openFileItem.Click += MenuItem_Click;
  39. openFileItem.Tag = "OpenFile";
  40. MenuItem goToWebItem = new MenuItem();
  41. goToWebItem.Header = "了解ComPDFKit SDK";
  42. goToWebItem.Click += MenuItem_Click;
  43. MenuItem aboutItem = new MenuItem();
  44. aboutItem.Header = "关于";
  45. aboutItem.Click += MenuItem_Click;
  46. // 将 MenuItem 添加到 ContextMenu 中
  47. logoMenu.Items.Add(openFileItem);
  48. logoMenu.Items.Add(goToWebItem);
  49. logoMenu.Items.Add(aboutItem);
  50. }
  51. private void MenuItem_Click(object sender, RoutedEventArgs e)
  52. {
  53. var menuItem = sender as MenuItem;
  54. if (menuItem.Tag.ToString() == "OpenFile")
  55. {
  56. OpenFileDialog openFileDialog = new OpenFileDialog();
  57. openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
  58. if (openFileDialog.ShowDialog() == true)
  59. {
  60. pdfViewer = new CPDFViewer();
  61. string selectedFilePath = openFileDialog.FileName;
  62. pdfViewer.InitDocument(selectedFilePath);
  63. if (pdfViewer.Document!=null&&
  64. pdfViewer.Document.ErrorType == ComPDFKit.PDFDocument.CPDFDocumentError.CPDFDocumentErrorSuccess)
  65. {
  66. pdfViewer.Load();
  67. PDFGrid.Children.Add(pdfViewer);
  68. }
  69. }
  70. }
  71. }
  72. public void AddPreViewModes()
  73. {
  74. // 创建ComboBoxItem元素
  75. ComboBoxItem ViewerMode = new ComboBoxItem();
  76. ViewerMode.Content = "预览模式";
  77. ViewerMode.IsSelected = true; // 将A设置为首选项
  78. // 添加ComboBoxItem元素到ComboBox中
  79. previewModeComboBox.Items.Add(ViewerMode);
  80. }
  81. private void displaySettingsToggleButton_Click(object sender, RoutedEventArgs e)
  82. {
  83. if (popup.IsOpen)
  84. {
  85. popup.IsOpen = false;
  86. }
  87. else
  88. {
  89. popup.IsOpen = true;
  90. }
  91. }
  92. private void logoButton_Click(object sender, RoutedEventArgs e)
  93. {
  94. Button button = sender as Button;
  95. if (button.ContextMenu.IsOpen)
  96. {
  97. button.ContextMenu.IsOpen = false;
  98. }
  99. else
  100. {
  101. button.ContextMenu.IsOpen = true;
  102. }
  103. }
  104. /// <summary>
  105. /// 搜索工具点击处理
  106. /// </summary>
  107. private void SearchToolButton_Click(object sender, RoutedEventArgs e)
  108. {
  109. UIElement botaTool = GetBotaTool();
  110. if (botaTool == null || !(botaTool is PDFSearch))
  111. {
  112. PDFSearch searchControl = new PDFSearch();
  113. SetBotaTool(searchControl);
  114. }
  115. ExpandTool(SearchToolButton.IsChecked == true);
  116. }
  117. /// <summary>
  118. /// 设置Bota工具内容
  119. /// </summary>
  120. /// <param name="newChild"></param>
  121. private void SetBotaTool(UIElement newChild)
  122. {
  123. BotaToolContainer.Child = newChild;
  124. }
  125. /// <summary>
  126. /// 获取Bota工具
  127. /// </summary>
  128. /// <returns></returns>
  129. private UIElement GetBotaTool()
  130. {
  131. return BotaToolContainer.Child;
  132. }
  133. /// <summary>
  134. /// 展开Bota工具
  135. /// </summary>
  136. /// <param name="isExpand"></param>
  137. private void ExpandTool(bool isExpand)
  138. {
  139. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  140. BotaToolContainer.Width = isExpand ? 200 : 0;
  141. }
  142. }
  143. }