123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778 |
- using ComPDFKit.Controls.Common;
- using ComPDFKit.Controls.Helper;
- using ComPDFKit.PDFDocument;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Drawing;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Forms;
- using System.Windows.Input;
- using System.Windows.Interop;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using DragEventArgs = System.Windows.DragEventArgs;
- using Microsoft.WindowsAPICodePack.Dialogs;
- using System.Threading.Tasks;
- using System.Globalization;
- using System.Windows.Data;
- namespace ComPDFKit.Controls.Compress
- {
- public partial class CompressDialog : Window
- {
- private CPDFDocument tempDocument;
- private List<string> pathlist = new List<string>();
- private float quality = 45;
- private int compressindex = -1;
- private IntPtr compressingIntpr = IntPtr.Zero;
- private string compressingfilepath = "";
- private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
- private delegate void RefreshPageIndex(int pageIndex);
- private bool stopClose = false;
- private bool isCanceled = false;
- public ObservableCollection<CompressDataItem> CompressDatas { get; set; }
-
- public CompressDialog()
- {
- InitializeComponent();
- SetLangText();
- rbtnMedium.IsChecked = true;
- CompressDatas = new ObservableCollection<CompressDataItem>();
- CompressListView.ItemsSource = CompressDatas;
- }
- private BitmapSource GetImagePath(string filePath, out IntPtr bitmapHandle)
- {
- try
- {
- Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(filePath);
- Bitmap bitmap = ico.ToBitmap();
- bitmapHandle = bitmap.GetHbitmap();
- BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmapHandle, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
- return bitmapSource;
- }
- catch
- {
- bitmapHandle = IntPtr.Zero;
- return null;
- }
- }
- private void SetLangText()
- {
- this.Title = LanguageHelper.CompressManager.GetString("CompressStr");
- btnAddFile.Content = LanguageHelper.CompressManager.GetString("Main_AddFile");
- btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_RemoveAll");
- btnMoveUp.Content = LanguageHelper.CompressManager.GetString("Merge_MoveUp");
- btnMoveDown.Content = LanguageHelper.CompressManager.GetString("Merge_MoveDown");
- btnCompress.Content = LanguageHelper.CompressManager.GetString("CompressStr");
- btnCancel.Content = LanguageHelper.CompressManager.GetString("Main_Cancel");
- groupBox1Text.Text = LanguageHelper.CompressManager.GetString("Compress_OptimizationQuality");
- rbtnLow.Content = LanguageHelper.CompressManager.GetString("Compress_Low");
- rbtnMedium.Content = LanguageHelper.CompressManager.GetString("Compress_Medium");
- rbtnMedium.IsChecked = true;
- rbtnHigh.Content = LanguageHelper.CompressManager.GetString("Compress_High");
- rbtnCustom.Content = LanguageHelper.CompressManager.GetString("Compress_Custom");
- PathHeader.Header = LanguageHelper.CompressManager.GetString("FileInfo_Location");
- ProgressHeader.Header = LanguageHelper.CompressManager.GetString("Convert_Progress");
- FileName.Header = LanguageHelper.CompressManager.GetString("Main_FileName");
- SizeHeader.Header = LanguageHelper.CompressManager.GetString("FileInfo_Size");
- lbTotalFiles.Content = string.Format(LanguageHelper.CompressManager.GetString("Merge_TotalPage"), 0);
- }
- private void btnAddFile_Click(object sender, RoutedEventArgs e)
- {
- OpenFileDialog file = new OpenFileDialog();
- file.Multiselect = true;
- file.Filter = "PDF Files (*.pdf)|*.pdf";
- if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- var files = file.FileNames;
- for (int i = 0; i < files.Length; i++)
- {
- AddFiletoList(files[i]);
- }
- UpdateMoveButtonState();
- }
- }
- private void AddFiletoList(string filePath)
- {
- string password = "";
- if (pathlist.Contains(filePath) || Path.GetExtension(filePath).ToLower() != ".pdf")
- {
- return;
- }
- CPDFDocument doc = CPDFDocument.InitWithFilePath(filePath);
- if (doc == null)
- {
- MessageBoxEx.Show(LanguageHelper.CompressManager.GetString("Main_OpenFileFailedWarning"));
- return;
- }
- if (doc.IsLocked)
- {
- PasswordWindow passwordWindow = new PasswordWindow();
- passwordWindow.InitDocument(doc);
- passwordWindow.Owner = Window.GetWindow(this);
- passwordWindow.PasswordDialog.SetShowText(filePath + " is encrypted.");
- passwordWindow.ShowDialog();
- if (doc.IsLocked)
- {
- doc.Release();
- return;
- }
- password = passwordWindow.Password;
- }
- pathlist.Add(filePath);
- BitmapSource bitmapSource = GetImagePath(doc.FilePath, out IntPtr bitmapHandle);
- CompressDataItem newdata = new CompressDataItem()
- {
- Name = doc.FileName,
- Size = CommonHelper.GetFileSize(filePath),
- Progress = "0/" + doc.PageCount,
- Path = doc.FilePath,
- PageCount = doc.PageCount,
- ImagePath = bitmapSource
- };
- AddListViewItem(newdata, password);
- doc.Release();
- UpdateTotalCount();
- }
- private void UpdateTotalCount()
- {
- lbTotalFiles.Content = string.Format(LanguageHelper.CompressManager.GetString("Merge_TotalPage"), CompressListView.Items.Count);
- if (CompressListView.Items.Count == 0)
- btnCompress.IsEnabled = false;
- else
- btnCompress.IsEnabled = true;
- }
- private void AddListViewItem(CompressDataItem data, string password)
- {
- if (!string.IsNullOrEmpty(password))
- data.PassWord = password;
- CompressDatas.Add(data);
- UpdateTotalCount();
- }
- private void btnRemove_Click(object sender, RoutedEventArgs e)
- {
- if (CompressListView.SelectedItems.Count > 0)
- {
- List<int> pages = new List<int>();
- foreach (var selectedItem in CompressListView.SelectedItems)
- {
- var imageItem = (CompressDataItem)selectedItem;
- int index = CompressListView.Items.IndexOf(imageItem);
- pages.Add(index);
- }
- pages.Sort();
- for (int i = pages.Count - 1; i >= 0; i--)
- {
- int index = pages[i];
- string path = ((CompressDataItem)CompressListView.Items[index]).Path;
- pathlist.Remove(path);
- CompressDatas.RemoveAt(index);
- }
- }
- else
- {
- CompressDatas.Clear();
- pathlist.Clear();
- }
- UpdateTotalCount();
- }
- private void btnChoosePage_Click(object sender, RoutedEventArgs e)
- {
- }
- private async void btnCompress_Click(object sender, RoutedEventArgs e)
- {
- if ((bool)rbtnLow.IsChecked)
- quality = 10;
- else if ((bool)rbtnMedium.IsChecked)
- quality = 40;
- else if ((bool)rbtnHigh.IsChecked)
- quality = 80;
- else
- {
- int q = 0;
- bool r = int.TryParse(txtQuality.Text, out q);
- if (!r || q > 100 || q < 0)
- {
- MessageBoxEx.Show(LanguageHelper.CompressManager.GetString("Compress_NumberErrorWarning"));
- txtQuality.Focus();
- return;
- }
- quality = q;
- }
- CommonOpenFileDialog commonFileDialog = new CommonOpenFileDialog(LanguageHelper.CompressManager.GetString("Main_OpenFolderNoteWarning"));
- commonFileDialog.IsFolderPicker = true;
- if (commonFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
- {
- if (string.IsNullOrEmpty(commonFileDialog.FileName))
- {
- MessageBoxEx.Show(LanguageHelper.CompressManager.GetString("Main_NoSelectedFilesWarning"),
- LanguageHelper.CompressManager.GetString("Main_HintWarningTitle"),
- MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
- return;
- }
- }
- else
- return;
- stopClose = true;
- groupBox1.IsEnabled = false;
- btnAddFile.IsEnabled = false;
- btnCompress.IsEnabled = false;
- btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_RemoveAll");
- btnRemove.IsEnabled = false;
- btnMoveDown.IsEnabled = false;
- btnMoveUp.IsEnabled = false;
- bool isFailed = false;
- string selectedFile = "";
- for (int i = 0; i < CompressListView.Items.Count; i++)
- {
- var item = (CompressDataItem)CompressListView.Items[i];
- string path = item.Path;
- if (isCanceled)
- {
- item.Progress = LanguageHelper.CompressManager.GetString("Main_Interrupt");
- continue;
- }
- CPDFDocument doc = CPDFDocument.InitWithFilePath(path);
- if (doc.IsLocked && item.PassWord != null && !string.IsNullOrEmpty(item.PassWord))
- {
- doc.UnlockWithPassword(item.PassWord.ToString());
- }
- var filename = doc.FileName + " compressed";
- var filepath = commonFileDialog.FileName + "\\" + filename + ".pdf";
- filepath = CommonHelper.CreateFilePath(filepath);
- selectedFile = compressingfilepath = filepath;
- indexDelegate += GetIndex;
- compressingIntpr = doc.CompressFile_Init(quality, indexDelegate);
- GC.KeepAlive(indexDelegate);
- compressindex = i;
- tempDocument = doc;
- var r = await Task.Run<bool>(() => { return doc.CompressFile_Start(compressingIntpr, filepath); });
- if (!r)
- {
- item.Progress = LanguageHelper.CompressManager.GetString("Main_FailedState");
- doc.Release();
- if (File.Exists(filepath))
- File.Delete(filepath);
- isFailed = true;
- continue;
- }
- compressingfilepath = "";
- doc.Release();
- }
- int itemCount = CompressListView.Items.Count;
- if (!isFailed)
- {
- System.Diagnostics.Process.Start("explorer", "/select,\"" + selectedFile + "\"");
- }
- isCanceled = false;
- compressindex = -1;
- stopClose = false;
- btnAddFile.IsEnabled = true;
- btnCompress.IsEnabled = true;
- btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_Delete");
- btnRemove.IsEnabled = true;
- btnMoveDown.IsEnabled = true;
- btnMoveUp.IsEnabled = true;
- groupBox1.IsEnabled = true;
- }
- private int GetIndex(int pageindex)
- {
- try
- {
- if (Dispatcher.CheckAccess() == false)
- {
- return Dispatcher.Invoke(new CPDFDocument.GetPageIndexDelegate(delegate (int s)
- {
- if (CompressListView.Items.Count >= compressindex)
- {
- if (CompressListView.Items[compressindex] is CompressDataItem dataItem)
- {
- dataItem.Progress = pageindex + "/" + dataItem.PageCount;
- if (pageindex == dataItem.PageCount - 1)
- {
- dataItem.Progress = LanguageHelper.CompressManager.GetString("Main_CompletedState");
- }
- }
- }
- return 0;
- }), pageindex).ToString().Length;
- }
- else
- {
- if (CompressListView.Items.Count >= compressindex)
- {
- if (CompressListView.Items[compressindex] is CompressDataItem dataItem)
- {
- dataItem.Progress = pageindex + "/" + dataItem.PageCount;
- if (pageindex == dataItem.PageCount - 1)
- {
- dataItem.Progress = LanguageHelper.CompressManager.GetString("Main_CompletedState");
- }
- }
- }
- return 0;
- }
- }
- catch { return -1; }
- }
- private void btnCancel_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (compressindex != -1 && !compressingIntpr.Equals(IntPtr.Zero))
- {
- if (MessageBoxEx.Show(LanguageHelper.CompressManager.GetString("CompressInterruptWarning"), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes && tempDocument != null)
- {
- tempDocument.CompressFile_Cancel(compressingIntpr);
- isCanceled = true;
- if (File.Exists(compressingfilepath))
- File.Delete(compressingfilepath);
- }
- }
- else
- {
- this.Close();
- this.DialogResult = false;
- }
- }
- catch { }
- }
- private void ConverterListView_DragOver(object sender, DragEventArgs e)
- {
- var files = (Array)e.Data.GetData(System.Windows.DataFormats.FileDrop);
- int count = 0;
- string pdf = "pdf";
- foreach (string file in files)
- {
- string text = Path.GetExtension(file);
- if (text.IndexOf(pdf, StringComparison.OrdinalIgnoreCase) >= 0)
- {
- count++;
- }
- }
- if (count < 1)
- {
- e.Effects = System.Windows.DragDropEffects.None;
- Mouse.SetCursor(System.Windows.Input.Cursors.No);
- }
- else
- {
- e.Effects = System.Windows.DragDropEffects.Copy;
- Mouse.SetCursor(System.Windows.Input.Cursors.Arrow);
- }
- return;
- }
- private void ConverterListView_Drop(object sender, DragEventArgs e)
- {
- var files = (Array)e.Data.GetData(System.Windows.DataFormats.FileDrop);
- if (files != null && files.Length > 0)
- {
- var Files = (string[])files;
- for (int i = 0; i < Files.Length; i++)
- {
- AddFiletoList(Files[i]);
- }
- }
- }
- private void ConverterListView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
- {
- }
- private void ConverterListView_PreviewMouseUp(object sender, MouseButtonEventArgs e)
- {
- DependencyObject element = (DependencyObject)e.OriginalSource;
- if (element is Thumb thumb)
- {
- GridViewColumnHeader gridViewColumnHeader = CommonHelper.ViewportHelper.FindVisualParent<GridViewColumnHeader>(thumb);
- if (gridViewColumnHeader != null && gridViewColumnHeader.Content != null)
- {
- GridViewColumn column = FindGridViewColumn(gridViewColumnHeader.Content.ToString(), CompressListView);
- if (column != null)
- {
- if (column.Header == LanguageHelper.CompressManager.GetString("Main_FileName") && column.ActualWidth > 240)
- {
- TextBlock textBlock = FindTextBlockInCellTemplate(column);
- if (textBlock != null)
- {
- textBlock.MaxWidth = textBlock.Width = column.ActualWidth - 20;
- TxbName_SizeChanged(textBlock, null);
- }
- }
- }
- }
- }
- }
- private TextBlock FindTextBlockInCellTemplate(GridViewColumn column)
- {
- DataTemplate dataTemplate = column.CellTemplate;
- if (dataTemplate != null)
- {
- FrameworkElement frameworkElement = dataTemplate.LoadContent() as FrameworkElement;
- if (frameworkElement != null)
- {
- return CommonHelper.ViewportHelper.FindVisualChild<TextBlock>(frameworkElement);
- }
- }
- return null;
- }
- private GridViewColumn FindGridViewColumn(string columnHeader, System.Windows.Controls.ListView listView)
- {
- GridView gridView = listView.View as GridView;
- if (gridView != null)
- {
- foreach (var column in gridView.Columns)
- {
- GridViewColumn gvColumn = column as GridViewColumn;
- if (gvColumn != null && gvColumn.Header.ToString() == columnHeader)
- {
- return gvColumn;
- }
- }
- }
- return null;
- }
- private void UpdateMoveButtonState()
- {
- if (compressindex != -1 && !compressingIntpr.Equals(IntPtr.Zero))
- {
- return;
- }
- if (CompressListView.Items.Count > 0 && CompressListView.SelectedItems.Count > 0)
- {
- btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_Delete"); ;
- int count = CompressListView.Items.Count;
- if (CompressListView.SelectedItems.Contains(CompressListView.Items[CompressListView.Items.Count - 1]))
- {
- btnMoveDown.IsEnabled = false;
- }
- else
- btnMoveDown.IsEnabled = true;
- if (CompressListView.SelectedItems.Contains(CompressListView.Items[0]))
- btnMoveUp.IsEnabled = false;
- else
- btnMoveUp.IsEnabled = true;
- }
- else
- {
- btnRemove.Content = LanguageHelper.CompressManager.GetString("Main_RemoveAll");
- btnMoveDown.IsEnabled = false;
- btnMoveUp.IsEnabled = false;
- }
- }
- private void ConverterListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- UpdateMoveButtonState();
- }
- private void UpdateColumnsWidth(System.Windows.Controls.ListView listView)
- {
- int autoFillColumnIndex = (listView.View as System.Windows.Controls.GridView).Columns.Count - 1;
- if (listView.ActualWidth == Double.NaN)
- {
- listView.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));
- }
- double remainingSpace = listView.ActualWidth;
- for (int i = 0; i < (listView.View as System.Windows.Controls.GridView).Columns.Count; i++)
- {
- if (i != autoFillColumnIndex)
- {
- remainingSpace -= (listView.View as System.Windows.Controls.GridView).Columns[i].ActualWidth;
- (listView.View as System.Windows.Controls.GridView).Columns[autoFillColumnIndex].Width = remainingSpace >= 0 ? remainingSpace : 0;
- }
- }
- }
- private void ConverterListView_Loaded(object sender, RoutedEventArgs e)
- {
- UpdateColumnsWidth(sender as System.Windows.Controls.ListView);
- }
- private void ConverterListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- var pos = e.GetPosition(CompressListView);
- var result = VisualTreeHelper.HitTest(CompressListView, pos);
- if (result != null)
- {
- var listBoxItem = CommonHelper.ViewportHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (listBoxItem == null)
- {
- CompressListView.SelectedItems.Clear();
- }
- }
- CompressListView.Focus();
- }
- private void ConverterListView_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- UpdateColumnsWidth(sender as System.Windows.Controls.ListView);
- }
- private void TxbName_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- }
- private void PART_HeaderGripper_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
- {
- }
- private void rbtnAllPage_Checked(object sender, RoutedEventArgs e)
- {
- }
- private void rbtnCurrentPage_Checked(object sender, RoutedEventArgs e)
- {
- }
- private void rbtnOldPageOnly_Checked(object sender, RoutedEventArgs e)
- {
- }
- private void RbtnEvenPageOnly_Checked(object sender, RoutedEventArgs e)
- {
- }
- public class CompressDataItem : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- private string passWord;
- public string PassWord { get; set; }
- public BitmapSource ImagePath { get; set; }
- public string Name { get; set; }
- public string Size { get; set; }
- public string Path { get; set; }
- public int PageCount { get; set; }
- private string progress;
- public string Progress
- {
- get { return progress; }
- set
- {
- progress = value;
- OnPropertyChanged("Progress");
- }
- }
- }
- private void btnMoveDown_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- var items = CompressListView.SelectedItems;
- if (items.Count > 0)
- {
- List<int> indexs = new List<int>();
- foreach (var selectedItem in CompressListView.SelectedItems)
- {
- var imageItem = (CompressDataItem)selectedItem;
- int index = CompressListView.Items.IndexOf(imageItem);
- indexs.Add(index);
- }
- indexs.Sort();
- for (int i = 0; i < indexs.Count; i++)
- {
- var index = indexs[indexs.Count - 1 - i];
- var item = CompressDatas[index];
- CompressDatas.RemoveAt(index);
- CompressDatas.Insert(index + 1, item);
- }
- CompressListView.SelectedItems.Clear();
- for (int i = 0; i < indexs.Count; i++)
- {
- CompressListView.SelectedItems.Add(CompressListView.Items[indexs[i] + 1]);
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- private void btnMoveUp_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- var items = CompressListView.SelectedItems;
- if (items.Count > 0)
- {
- List<int> indexs = new List<int>();
- foreach (var selectedItem in CompressListView.SelectedItems)
- {
- var imageItem = (CompressDataItem)selectedItem;
- int index = CompressListView.Items.IndexOf(imageItem);
- indexs.Add(index);
- }
- indexs.Sort();
- for (int i = 0; i < indexs.Count; i++)
- {
- var index = indexs[i];
- var item = CompressDatas[index];
- CompressDatas.RemoveAt(index);
- CompressDatas.Insert(index - 1, item);
- }
- CompressListView.SelectedItems.Clear();
- for (int i = 0; i < indexs.Count; i++)
- {
- CompressListView.SelectedItems.Add(CompressListView.Items[indexs[i] - 1]);
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- Regex regex = new Regex("[^0-9]+");
- if (regex.IsMatch(e.Text))
- {
- e.Handled = true;
- }
- }
- private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (sender is System.Windows.Controls.TextBox textBox)
- {
- if (!string.IsNullOrEmpty(textBox.Text))
- {
- int value = int.Parse(textBox.Text);
- if (value < 1)
- {
- textBox.Text = "1";
- textBox.Select(textBox.Text.Length, 0);
- }
- else if (value > 100)
- {
- textBox.Text = "100";
- textBox.Select(textBox.Text.Length, 0);
- }
- }
- else
- {
- textBox.Text = "1";
- textBox.Select(textBox.Text.Length, 0);
- }
- }
- }
- }
- public class MaxWidthToTextTrimmingConverter : IMultiValueConverter
- {
- public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
- {
- if (values.Length == 2 && values[0] is double maxWidth && values[1] is string text)
- {
- if (maxWidth >= MeasureTextWidth(text))
- {
- return TextTrimming.None;
- }
- else
- {
- return TextTrimming.CharacterEllipsis;
- }
- }
- return TextTrimming.None;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotSupportedException();
- }
- public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- private double MeasureTextWidth(string text)
- {
- var formattedText = new FormattedText(
- text,
- CultureInfo.CurrentCulture,
- System.Windows.FlowDirection.LeftToRight,
- new Typeface("Arial"),
- 12,
- System.Windows.Media.Brushes.Black);
- return formattedText.WidthIncludingTrailingWhitespace;
- }
- }
- }
|