CPDFFontUI.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 FontFamilyChanged;
  26. public event EventHandler FontStyleChanged;
  27. public event EventHandler FontSizeChanged;
  28. public event EventHandler FontAlignChanged;
  29. public string FontFamilyValue
  30. {
  31. get => FontFamilyComboBox.SelectedItem.ToString();
  32. set
  33. {
  34. if (value == "Courier New")
  35. {
  36. FontFamilyComboBox.SelectedIndex = 0;
  37. }
  38. else if (value == "Arial")
  39. {
  40. FontFamilyComboBox.SelectedIndex = 1;
  41. }
  42. else
  43. {
  44. FontFamilyComboBox.SelectedIndex = 2;
  45. }
  46. }
  47. }
  48. private bool _isBold;
  49. public bool IsBold
  50. {
  51. get => _isBold;
  52. set
  53. {
  54. _isBold = value;
  55. if (_isBold && IsItalic)
  56. {
  57. FontStyleComboBox.SelectedIndex = 3;
  58. }
  59. else if (_isBold && !IsItalic)
  60. {
  61. FontStyleComboBox.SelectedIndex = 1;
  62. }
  63. else if (!_isBold && IsItalic)
  64. {
  65. FontStyleComboBox.SelectedIndex = 2;
  66. }
  67. else
  68. {
  69. FontStyleComboBox.SelectedIndex = 0;
  70. }
  71. }
  72. }
  73. private bool _isItalic;
  74. public bool IsItalic
  75. {
  76. get => _isItalic;
  77. set
  78. {
  79. _isItalic = value;
  80. if (IsBold && _isItalic)
  81. {
  82. FontStyleComboBox.SelectedIndex = 3;
  83. }
  84. else if (IsBold && !_isItalic)
  85. {
  86. FontStyleComboBox.SelectedIndex = 1;
  87. }
  88. else if (!IsBold && _isItalic)
  89. {
  90. FontStyleComboBox.SelectedIndex = 2;
  91. }
  92. else
  93. {
  94. FontStyleComboBox.SelectedIndex = 0;
  95. }
  96. }
  97. }
  98. private TextAlignment _textAlignment;
  99. public TextAlignment TextAlignment
  100. {
  101. get
  102. {
  103. if ((bool)LeftAlignRadioButton.IsChecked)
  104. {
  105. return TextAlignment.Left;
  106. }
  107. else if ((bool)CenterAlignRadioButton.IsChecked)
  108. {
  109. return TextAlignment.Center;
  110. }
  111. else
  112. {
  113. return TextAlignment.Right;
  114. }
  115. }
  116. set
  117. {
  118. if (TextAlignment.Left == value)
  119. {
  120. LeftAlignRadioButton.IsChecked = true;
  121. }
  122. else if (TextAlignment.Center == value)
  123. {
  124. CenterAlignRadioButton.IsChecked = true;
  125. }
  126. else
  127. {
  128. RightAlignRadioButton.IsChecked = true;
  129. }
  130. }
  131. }
  132. private int _fontSizeValue = 20;
  133. public int FontSizeValue
  134. {
  135. get => _fontSizeValue;
  136. set
  137. {
  138. _fontSizeValue = value;
  139. OnPropertyChanged(nameof(FontSizeValue));
  140. FontSizeChanged?.Invoke(this, EventArgs.Empty);
  141. }
  142. }
  143. public CPDFFontUI()
  144. {
  145. InitializeComponent();
  146. InitComboBox();
  147. this.DataContext = this;
  148. }
  149. public void InitComboBox()
  150. {
  151. List<string> fontNameList = new List<string>()
  152. {
  153. {"Courier" },
  154. {"Helvetica" },
  155. {"Times" }
  156. };
  157. FontFamilyComboBox.ItemsSource = fontNameList;
  158. FontFamilyComboBox.SelectedIndex = 1;
  159. List<string> fontStyleList = new List<string>()
  160. {
  161. {"Common" },
  162. {"Bold" },
  163. {"Italic" },
  164. {"Bold and Italic" }
  165. };
  166. FontStyleComboBox.ItemsSource = fontStyleList;
  167. FontStyleComboBox.SelectedIndex = 0;
  168. List<int> fontSizeList = new List<int>()
  169. {
  170. {6},
  171. {8},
  172. {9},
  173. {10},
  174. {12},
  175. {14},
  176. {18},
  177. {20},
  178. {24},
  179. {26},
  180. {28},
  181. {32},
  182. {30},
  183. {32},
  184. {48},
  185. {72}
  186. };
  187. FontSizeComboBox.InitPresetNumberArray(fontSizeList);
  188. }
  189. protected void OnPropertyChanged(string propertyName)
  190. {
  191. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  192. }
  193. private void FontFamilyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  194. {
  195. FontFamilyTextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  196. FontFamilyChanged?.Invoke(this, EventArgs.Empty);
  197. }
  198. private void FontStyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  199. {
  200. FontStyleTextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  201. if (FontStyleTextBox.Text == "Common")
  202. {
  203. IsBold = false;
  204. IsItalic = false;
  205. }
  206. else if (FontStyleTextBox.Text == "Italic")
  207. {
  208. IsBold = false;
  209. IsItalic = true;
  210. }
  211. else if (FontStyleTextBox.Text == "Bold")
  212. {
  213. IsBold = true;
  214. IsItalic = false;
  215. }
  216. else
  217. {
  218. IsBold = true;
  219. IsItalic = true;
  220. }
  221. FontStyleChanged?.Invoke(this, EventArgs.Empty);
  222. }
  223. private void AlignRadioButton_Checked(object sender, RoutedEventArgs e)
  224. {
  225. FontAlignChanged?.Invoke(this, EventArgs.Empty);
  226. }
  227. }
  228. }