CPDFTextStyleUI.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace Compdfkit_Tools.Edit
  6. {
  7. public partial class CPDFTextStyleUI : UserControl
  8. {
  9. #region
  10. public event EventHandler<string> TextFontChanged;
  11. public event EventHandler<bool> TextBoldChanged;
  12. public event EventHandler<bool> TextItalicChanged;
  13. public event EventHandler<double> TextSizeChanged;
  14. #endregion
  15. public CPDFTextStyleUI()
  16. {
  17. InitializeComponent();
  18. }
  19. #region Set font name
  20. public void SetFontNames(List<string> fontNames)
  21. {
  22. FontNameComboBox.ItemsSource = null;
  23. if (fontNames != null && fontNames.Count > 0)
  24. {
  25. List<ComboBoxItem> fontNameList = new List<ComboBoxItem>();
  26. foreach (string fontName in fontNames)
  27. {
  28. fontNameList.Add(new ComboBoxItem()
  29. {
  30. Content = fontName,
  31. VerticalContentAlignment=VerticalAlignment.Center,
  32. HorizontalContentAlignment=HorizontalAlignment.Left
  33. });
  34. }
  35. FontNameComboBox.ItemsSource = fontNameList;
  36. }
  37. }
  38. public void SelectFontName(string fontName)
  39. {
  40. if(string.IsNullOrEmpty(fontName))
  41. {
  42. return;
  43. }
  44. List<ComboBoxItem> fontNameList = FontNameComboBox.ItemsSource as List<ComboBoxItem>;
  45. if (fontNameList != null && fontNameList.Count>0)
  46. {
  47. int selectIndex = -1;
  48. for(int i=0;i<fontNameList.Count; i++)
  49. {
  50. ComboBoxItem checkItem= fontNameList[i];
  51. if(checkItem.Content!=null && checkItem.Content.ToString().ToLower()==fontName.ToLower() )
  52. {
  53. selectIndex=i;
  54. break;
  55. }
  56. }
  57. FontNameComboBox.SelectedIndex = selectIndex;
  58. }
  59. }
  60. public void SetFontStyle(bool isBold,bool isItalic)
  61. {
  62. if (isBold == false && isItalic == false)
  63. {
  64. FontStyleBox.SelectedIndex = 0;
  65. return;
  66. }
  67. if (isBold && isItalic == false)
  68. {
  69. FontStyleBox.SelectedIndex = 1;
  70. return;
  71. }
  72. if (isBold == false && isItalic)
  73. {
  74. FontStyleBox.SelectedIndex = 2;
  75. return;
  76. }
  77. if (isBold && isItalic )
  78. {
  79. FontStyleBox.SelectedIndex = 3;
  80. }
  81. }
  82. public void SetFontSize(double newFontSize)
  83. {
  84. if (FontSizeTextBox != null)
  85. {
  86. if (newFontSize - (int)(newFontSize) > 0)
  87. {
  88. FontSizeTextBox.Text = ((int)newFontSize).ToString();
  89. }
  90. else
  91. {
  92. FontSizeTextBox.Text = ((int)(newFontSize)).ToString();
  93. }
  94. }
  95. }
  96. #endregion
  97. #region Property changed
  98. private void FontNameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  99. {
  100. if(FontNameComboBox.SelectedIndex==-1)
  101. {
  102. TextFontChanged?.Invoke(this,string.Empty);
  103. return;
  104. }
  105. ComboBoxItem selectItem= FontNameComboBox.SelectedItem as ComboBoxItem;
  106. if(selectItem != null && selectItem.Content != null)
  107. {
  108. TextFontChanged?.Invoke(this, selectItem.Content.ToString());
  109. }
  110. }
  111. private void FontStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  112. {
  113. int selectIndex = Math.Max(0, FontStyleBox.SelectedIndex);
  114. switch(selectIndex)
  115. {
  116. case 0:
  117. TextBoldChanged?.Invoke(this,false);
  118. TextItalicChanged?.Invoke(this, false);
  119. break;
  120. case 1:
  121. TextBoldChanged?.Invoke(this, true);
  122. TextItalicChanged?.Invoke(this, false);
  123. break;
  124. case 2:
  125. TextBoldChanged?.Invoke(this, false);
  126. TextItalicChanged?.Invoke(this, true);
  127. break;
  128. case 3:
  129. TextBoldChanged?.Invoke(this, true);
  130. TextItalicChanged?.Invoke(this, true);
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. private void FontSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  137. {
  138. ComboBoxItem selectItem = FontSizeComboBox.SelectedItem as ComboBoxItem;
  139. if (selectItem != null && selectItem.Content != null)
  140. {
  141. if (int.TryParse(selectItem.Content.ToString(), out int newFontSize))
  142. {
  143. FontSizeTextBox.Text = (newFontSize).ToString();
  144. TextSizeChanged?.Invoke(this, newFontSize);
  145. }
  146. }
  147. }
  148. #endregion
  149. }
  150. }