CPDFFontUI.xaml.cs 6.4 KB

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