GlobalColorPickerWindow.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using GeekDesk.Interface;
  2. using GeekDesk.Util;
  3. using System;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.Threading;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. namespace GeekDesk.Control.Windows
  12. {
  13. /// <summary>
  14. /// GlobalColorPickerWindow.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class GlobalColorPickerWindow : IWindowCommon
  17. {
  18. PixelColorPickerWindow colorPickerWindow = null;
  19. class PrivateDataContext : INotifyPropertyChanged
  20. {
  21. private bool copyAnimation = false;
  22. public bool CopyAnimation
  23. {
  24. set
  25. {
  26. copyAnimation = value;
  27. OnPropertyChanged("CopyAnimation");
  28. }
  29. get { return copyAnimation; }
  30. }
  31. public event PropertyChangedEventHandler PropertyChanged;
  32. private void OnPropertyChanged(string propertyName)
  33. {
  34. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  35. }
  36. }
  37. public GlobalColorPickerWindow()
  38. {
  39. this.Topmost = true;
  40. InitializeComponent();
  41. this.DataContext = new PrivateDataContext();
  42. }
  43. public void OnKeyDown(object sender, KeyEventArgs e)
  44. {
  45. if (e.Key == Key.Escape)
  46. {
  47. this.DataContext = null;
  48. this.Close();
  49. }
  50. }
  51. /// <summary>
  52. /// 取消按钮事件
  53. /// </summary>
  54. /// <param name="sender"></param>
  55. /// <param name="e"></param>
  56. private void MyColorPicker_Canceled(object sender, RoutedEventArgs e)
  57. {
  58. MyColorPickerClose();
  59. }
  60. private void MyColorPicker_Confirmed(object sender, RoutedEventArgs e)
  61. {
  62. CopySuccess.Visibility = Visibility.Visible;
  63. PrivateDataContext pdc = this.DataContext as PrivateDataContext;
  64. pdc.CopyAnimation = true;
  65. new Thread(() =>
  66. {
  67. Thread.Sleep(400);
  68. this.Dispatcher.Invoke(() =>
  69. {
  70. pdc.CopyAnimation = false;
  71. CopySuccess.Visibility = Visibility.Collapsed;
  72. });
  73. }).Start();
  74. Color c = MyColorPicker.SelectedBrush.Color;
  75. Clipboard.SetData(DataFormats.Text, string.Format("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B));
  76. }
  77. /// <summary>
  78. /// 移动窗口
  79. /// </summary>
  80. /// <param name="sender"></param>
  81. /// <param name="e"></param>
  82. private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
  83. {
  84. if (e.LeftButton == MouseButtonState.Pressed)
  85. {
  86. this.DragMove();
  87. }
  88. }
  89. private void MyColorPicker_Checked(object sender, RoutedEventArgs e)
  90. {
  91. this.Hide();
  92. if (colorPickerWindow == null || !colorPickerWindow.Activate())
  93. {
  94. colorPickerWindow = new PixelColorPickerWindow(MyColorPicker);
  95. }
  96. colorPickerWindow.Show();
  97. }
  98. private void MyColorPickerClose()
  99. {
  100. this.Close();
  101. }
  102. private static System.Windows.Window window = null;
  103. public static void CreateNoShow()
  104. {
  105. if (window == null || !window.Activate())
  106. {
  107. window = new GlobalColorPickerWindow();
  108. window.Opacity = 0;
  109. App.DoEvents();
  110. window.Show();
  111. }
  112. window.Hide();
  113. new Thread(() =>
  114. {
  115. Thread.Sleep(200);
  116. App.Current.Dispatcher.Invoke(() =>
  117. {
  118. GlobalColorPickerWindow thisWindow = (GlobalColorPickerWindow)window;
  119. if (thisWindow.colorPickerWindow == null || !thisWindow.colorPickerWindow.Activate())
  120. {
  121. thisWindow.colorPickerWindow = new PixelColorPickerWindow(thisWindow.MyColorPicker);
  122. }
  123. thisWindow.colorPickerWindow.Show();
  124. });
  125. }).Start();
  126. }
  127. public static void Show()
  128. {
  129. if (window == null || !window.Activate())
  130. {
  131. window = new GlobalColorPickerWindow();
  132. }
  133. window.Opacity = 1;
  134. window.Show();
  135. Keyboard.Focus(window);
  136. }
  137. public static void ShowOrHide()
  138. {
  139. if (window == null || !window.Activate())
  140. {
  141. window = new GlobalColorPickerWindow();
  142. window.Show();
  143. Keyboard.Focus(window);
  144. }
  145. else
  146. {
  147. window.Close();
  148. }
  149. }
  150. private void MyColorPicker_SelectedColorChanged(object sender, HandyControl.Data.FunctionEventArgs<Color> e)
  151. {
  152. Show();
  153. }
  154. private void Button_MouseEnter(object sender, MouseEventArgs e)
  155. {
  156. Button btn = sender as Button;
  157. btn.Opacity = 1;
  158. }
  159. private void Button_MouseLeave(object sender, MouseEventArgs e)
  160. {
  161. Button btn = sender as Button;
  162. btn.Opacity = 0.6;
  163. }
  164. private void Button_Click(object sender, RoutedEventArgs e)
  165. {
  166. this.Close();
  167. }
  168. private ICommand _hideCommand;
  169. public ICommand HideCommand
  170. {
  171. get
  172. {
  173. if (_hideCommand == null)
  174. {
  175. _hideCommand = new RelayCommand(
  176. p =>
  177. {
  178. return true;
  179. },
  180. p =>
  181. {
  182. //CopySuccess.Visibility = Visibility.Collapsed;
  183. });
  184. }
  185. return _hideCommand;
  186. }
  187. }
  188. }
  189. }