ChatGPTAITranslationContent.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. isFirst = true;
  54. FileInfo fileInfo = new FileInfo(dropFile);
  55. if ((((float)fileInfo.Length) / 1024 / 1024) > 30)
  56. {
  57. viewModel.ErrorTipVisible = Visibility.Visible;
  58. isFirst = false;
  59. return;
  60. }
  61. string newfile = "";
  62. await Task.Run(async delegate
  63. {
  64. newfile = await ChatGTPAIHelper.fileTranslate(dropFile, viewModel.fromlanguage, viewModel.tolanguage);
  65. });
  66. if (!string.IsNullOrEmpty(newfile))
  67. {
  68. if (File.Exists(newfile))
  69. {
  70. if (viewModel.homeContentViewModel != null)
  71. {
  72. viewModel.homeContentViewModel.OpenFile(new string[] { newfile });
  73. isFirst = false;
  74. return;
  75. }
  76. }
  77. }
  78. isFirst = false;
  79. viewModel.ErrorTipVisible = Visibility.Visible;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. private void MainPage_DragEnter(object sender, DragEventArgs e)
  86. {
  87. //BtnBlank.IsEnabled = false;
  88. //页面编辑和缩略图
  89. if ((e.OriginalSource as Image) != null)
  90. return;
  91. try
  92. {
  93. if (dropHelper == null)
  94. {
  95. dropHelper = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("4657278A-411B-11D2-839A-00C04FD918D0"))) as IDropTargetHelper;
  96. }
  97. Point iconPoint = e.GetPosition(this);
  98. dropHelper?.DragEnter(new WindowInteropHelper(Window.GetWindow(this)).Handle, (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  99. }
  100. catch (Exception ex)
  101. {
  102. }
  103. }
  104. private void MainPage_DragOver(object sender, DragEventArgs e)
  105. {
  106. if ((e.OriginalSource as Image) != null)
  107. return;
  108. try
  109. {
  110. Point iconPoint = e.GetPosition(this);
  111. dropHelper?.DragOver(ref iconPoint, e.Effects);
  112. }
  113. catch (Exception ex)
  114. {
  115. }
  116. }
  117. private void MainPage_DragLeave(object sender, DragEventArgs e)
  118. {
  119. // BtnBlank.IsEnabled = true;
  120. if ((e.OriginalSource as Image) != null)
  121. return;
  122. try
  123. {
  124. dropHelper?.DragLeave();
  125. }
  126. catch (Exception ex)
  127. {
  128. }
  129. }
  130. }
  131. }