ChatGPTAITranslationContent.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using ComPDFKitViewer;
  2. using PDF_Master.Helper;
  3. using System.Data;
  4. using System.Windows;
  5. using System;
  6. using System.Windows.Controls;
  7. using System.Windows.Interop;
  8. using PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  9. using PDF_Master.ViewModels.HomePanel.ChatGPTAI;
  10. using Microsoft.Office.Core;
  11. using PDF_Master.ViewModels;
  12. using System.IO;
  13. using Point = System.Windows.Point;
  14. using Window = System.Windows.Window;
  15. using System.Threading.Tasks;
  16. namespace PDF_Master.Views.HomePanel.ChatGPTAI
  17. {
  18. /// <summary>
  19. /// Interaction logic for ChatGPTAITranslationContent
  20. /// </summary>
  21. public partial class ChatGPTAITranslationContent : UserControl
  22. {
  23. private ChatGPTAITranslationContentViewModel viewModel;
  24. private bool isFirst=false;
  25. private IDropTargetHelper dropHelper;
  26. public ChatGPTAITranslationContent()
  27. {
  28. InitializeComponent();
  29. viewModel = this.DataContext as ChatGPTAITranslationContentViewModel;
  30. }
  31. private async void Grid_Drop(object sender, DragEventArgs e)
  32. {
  33. try
  34. {
  35. Point iconPoint = e.GetPosition(this);
  36. dropHelper?.Drop((System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  37. }
  38. catch (Exception ex)
  39. {
  40. }
  41. string dropFile = "Drop";
  42. if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
  43. {
  44. //BtnBlank.IsEnabled = true;
  45. int count = ((System.Array)e.Data.GetData(System.Windows.DataFormats.FileDrop)).Length;
  46. for (int i = 0; i < count; i++)
  47. {
  48. dropFile = ((System.Array)e.Data.GetData(System.Windows.DataFormats.FileDrop)).GetValue(i).ToString();
  49. if (dropFile.ToLower().EndsWith("pdf")|| dropFile.ToLower().EndsWith(".doc") || dropFile.ToLower().EndsWith(".docx")|| dropFile.ToLower().EndsWith(".docm")|| dropFile.ToLower().EndsWith(".dot")|| dropFile.ToLower().EndsWith(".dotx") || dropFile.ToLower().EndsWith(".dotm"))
  50. {
  51. if (!isFirst)
  52. {
  53. //添加付费拦截锁
  54. if (!ServiceHelper.IAPBeforeFunction())
  55. {
  56. return;
  57. }
  58. isFirst = true;
  59. viewModel.ErrorTipVisible = Visibility.Visible;
  60. FileInfo fileInfo = new FileInfo(dropFile);
  61. if ((((float)fileInfo.Length) / 1024 / 1024) > 10)
  62. {
  63. viewModel.ErrorTipText = "The uploaded file cannot exceed 10MB";
  64. viewModel.ErrorTipVisible = Visibility.Visible;
  65. isFirst = false;
  66. return;
  67. }
  68. string newfile = "";
  69. await Task.Run(async delegate
  70. {
  71. newfile = await ChatGTPAIHelper.fileTranslate(dropFile, viewModel.fromlanguage, viewModel.tolanguage);
  72. });
  73. if (!string.IsNullOrEmpty(newfile))
  74. {
  75. if (File.Exists(newfile))
  76. {
  77. if (viewModel.homeContentViewModel != null)
  78. {
  79. viewModel.homeContentViewModel.OpenFile(new string[] { newfile });
  80. isFirst = false;
  81. return;
  82. }
  83. }
  84. }
  85. isFirst = false;
  86. viewModel.ErrorTipText = newfile;
  87. viewModel.ErrorTipVisible = Visibility.Visible;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. private void MainPage_DragEnter(object sender, DragEventArgs e)
  94. {
  95. DropColorBorder.Visibility = Visibility.Visible;
  96. //BtnBlank.IsEnabled = false;
  97. //页面编辑和缩略图
  98. if ((e.OriginalSource as Image) != null)
  99. return;
  100. try
  101. {
  102. if (dropHelper == null)
  103. {
  104. dropHelper = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("4657278A-411B-11D2-839A-00C04FD918D0"))) as IDropTargetHelper;
  105. }
  106. Point iconPoint = e.GetPosition(this);
  107. dropHelper?.DragEnter(new WindowInteropHelper(Window.GetWindow(this)).Handle, (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  108. }
  109. catch (Exception ex)
  110. {
  111. }
  112. }
  113. private void MainPage_DragOver(object sender, DragEventArgs e)
  114. {
  115. if ((e.OriginalSource as Image) != null)
  116. return;
  117. try
  118. {
  119. Point iconPoint = e.GetPosition(this);
  120. dropHelper?.DragOver(ref iconPoint, e.Effects);
  121. }
  122. catch (Exception ex)
  123. {
  124. }
  125. }
  126. private void MainPage_DragLeave(object sender, DragEventArgs e)
  127. {
  128. DropColorBorder.Visibility = Visibility.Collapsed;
  129. // BtnBlank.IsEnabled = true;
  130. if ((e.OriginalSource as Image) != null)
  131. return;
  132. try
  133. {
  134. dropHelper?.DragLeave();
  135. }
  136. catch (Exception ex)
  137. {
  138. }
  139. }
  140. }
  141. }