ThemesContent.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Office.Interop.PowerPoint;
  4. using PDF_Office.CustomControl;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Properties;
  7. using PDFSettings.Settings;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Navigation;
  21. using System.Windows.Shapes;
  22. using static PDF_Office.CustomControl.ColorDropBox;
  23. namespace PDF_Office.Views.PropertyPanel.ViewModular
  24. {
  25. /// <summary>
  26. /// ThemesContent.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class ThemesContent : UserControl
  29. {
  30. private ColorDropBoxPop colorPop = new ColorDropBoxPop();
  31. private AdornerLayer popLayer;
  32. private AdornerPresenter layerPresent;
  33. private Canvas popCanvas;
  34. public Color? SelectedColor { get; set; }
  35. public ThemesContent()
  36. {
  37. InitializeComponent();
  38. InitBeforeShow();
  39. }
  40. /// <summary>
  41. /// 设置自定义主题显示状态
  42. /// </summary>
  43. public void InitBeforeShow()
  44. {
  45. if (Settings.Default.AppProperties.CustomColorList != null && Settings.Default.AppProperties.CustomColorList.Count > 0)
  46. {
  47. var list = Settings.Default.AppProperties.CustomColorList;
  48. switch (list.Count)
  49. {
  50. case 1:
  51. (((RBtnThemes1.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[0]);
  52. RBtnThemes1.Visibility = Visibility.Visible;
  53. break;
  54. case 2:
  55. (((RBtnThemes1.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[0]);
  56. (((RBtnThemes2.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[1]);
  57. RBtnThemes1.Visibility = Visibility.Visible;
  58. RBtnThemes2.Visibility = Visibility.Visible;
  59. break;
  60. case 3:
  61. (((RBtnThemes1.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[0]);
  62. (((RBtnThemes2.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[1]);
  63. (((RBtnThemes3.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[2]);
  64. RBtnThemes1.Visibility = Visibility.Visible;
  65. RBtnThemes2.Visibility = Visibility.Visible;
  66. RBtnThemes3.Visibility = Visibility.Visible;
  67. break;
  68. case 4:
  69. (((RBtnThemes1.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[0]);
  70. (((RBtnThemes2.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[1]);
  71. (((RBtnThemes3.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[2]);
  72. (((RBtnThemes4.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill = new SolidColorBrush(list[3]);
  73. RBtnThemes1.Visibility = Visibility.Visible;
  74. RBtnThemes2.Visibility = Visibility.Visible;
  75. RBtnThemes3.Visibility = Visibility.Visible;
  76. RBtnThemes4.Visibility = Visibility.Visible;
  77. break;
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// 页面加载
  83. /// </summary>
  84. /// <param name="sender"></param>
  85. /// <param name="e"></param>
  86. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  87. {
  88. if (popLayer == null)
  89. {
  90. Window parentWnd = Window.GetWindow(this);
  91. if (parentWnd != null && parentWnd.Content is UIElement)
  92. {
  93. popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
  94. if (popLayer != null && colorPop != null)
  95. {
  96. layerPresent = new AdornerPresenter(popLayer);
  97. popLayer.Add(layerPresent);
  98. colorPop.Visibility = Visibility.Collapsed;
  99. colorPop.ColorSelected += ColorPop_ColorSelected;
  100. popCanvas = new Canvas();
  101. popCanvas.Children.Add(colorPop);
  102. layerPresent.VisualContent = popCanvas;
  103. parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
  104. {
  105. colorPop.Visibility = Visibility.Collapsed;
  106. }), false);
  107. }
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// 自定义主题颜色,选择
  113. /// </summary>
  114. /// <param name="sender"></param>
  115. /// <param name="e"></param>
  116. private void ColorPop_ColorSelected(object sender, Color e)
  117. {
  118. if (RBtnThemes1.Visibility == Visibility.Collapsed)
  119. {
  120. RBtnThemes1.Visibility = Visibility.Visible;
  121. var panel = RBtnThemes1.Content as StackPanel;
  122. var rec = (panel.Children[0] as Border).Child as Rectangle;
  123. rec.Fill = new SolidColorBrush(e);
  124. Settings.Default.AppProperties.CustomColorList.Add(e);
  125. }
  126. else if (RBtnThemes2.Visibility == Visibility.Collapsed)
  127. {
  128. RBtnThemes2.Visibility = Visibility.Visible;
  129. var panel = RBtnThemes2.Content as StackPanel;
  130. var rec = (panel.Children[0] as Border).Child as Rectangle;
  131. rec.Fill = new SolidColorBrush(e);
  132. Settings.Default.AppProperties.CustomColorList.Add(e);
  133. }
  134. else if (RBtnThemes3.Visibility == Visibility.Collapsed)
  135. {
  136. RBtnThemes3.Visibility = Visibility.Visible;
  137. var panel = RBtnThemes3.Content as StackPanel;
  138. var rec = (panel.Children[0] as Border).Child as Rectangle;
  139. rec.Fill = new SolidColorBrush(e);
  140. Settings.Default.AppProperties.CustomColorList.Add(e);
  141. }
  142. else if (RBtnThemes4.Visibility == Visibility.Collapsed)
  143. {
  144. RBtnThemes4.Visibility = Visibility.Visible;
  145. var panel = RBtnThemes4.Content as StackPanel;
  146. var rec = (panel.Children[0] as Border).Child as Rectangle;
  147. rec.Fill = new SolidColorBrush(e);
  148. Settings.Default.AppProperties.CustomColorList.Add(e);
  149. }
  150. #region to do
  151. //if(Settings.Default.AppProperties.CustomColorList.Count>4)
  152. //{
  153. // for(int i=4;i< Settings.Default.AppProperties.CustomColorList.Count;i++)
  154. // {
  155. // Settings.Default.AppProperties.CustomColorList.RemoveAt(i);
  156. // }
  157. //}
  158. #endregion to do
  159. Settings.Default.Save();
  160. if (SelectedColor != e)
  161. {
  162. SelectedColor = e;
  163. colorPop.AddColorToRecent(e);
  164. }
  165. }
  166. /// <summary>
  167. /// 自定义颜色显示状态发生变化时
  168. /// </summary>
  169. /// <param name="sender"></param>
  170. /// <param name="e"></param>
  171. private void RBtnThemes1_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  172. {
  173. if (RBtnThemes1.Visibility == Visibility.Visible &&
  174. RBtnThemes2.Visibility == Visibility.Visible &&
  175. RBtnThemes3.Visibility == Visibility.Visible &&
  176. RBtnThemes4.Visibility == Visibility.Visible)
  177. {
  178. RBtnCustom.Visibility = Visibility.Collapsed;
  179. }
  180. else
  181. {
  182. RBtnCustom.Visibility = Visibility.Visible;
  183. }
  184. }
  185. /// <summary>
  186. /// 添加自定义主题颜色
  187. /// </summary>
  188. /// <param name="sender"></param>
  189. /// <param name="e"></param>
  190. private void RBtnCustom_Click(object sender, RoutedEventArgs e)
  191. {
  192. if (colorPop != null)
  193. {
  194. if (colorPop.Visibility == Visibility.Visible)
  195. {
  196. colorPop.Visibility = Visibility.Collapsed;
  197. return;
  198. }
  199. colorPop.Visibility = Visibility.Visible;
  200. System.Windows.Point offset = GetOffsetToWindow();
  201. Window parentWnd = Window.GetWindow(this);
  202. colorPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
  203. colorPop.SetValue(Canvas.TopProperty, offset.Y - colorPop.DesiredSize.Height / 4);
  204. if (offset.X + colorPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
  205. {
  206. colorPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - colorPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
  207. }
  208. else
  209. {
  210. colorPop.SetValue(Canvas.LeftProperty, offset.X - colorPop.DesiredSize.Width / 2 - 30);
  211. }
  212. colorPop.AddColorToRecent((Color)ColorConverter.ConvertFromString("White")); ;
  213. }
  214. }
  215. public System.Windows.Point GetOffsetToWindow()
  216. {
  217. Window parentWnd = Window.GetWindow(this);
  218. return TransformToAncestor(parentWnd).Transform(new System.Windows.Point(0, 0));
  219. }
  220. /// <summary>
  221. /// 删除自定义颜色
  222. /// </summary>
  223. /// <param name="sender"></param>
  224. /// <param name="e"></param>
  225. private void MenuItem_Click(object sender, RoutedEventArgs e)
  226. {
  227. var contextmenu = (sender as MenuItem).Parent as ContextMenu;
  228. if (contextmenu != null)
  229. {
  230. RadioButton btn = contextmenu.PlacementTarget as RadioButton;
  231. btn.Visibility = Visibility.Collapsed;
  232. if (btn.IsChecked == true)
  233. {
  234. //如果删除了选中色 则更换主题为白色
  235. RBtnLight.IsChecked = true;
  236. }
  237. var color = (((btn.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill;
  238. if (Settings.Default.AppProperties.CustomColorList.Contains(((SolidColorBrush)color).Color))
  239. {
  240. Settings.Default.AppProperties.CustomColorList.Remove(((SolidColorBrush)color).Color);
  241. Settings.Default.Save();
  242. }
  243. }
  244. }
  245. }
  246. }