CompressDialog.xaml.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. using ComPDFKit.Controls.Common;
  2. using ComPDFKit.Controls.Helper;
  3. using ComPDFKit.PDFDocument;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.ComponentModel;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Text.RegularExpressions;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Controls.Primitives;
  14. using System.Windows.Forms;
  15. using System.Windows.Input;
  16. using System.Windows.Interop;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using DragEventArgs = System.Windows.DragEventArgs;
  20. using Microsoft.WindowsAPICodePack.Dialogs;
  21. using System.Threading.Tasks;
  22. using System.Globalization;
  23. using System.Windows.Data;
  24. namespace ComPDFKit.Controls.Compress
  25. {
  26. public partial class CompressDialog : Window
  27. {
  28. private CPDFDocument tempDocument;
  29. private List<string> pathlist = new List<string>();
  30. private float quality = 45;
  31. private int compressindex = -1;
  32. private IntPtr compressingIntpr = IntPtr.Zero;
  33. private string compressingfilepath = "";
  34. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  35. private delegate void RefreshPageIndex(int pageIndex);
  36. private bool stopClose = false;
  37. private bool isCanceled = false;
  38. public ObservableCollection<CompressDataItem> CompressDatas { get; set; }
  39. public CompressDialog()
  40. {
  41. InitializeComponent();
  42. SetLangText();
  43. rbtnMedium.IsChecked = true;
  44. CompressDatas = new ObservableCollection<CompressDataItem>();
  45. CompressListView.ItemsSource = CompressDatas;
  46. }
  47. private BitmapSource GetImagePath(string filePath, out IntPtr bitmapHandle)
  48. {
  49. try
  50. {
  51. Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(filePath);
  52. Bitmap bitmap = ico.ToBitmap();
  53. bitmapHandle = bitmap.GetHbitmap();
  54. BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmapHandle, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
  55. return bitmapSource;
  56. }
  57. catch
  58. {
  59. bitmapHandle = IntPtr.Zero;
  60. return null;
  61. }
  62. }
  63. private void SetLangText()
  64. {
  65. this.Title = LanguageHelper.CompressManager.GetString("CompressStr");
  66. btnAddFile.Content = LanguageHelper.CompressManager.GetString("Main_AddFile");
  67. btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_RemoveAll");
  68. btnMoveUp.Content = LanguageHelper.CompressManager.GetString("Merge_MoveUp");
  69. btnMoveDown.Content = LanguageHelper.CompressManager.GetString("Merge_MoveDown");
  70. btnCompress.Content = LanguageHelper.CompressManager.GetString("CompressStr");
  71. btnCancel.Content = LanguageHelper.CompressManager.GetString("Main_Cancel");
  72. groupBox1Text.Text = LanguageHelper.CompressManager.GetString("Compress_OptimizationQuality");
  73. rbtnLow.Content = LanguageHelper.CompressManager.GetString("Compress_Low");
  74. rbtnMedium.Content = LanguageHelper.CompressManager.GetString("Compress_Medium");
  75. rbtnMedium.IsChecked = true;
  76. rbtnHigh.Content = LanguageHelper.CompressManager.GetString("Compress_High");
  77. rbtnCustom.Content = LanguageHelper.CompressManager.GetString("Compress_Custom");
  78. PathHeader.Header = LanguageHelper.CompressManager.GetString("FileInfo_Location");
  79. ProgressHeader.Header = LanguageHelper.CompressManager.GetString("Convert_Progress");
  80. FileName.Header = LanguageHelper.CompressManager.GetString("Main_FileName");
  81. SizeHeader.Header = LanguageHelper.CompressManager.GetString("FileInfo_Size");
  82. lbTotalFiles.Content = string.Format(LanguageHelper.CompressManager.GetString("Merge_TotalPage"), 0);
  83. }
  84. private void btnAddFile_Click(object sender, RoutedEventArgs e)
  85. {
  86. OpenFileDialog file = new OpenFileDialog();
  87. file.Multiselect = true;
  88. file.Filter = "PDF Files (*.pdf)|*.pdf";
  89. if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  90. {
  91. var files = file.FileNames;
  92. for (int i = 0; i < files.Length; i++)
  93. {
  94. AddFiletoList(files[i]);
  95. }
  96. UpdateMoveButtonState();
  97. }
  98. }
  99. private void AddFiletoList(string filePath)
  100. {
  101. string password = "";
  102. if (pathlist.Contains(filePath) || Path.GetExtension(filePath).ToLower() != ".pdf")
  103. {
  104. return;
  105. }
  106. CPDFDocument doc = CPDFDocument.InitWithFilePath(filePath);
  107. if (doc == null)
  108. {
  109. MessageBoxEx.Show(LanguageHelper.CompressManager.GetString("Main_OpenFileFailedWarning"));
  110. return;
  111. }
  112. if (doc.IsLocked)
  113. {
  114. PasswordWindow passwordWindow = new PasswordWindow();
  115. passwordWindow.InitDocument(doc);
  116. passwordWindow.Owner = Window.GetWindow(this);
  117. passwordWindow.PasswordDialog.SetShowText(filePath + " is encrypted.");
  118. passwordWindow.ShowDialog();
  119. if (doc.IsLocked)
  120. {
  121. doc.Release();
  122. return;
  123. }
  124. password = passwordWindow.Password;
  125. }
  126. pathlist.Add(filePath);
  127. BitmapSource bitmapSource = GetImagePath(doc.FilePath, out IntPtr bitmapHandle);
  128. CompressDataItem newdata = new CompressDataItem()
  129. {
  130. Name = doc.FileName,
  131. Size = CommonHelper.GetFileSize(filePath),
  132. Progress = "0/" + doc.PageCount,
  133. Path = doc.FilePath,
  134. PageCount = doc.PageCount,
  135. ImagePath = bitmapSource
  136. };
  137. AddListViewItem(newdata, password);
  138. doc.Release();
  139. UpdateTotalCount();
  140. }
  141. private void UpdateTotalCount()
  142. {
  143. lbTotalFiles.Content = string.Format(LanguageHelper.CompressManager.GetString("Merge_TotalPage"), CompressListView.Items.Count);
  144. if (CompressListView.Items.Count == 0)
  145. btnCompress.IsEnabled = false;
  146. else
  147. btnCompress.IsEnabled = true;
  148. }
  149. private void AddListViewItem(CompressDataItem data, string password)
  150. {
  151. if (!string.IsNullOrEmpty(password))
  152. data.PassWord = password;
  153. CompressDatas.Add(data);
  154. UpdateTotalCount();
  155. }
  156. private void btnRemove_Click(object sender, RoutedEventArgs e)
  157. {
  158. if (CompressListView.SelectedItems.Count > 0)
  159. {
  160. List<int> pages = new List<int>();
  161. foreach (var selectedItem in CompressListView.SelectedItems)
  162. {
  163. var imageItem = (CompressDataItem)selectedItem;
  164. int index = CompressListView.Items.IndexOf(imageItem);
  165. pages.Add(index);
  166. }
  167. pages.Sort();
  168. for (int i = pages.Count - 1; i >= 0; i--)
  169. {
  170. int index = pages[i];
  171. string path = ((CompressDataItem)CompressListView.Items[index]).Path;
  172. pathlist.Remove(path);
  173. CompressDatas.RemoveAt(index);
  174. }
  175. }
  176. else
  177. {
  178. CompressDatas.Clear();
  179. pathlist.Clear();
  180. }
  181. UpdateTotalCount();
  182. }
  183. private void btnChoosePage_Click(object sender, RoutedEventArgs e)
  184. {
  185. }
  186. private async void btnCompress_Click(object sender, RoutedEventArgs e)
  187. {
  188. if ((bool)rbtnLow.IsChecked)
  189. quality = 10;
  190. else if ((bool)rbtnMedium.IsChecked)
  191. quality = 40;
  192. else if ((bool)rbtnHigh.IsChecked)
  193. quality = 80;
  194. else
  195. {
  196. int q = 0;
  197. bool r = int.TryParse(txtQuality.Text, out q);
  198. if (!r || q > 100 || q < 0)
  199. {
  200. MessageBoxEx.Show(LanguageHelper.CompressManager.GetString("Compress_NumberErrorWarning"));
  201. txtQuality.Focus();
  202. return;
  203. }
  204. quality = q;
  205. }
  206. CommonOpenFileDialog commonFileDialog = new CommonOpenFileDialog(LanguageHelper.CompressManager.GetString("Main_OpenFolderNoteWarning"));
  207. commonFileDialog.IsFolderPicker = true;
  208. if (commonFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
  209. {
  210. if (string.IsNullOrEmpty(commonFileDialog.FileName))
  211. {
  212. MessageBoxEx.Show(LanguageHelper.CompressManager.GetString("Main_NoSelectedFilesWarning"),
  213. LanguageHelper.CompressManager.GetString("Main_HintWarningTitle"),
  214. MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
  215. return;
  216. }
  217. }
  218. else
  219. return;
  220. stopClose = true;
  221. groupBox1.IsEnabled = false;
  222. btnAddFile.IsEnabled = false;
  223. btnCompress.IsEnabled = false;
  224. btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_RemoveAll");
  225. btnRemove.IsEnabled = false;
  226. btnMoveDown.IsEnabled = false;
  227. btnMoveUp.IsEnabled = false;
  228. bool isFailed = false;
  229. string selectedFile = "";
  230. for (int i = 0; i < CompressListView.Items.Count; i++)
  231. {
  232. var item = (CompressDataItem)CompressListView.Items[i];
  233. string path = item.Path;
  234. if (isCanceled)
  235. {
  236. item.Progress = LanguageHelper.CompressManager.GetString("Main_Interrupt");
  237. continue;
  238. }
  239. CPDFDocument doc = CPDFDocument.InitWithFilePath(path);
  240. if (doc.IsLocked && item.PassWord != null && !string.IsNullOrEmpty(item.PassWord))
  241. {
  242. doc.UnlockWithPassword(item.PassWord.ToString());
  243. }
  244. var filename = doc.FileName + " compressed";
  245. var filepath = commonFileDialog.FileName + "\\" + filename + ".pdf";
  246. filepath = CommonHelper.CreateFilePath(filepath);
  247. selectedFile = compressingfilepath = filepath;
  248. indexDelegate += GetIndex;
  249. compressingIntpr = doc.CompressFile_Init(quality, indexDelegate);
  250. GC.KeepAlive(indexDelegate);
  251. compressindex = i;
  252. tempDocument = doc;
  253. var r = await Task.Run<bool>(() => { return doc.CompressFile_Start(compressingIntpr, filepath); });
  254. if (!r)
  255. {
  256. item.Progress = LanguageHelper.CompressManager.GetString("Main_FailedState");
  257. doc.Release();
  258. if (File.Exists(filepath))
  259. File.Delete(filepath);
  260. isFailed = true;
  261. continue;
  262. }
  263. compressingfilepath = "";
  264. doc.Release();
  265. }
  266. int itemCount = CompressListView.Items.Count;
  267. if (!isFailed)
  268. {
  269. System.Diagnostics.Process.Start("explorer", "/select,\"" + selectedFile + "\"");
  270. }
  271. isCanceled = false;
  272. compressindex = -1;
  273. stopClose = false;
  274. btnAddFile.IsEnabled = true;
  275. btnCompress.IsEnabled = true;
  276. btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_Delete");
  277. btnRemove.IsEnabled = true;
  278. btnMoveDown.IsEnabled = true;
  279. btnMoveUp.IsEnabled = true;
  280. groupBox1.IsEnabled = true;
  281. }
  282. private int GetIndex(int pageindex)
  283. {
  284. try
  285. {
  286. if (Dispatcher.CheckAccess() == false)
  287. {
  288. return Dispatcher.Invoke(new CPDFDocument.GetPageIndexDelegate(delegate (int s)
  289. {
  290. if (CompressListView.Items.Count >= compressindex)
  291. {
  292. if (CompressListView.Items[compressindex] is CompressDataItem dataItem)
  293. {
  294. dataItem.Progress = pageindex + "/" + dataItem.PageCount;
  295. if (pageindex == dataItem.PageCount - 1)
  296. {
  297. dataItem.Progress = LanguageHelper.CompressManager.GetString("Main_CompletedState");
  298. }
  299. }
  300. }
  301. return 0;
  302. }), pageindex).ToString().Length;
  303. }
  304. else
  305. {
  306. if (CompressListView.Items.Count >= compressindex)
  307. {
  308. if (CompressListView.Items[compressindex] is CompressDataItem dataItem)
  309. {
  310. dataItem.Progress = pageindex + "/" + dataItem.PageCount;
  311. if (pageindex == dataItem.PageCount - 1)
  312. {
  313. dataItem.Progress = LanguageHelper.CompressManager.GetString("Main_CompletedState");
  314. }
  315. }
  316. }
  317. return 0;
  318. }
  319. }
  320. catch { return -1; }
  321. }
  322. private void btnCancel_Click(object sender, RoutedEventArgs e)
  323. {
  324. try
  325. {
  326. if (compressindex != -1 && !compressingIntpr.Equals(IntPtr.Zero))
  327. {
  328. if (MessageBoxEx.Show(LanguageHelper.CompressManager.GetString("CompressInterruptWarning"), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes && tempDocument != null)
  329. {
  330. tempDocument.CompressFile_Cancel(compressingIntpr);
  331. isCanceled = true;
  332. if (File.Exists(compressingfilepath))
  333. File.Delete(compressingfilepath);
  334. }
  335. }
  336. else
  337. {
  338. this.Close();
  339. this.DialogResult = false;
  340. }
  341. }
  342. catch { }
  343. }
  344. private void ConverterListView_DragOver(object sender, DragEventArgs e)
  345. {
  346. var files = (Array)e.Data.GetData(System.Windows.DataFormats.FileDrop);
  347. int count = 0;
  348. string pdf = "pdf";
  349. foreach (string file in files)
  350. {
  351. string text = Path.GetExtension(file);
  352. if (text.IndexOf(pdf, StringComparison.OrdinalIgnoreCase) >= 0)
  353. {
  354. count++;
  355. }
  356. }
  357. if (count < 1)
  358. {
  359. e.Effects = System.Windows.DragDropEffects.None;
  360. Mouse.SetCursor(System.Windows.Input.Cursors.No);
  361. }
  362. else
  363. {
  364. e.Effects = System.Windows.DragDropEffects.Copy;
  365. Mouse.SetCursor(System.Windows.Input.Cursors.Arrow);
  366. }
  367. return;
  368. }
  369. private void ConverterListView_Drop(object sender, DragEventArgs e)
  370. {
  371. var files = (Array)e.Data.GetData(System.Windows.DataFormats.FileDrop);
  372. if (files != null && files.Length > 0)
  373. {
  374. var Files = (string[])files;
  375. for (int i = 0; i < Files.Length; i++)
  376. {
  377. AddFiletoList(Files[i]);
  378. }
  379. }
  380. }
  381. private void ConverterListView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  382. {
  383. }
  384. private void ConverterListView_PreviewMouseUp(object sender, MouseButtonEventArgs e)
  385. {
  386. DependencyObject element = (DependencyObject)e.OriginalSource;
  387. if (element is Thumb thumb)
  388. {
  389. GridViewColumnHeader gridViewColumnHeader = CommonHelper.ViewportHelper.FindVisualParent<GridViewColumnHeader>(thumb);
  390. if (gridViewColumnHeader != null && gridViewColumnHeader.Content != null)
  391. {
  392. GridViewColumn column = FindGridViewColumn(gridViewColumnHeader.Content.ToString(), CompressListView);
  393. if (column != null)
  394. {
  395. if (column.Header == LanguageHelper.CompressManager.GetString("Main_FileName") && column.ActualWidth > 240)
  396. {
  397. TextBlock textBlock = FindTextBlockInCellTemplate(column);
  398. if (textBlock != null)
  399. {
  400. textBlock.MaxWidth = textBlock.Width = column.ActualWidth - 20;
  401. TxbName_SizeChanged(textBlock, null);
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }
  408. private TextBlock FindTextBlockInCellTemplate(GridViewColumn column)
  409. {
  410. DataTemplate dataTemplate = column.CellTemplate;
  411. if (dataTemplate != null)
  412. {
  413. FrameworkElement frameworkElement = dataTemplate.LoadContent() as FrameworkElement;
  414. if (frameworkElement != null)
  415. {
  416. return CommonHelper.ViewportHelper.FindVisualChild<TextBlock>(frameworkElement);
  417. }
  418. }
  419. return null;
  420. }
  421. private GridViewColumn FindGridViewColumn(string columnHeader, System.Windows.Controls.ListView listView)
  422. {
  423. GridView gridView = listView.View as GridView;
  424. if (gridView != null)
  425. {
  426. foreach (var column in gridView.Columns)
  427. {
  428. GridViewColumn gvColumn = column as GridViewColumn;
  429. if (gvColumn != null && gvColumn.Header.ToString() == columnHeader)
  430. {
  431. return gvColumn;
  432. }
  433. }
  434. }
  435. return null;
  436. }
  437. private void UpdateMoveButtonState()
  438. {
  439. if (compressindex != -1 && !compressingIntpr.Equals(IntPtr.Zero))
  440. {
  441. return;
  442. }
  443. if (CompressListView.Items.Count > 0 && CompressListView.SelectedItems.Count > 0)
  444. {
  445. btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_Delete"); ;
  446. int count = CompressListView.Items.Count;
  447. if (CompressListView.SelectedItems.Contains(CompressListView.Items[CompressListView.Items.Count - 1]))
  448. {
  449. btnMoveDown.IsEnabled = false;
  450. }
  451. else
  452. btnMoveDown.IsEnabled = true;
  453. if (CompressListView.SelectedItems.Contains(CompressListView.Items[0]))
  454. btnMoveUp.IsEnabled = false;
  455. else
  456. btnMoveUp.IsEnabled = true;
  457. }
  458. else
  459. {
  460. btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_RemoveAll");
  461. btnMoveDown.IsEnabled = false;
  462. btnMoveUp.IsEnabled = false;
  463. }
  464. }
  465. private void ConverterListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  466. {
  467. UpdateMoveButtonState();
  468. }
  469. private void UpdateColumnsWidth(System.Windows.Controls.ListView listView)
  470. {
  471. int autoFillColumnIndex = (listView.View as System.Windows.Controls.GridView).Columns.Count - 1;
  472. if (listView.ActualWidth == Double.NaN)
  473. {
  474. listView.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));
  475. }
  476. double remainingSpace = listView.ActualWidth;
  477. for (int i = 0; i < (listView.View as System.Windows.Controls.GridView).Columns.Count; i++)
  478. {
  479. if (i != autoFillColumnIndex)
  480. {
  481. remainingSpace -= (listView.View as System.Windows.Controls.GridView).Columns[i].ActualWidth;
  482. (listView.View as System.Windows.Controls.GridView).Columns[autoFillColumnIndex].Width = remainingSpace >= 0 ? remainingSpace : 0;
  483. }
  484. }
  485. }
  486. private void ConverterListView_Loaded(object sender, RoutedEventArgs e)
  487. {
  488. UpdateColumnsWidth(sender as System.Windows.Controls.ListView);
  489. }
  490. private void ConverterListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  491. {
  492. var pos = e.GetPosition(CompressListView);
  493. var result = VisualTreeHelper.HitTest(CompressListView, pos);
  494. if (result != null)
  495. {
  496. var listBoxItem = CommonHelper.ViewportHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  497. if (listBoxItem == null)
  498. {
  499. CompressListView.SelectedItems.Clear();
  500. }
  501. }
  502. CompressListView.Focus();
  503. }
  504. private void ConverterListView_SizeChanged(object sender, SizeChangedEventArgs e)
  505. {
  506. UpdateColumnsWidth(sender as System.Windows.Controls.ListView);
  507. }
  508. private void TxbName_SizeChanged(object sender, SizeChangedEventArgs e)
  509. {
  510. }
  511. private void PART_HeaderGripper_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
  512. {
  513. }
  514. private void rbtnAllPage_Checked(object sender, RoutedEventArgs e)
  515. {
  516. }
  517. private void rbtnCurrentPage_Checked(object sender, RoutedEventArgs e)
  518. {
  519. }
  520. private void rbtnOldPageOnly_Checked(object sender, RoutedEventArgs e)
  521. {
  522. }
  523. private void RbtnEvenPageOnly_Checked(object sender, RoutedEventArgs e)
  524. {
  525. }
  526. public class CompressDataItem : INotifyPropertyChanged
  527. {
  528. public event PropertyChangedEventHandler PropertyChanged;
  529. protected virtual void OnPropertyChanged(string propertyName)
  530. {
  531. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  532. }
  533. private string passWord;
  534. public string PassWord { get; set; }
  535. public BitmapSource ImagePath { get; set; }
  536. public string Name { get; set; }
  537. public string Size { get; set; }
  538. public string Path { get; set; }
  539. public int PageCount { get; set; }
  540. private string progress;
  541. public string Progress
  542. {
  543. get { return progress; }
  544. set
  545. {
  546. progress = value;
  547. OnPropertyChanged("Progress");
  548. }
  549. }
  550. }
  551. private void btnMoveDown_Click(object sender, RoutedEventArgs e)
  552. {
  553. try
  554. {
  555. var items = CompressListView.SelectedItems;
  556. if (items.Count > 0)
  557. {
  558. List<int> indexs = new List<int>();
  559. foreach (var selectedItem in CompressListView.SelectedItems)
  560. {
  561. var imageItem = (CompressDataItem)selectedItem;
  562. int index = CompressListView.Items.IndexOf(imageItem);
  563. indexs.Add(index);
  564. }
  565. indexs.Sort();
  566. for (int i = 0; i < indexs.Count; i++)
  567. {
  568. var index = indexs[indexs.Count - 1 - i];
  569. var item = CompressDatas[index];
  570. CompressDatas.RemoveAt(index);
  571. CompressDatas.Insert(index + 1, item);
  572. }
  573. CompressListView.SelectedItems.Clear();
  574. for (int i = 0; i < indexs.Count; i++)
  575. {
  576. CompressListView.SelectedItems.Add(CompressListView.Items[indexs[i] + 1]);
  577. }
  578. }
  579. }
  580. catch (Exception ex)
  581. {
  582. }
  583. }
  584. private void btnMoveUp_Click(object sender, RoutedEventArgs e)
  585. {
  586. try
  587. {
  588. var items = CompressListView.SelectedItems;
  589. if (items.Count > 0)
  590. {
  591. List<int> indexs = new List<int>();
  592. foreach (var selectedItem in CompressListView.SelectedItems)
  593. {
  594. var imageItem = (CompressDataItem)selectedItem;
  595. int index = CompressListView.Items.IndexOf(imageItem);
  596. indexs.Add(index);
  597. }
  598. indexs.Sort();
  599. for (int i = 0; i < indexs.Count; i++)
  600. {
  601. var index = indexs[i];
  602. var item = CompressDatas[index];
  603. CompressDatas.RemoveAt(index);
  604. CompressDatas.Insert(index - 1, item);
  605. }
  606. CompressListView.SelectedItems.Clear();
  607. for (int i = 0; i < indexs.Count; i++)
  608. {
  609. CompressListView.SelectedItems.Add(CompressListView.Items[indexs[i] - 1]);
  610. }
  611. }
  612. }
  613. catch (Exception ex)
  614. {
  615. }
  616. }
  617. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  618. {
  619. Regex regex = new Regex("[^0-9]+");
  620. if (regex.IsMatch(e.Text))
  621. {
  622. e.Handled = true;
  623. }
  624. }
  625. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  626. {
  627. if (sender is System.Windows.Controls.TextBox textBox)
  628. {
  629. if (!string.IsNullOrEmpty(textBox.Text))
  630. {
  631. int value = int.Parse(textBox.Text);
  632. if (value < 1)
  633. {
  634. textBox.Text = "1";
  635. textBox.Select(textBox.Text.Length, 0);
  636. }
  637. else if (value > 100)
  638. {
  639. textBox.Text = "100";
  640. textBox.Select(textBox.Text.Length, 0);
  641. }
  642. }
  643. else
  644. {
  645. textBox.Text = "1";
  646. textBox.Select(textBox.Text.Length, 0);
  647. }
  648. }
  649. }
  650. }
  651. public class MaxWidthToTextTrimmingConverter : IMultiValueConverter
  652. {
  653. public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  654. {
  655. if (values.Length == 2 && values[0] is double maxWidth && values[1] is string text)
  656. {
  657. if (maxWidth >= MeasureTextWidth(text))
  658. {
  659. return TextTrimming.None;
  660. }
  661. else
  662. {
  663. return TextTrimming.CharacterEllipsis;
  664. }
  665. }
  666. return TextTrimming.None;
  667. }
  668. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  669. {
  670. throw new NotSupportedException();
  671. }
  672. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  673. {
  674. throw new NotImplementedException();
  675. }
  676. private double MeasureTextWidth(string text)
  677. {
  678. var formattedText = new FormattedText(
  679. text,
  680. CultureInfo.CurrentCulture,
  681. System.Windows.FlowDirection.LeftToRight,
  682. new Typeface("Arial"),
  683. 12,
  684. System.Windows.Media.Brushes.Black);
  685. return formattedText.WidthIncludingTrailingWhitespace;
  686. }
  687. }
  688. }