CPDFFontUI.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace compdfkit_tools.Common
  18. {
  19. /// <summary>
  20. /// CPDFFontUI.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class CPDFFontUI : UserControl, INotifyPropertyChanged
  23. {
  24. public event PropertyChangedEventHandler PropertyChanged;
  25. public event EventHandler FontChanged;
  26. public string FontFamilyValue
  27. {
  28. get => FontFamilyComboBox.SelectedItem.ToString();
  29. set => FontFamilyComboBox.SelectedItem = value;
  30. }
  31. public bool IsRewrite = false;
  32. public bool IsBold;
  33. public bool IsItalic;
  34. public TextAlignment TextAlignment
  35. {
  36. get
  37. {
  38. if ((bool)LeftAlignRadioButton.IsChecked)
  39. {
  40. return TextAlignment.Left;
  41. }
  42. else if ((bool)CenterAlignRadioButton.IsChecked)
  43. {
  44. return TextAlignment.Center;
  45. }
  46. else
  47. {
  48. return TextAlignment.Right;
  49. }
  50. }
  51. }
  52. private int _fontSizeValue = 20;
  53. public int FontSizeValue
  54. {
  55. get => _fontSizeValue;
  56. set
  57. {
  58. _fontSizeValue = value;
  59. OnPropertyChanged(nameof(FontSizeValue));
  60. OnFontChanged();
  61. FontChanged?.Invoke(this, EventArgs.Empty);
  62. }
  63. }
  64. public CPDFFontUI()
  65. {
  66. InitializeComponent();
  67. InitComboBox();
  68. this.DataContext = this;
  69. }
  70. public void InitComboBox()
  71. {
  72. List<string> fontNameList = new List<string>()
  73. {
  74. {"Courier" },
  75. {"Helvetica" },
  76. {"Times" }
  77. };
  78. FontFamilyComboBox.ItemsSource = fontNameList;
  79. FontFamilyComboBox.SelectedIndex = 1;
  80. List<string> fontStyleList = new List<string>()
  81. {
  82. {"Common" },
  83. {"Bold" },
  84. {"Italic" },
  85. {"Bold and Italic" }
  86. };
  87. FontStyleComboBox.ItemsSource = fontStyleList;
  88. FontStyleComboBox.SelectedIndex = 0;
  89. List<int> fontSizeList = new List<int>()
  90. {
  91. {6},
  92. {8},
  93. {9},
  94. {10},
  95. {12},
  96. {14},
  97. {18},
  98. {20},
  99. {24},
  100. {26},
  101. {28},
  102. {32},
  103. {30},
  104. {32},
  105. {48},
  106. {72}
  107. };
  108. FontSizeComboBox.InitPresetNumberArray(fontSizeList);
  109. }
  110. protected void OnPropertyChanged(string propertyName)
  111. {
  112. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  113. }
  114. private void FontFamilyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  115. {
  116. if (!IsRewrite)
  117. {
  118. FontFamilyTextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  119. FontChanged?.Invoke(this, EventArgs.Empty);
  120. }
  121. }
  122. private void FontStyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  123. {
  124. FontStyleTextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  125. if(FontStyleTextBox.Text == "Common")
  126. {
  127. IsBold = false;
  128. IsItalic = false;
  129. }
  130. else if (FontStyleTextBox.Text == "Italic")
  131. {
  132. IsBold = false;
  133. IsItalic = true;
  134. }
  135. else if (FontStyleTextBox.Text == "Bold")
  136. {
  137. IsBold = true;
  138. IsItalic = false;
  139. }
  140. else
  141. {
  142. IsBold = true;
  143. IsItalic = true;
  144. }
  145. FontChanged?.Invoke(this, EventArgs.Empty);
  146. }
  147. private void AlignRadioButton_Checked(object sender, RoutedEventArgs e)
  148. {
  149. FontChanged?.Invoke(this, EventArgs.Empty);
  150. }
  151. private void OnFontChanged()
  152. {
  153. FontChanged?.Invoke(this, EventArgs.Empty);
  154. }
  155. }
  156. }