CPDFScalingControl.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Compdfkit_Tools.Helper;
  2. using ComPDFKitViewer;
  3. using System;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. namespace Compdfkit_Tools.PDFControl
  8. {
  9. public partial class CPDFScalingControl : UserControl
  10. {
  11. public PDFViewControl ViewControl;
  12. public CPDFScalingControl()
  13. {
  14. InitializeComponent();
  15. }
  16. public void InitWithPDFViewer(PDFViewControl viewControl)
  17. {
  18. ViewControl = viewControl;
  19. }
  20. private void PDFControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  21. {
  22. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  23. {
  24. CPDFViewer pdfViewer = ViewControl.GetCPDFViewer();
  25. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  26. }
  27. }
  28. private void PDFScalingControl_Loaded(object sender, RoutedEventArgs e)
  29. {
  30. CPDFScalingUI.SetScaleEvent -= PDFScalingControl_SetScaleEvent;
  31. CPDFScalingUI.ScaleIncreaseEvent -= PDFScalingControl_ScaleIncreaseEvent;
  32. CPDFScalingUI.ScaleDecreaseEvent -= PDFScalingControl_ScaleDecreaseEvent;
  33. CPDFScalingUI.SetPresetScaleEvent -= CPDFScalingUI_SetPresetScaleEvent;
  34. ViewControl.MouseWheelZoomHandler -= PDFControl_MouseWheelZoomHandler;
  35. ViewControl.FocusPDFViewToolChanged -= ViewControl_FocusPDFViewToolChanged;
  36. CPDFScalingUI.SetScaleEvent += PDFScalingControl_SetScaleEvent;
  37. CPDFScalingUI.ScaleIncreaseEvent += PDFScalingControl_ScaleIncreaseEvent;
  38. CPDFScalingUI.ScaleDecreaseEvent += PDFScalingControl_ScaleDecreaseEvent;
  39. CPDFScalingUI.SetPresetScaleEvent += CPDFScalingUI_SetPresetScaleEvent;
  40. ViewControl.MouseWheelZoomHandler += PDFControl_MouseWheelZoomHandler;
  41. ViewControl.FocusPDFViewToolChanged += ViewControl_FocusPDFViewToolChanged;
  42. }
  43. private void ViewControl_FocusPDFViewToolChanged(object sender, EventArgs e)
  44. {
  45. CPDFViewer pdfViewer = ViewControl.GetCPDFViewer();
  46. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  47. }
  48. private void CPDFScalingUI_Unloaded(object sender, RoutedEventArgs e)
  49. {
  50. CPDFScalingUI.SetScaleEvent -= PDFScalingControl_SetScaleEvent;
  51. CPDFScalingUI.ScaleIncreaseEvent -= PDFScalingControl_ScaleIncreaseEvent;
  52. CPDFScalingUI.ScaleDecreaseEvent -= PDFScalingControl_ScaleDecreaseEvent;
  53. CPDFScalingUI.SetPresetScaleEvent -= CPDFScalingUI_SetPresetScaleEvent;
  54. ViewControl.MouseWheelZoomHandler -= PDFControl_MouseWheelZoomHandler;
  55. }
  56. private void PDFScalingControl_ScaleDecreaseEvent(object sender, EventArgs e)
  57. {
  58. if (ViewControl == null || ViewControl.PDFViewTool == null)
  59. {
  60. return;
  61. }
  62. CPDFViewer pdfViewer = ViewControl.GetCPDFViewer();
  63. if (pdfViewer == null)
  64. {
  65. return;
  66. }
  67. if (pdfViewer.GetZoom() < 3)
  68. {
  69. double newZoom = Math.Max(0.01, pdfViewer.GetZoom() - 0.1);
  70. pdfViewer.SetZoom(newZoom);
  71. pdfViewer.UpDateRenderFrame();
  72. }
  73. else if (pdfViewer.GetZoom() < 6)
  74. {
  75. pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.2);
  76. pdfViewer.UpDateRenderFrame();
  77. }
  78. else if (pdfViewer.GetZoom() >6)
  79. {
  80. pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.3);
  81. pdfViewer.UpDateRenderFrame();
  82. }
  83. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  84. }
  85. private void PDFScalingControl_ScaleIncreaseEvent(object sender, EventArgs e)
  86. {
  87. if (ViewControl == null || ViewControl.PDFViewTool == null)
  88. {
  89. return;
  90. }
  91. CPDFViewer pdfViewer = ViewControl.GetCPDFViewer();
  92. if (pdfViewer == null)
  93. {
  94. return;
  95. }
  96. if (pdfViewer.GetZoom() < 3)
  97. {
  98. pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.1);
  99. pdfViewer.UpDateRenderFrame();
  100. }
  101. else if (pdfViewer.GetZoom() < 6)
  102. {
  103. pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.2);
  104. pdfViewer.UpDateRenderFrame();
  105. }
  106. else if (pdfViewer.GetZoom() <= 10)
  107. {
  108. double newZoom = Math.Max(10, pdfViewer.GetZoom() + 0.3);
  109. pdfViewer.SetZoom(newZoom);
  110. pdfViewer.UpDateRenderFrame();
  111. }
  112. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom()* 100)));
  113. }
  114. private void PDFScalingControl_SetScaleEvent(object sender, string e)
  115. {
  116. if (ViewControl == null || ViewControl.PDFViewTool == null)
  117. {
  118. return;
  119. }
  120. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  121. if (pdfViewer == null)
  122. {
  123. return;
  124. }
  125. if (!string.IsNullOrEmpty(e))
  126. {
  127. pdfViewer.SetZoom(double.Parse(e) / 100);
  128. pdfViewer.UpDateRenderFrame();
  129. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  130. }
  131. }
  132. private void CPDFScalingUI_SetPresetScaleEvent(object sender, string e)
  133. {
  134. if (ViewControl == null || ViewControl.PDFViewTool == null)
  135. {
  136. return;
  137. }
  138. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  139. if (pdfViewer == null)
  140. {
  141. return;
  142. }
  143. if (e == LanguageHelper.CommonManager.GetString("Zoom_Real"))
  144. {
  145. pdfViewer.SetFitMode(FitMode.FitOriginal);
  146. }
  147. else if (e == LanguageHelper.CommonManager.GetString("Zoom_FitWidth"))
  148. {
  149. pdfViewer.SetFitMode(FitMode.FitWidth);
  150. }
  151. else if (e == LanguageHelper.CommonManager.GetString("Zoom_FitPage"))
  152. {
  153. pdfViewer.SetFitMode(FitMode.FitHeight);
  154. }
  155. else
  156. {
  157. pdfViewer.SetZoom(double.Parse(e) / 100);
  158. }
  159. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  160. pdfViewer.UpDateRenderFrame();
  161. }
  162. public void SetZoomTextBoxText(string value)
  163. {
  164. CPDFScalingUI.SetZoomTextBoxText(value);
  165. }
  166. private void CPDFPageScalingControl_LostFocus(object sender, RoutedEventArgs e)
  167. {
  168. if (ViewControl == null || ViewControl.PDFViewTool == null)
  169. {
  170. return;
  171. }
  172. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  173. if (pdfViewer == null)
  174. {
  175. return;
  176. }
  177. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  178. }
  179. }
  180. }