GlobalColorPickerWindow.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using GeekDesk.Interface;
  2. using System;
  3. using System.Threading;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. namespace GeekDesk.Control.Windows
  9. {
  10. /// <summary>
  11. /// GlobalColorPickerWindow.xaml 的交互逻辑
  12. /// </summary>
  13. public partial class GlobalColorPickerWindow : IWindowCommon
  14. {
  15. PixelColorPickerWindow colorPickerWindow = null;
  16. public GlobalColorPickerWindow()
  17. {
  18. this.Topmost = true;
  19. InitializeComponent();
  20. }
  21. public void OnKeyDown(object sender, KeyEventArgs e)
  22. {
  23. if (e.Key == Key.Escape)
  24. {
  25. this.DataContext = null;
  26. this.Close();
  27. }
  28. }
  29. /// <summary>
  30. /// 取消按钮事件
  31. /// </summary>
  32. /// <param name="sender"></param>
  33. /// <param name="e"></param>
  34. private void MyColorPicker_Canceled(object sender, EventArgs e)
  35. {
  36. MyColorPickerClose();
  37. }
  38. private void MyColorPicker_Confirmed(object sender, HandyControl.Data.FunctionEventArgs<Color> e)
  39. {
  40. Color c = MyColorPicker.SelectedBrush.Color;
  41. Clipboard.SetData(DataFormats.Text, string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", c.A, c.R, c.G, c.B));
  42. }
  43. /// <summary>
  44. /// 移动窗口
  45. /// </summary>
  46. /// <param name="sender"></param>
  47. /// <param name="e"></param>
  48. private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
  49. {
  50. if (e.LeftButton == MouseButtonState.Pressed)
  51. {
  52. this.DragMove();
  53. }
  54. }
  55. private void MyColorPicker_Checked(object sender, RoutedEventArgs e)
  56. {
  57. this.Hide();
  58. if (colorPickerWindow == null || !colorPickerWindow.Activate())
  59. {
  60. colorPickerWindow = new PixelColorPickerWindow(MyColorPicker);
  61. }
  62. colorPickerWindow.Show();
  63. }
  64. private void MyColorPickerClose()
  65. {
  66. this.Close();
  67. }
  68. private static System.Windows.Window window = null;
  69. public static void CreateNoShow()
  70. {
  71. if (window == null || !window.Activate())
  72. {
  73. window = new GlobalColorPickerWindow();
  74. window.Opacity = 0;
  75. App.DoEvents();
  76. window.Show();
  77. }
  78. window.Hide();
  79. new Thread(() =>
  80. {
  81. Thread.Sleep(200);
  82. App.Current.Dispatcher.Invoke(() =>
  83. {
  84. GlobalColorPickerWindow thisWindow = (GlobalColorPickerWindow)window;
  85. if (thisWindow.colorPickerWindow == null || !thisWindow.colorPickerWindow.Activate())
  86. {
  87. thisWindow.colorPickerWindow = new PixelColorPickerWindow(thisWindow.MyColorPicker);
  88. }
  89. thisWindow.colorPickerWindow.Show();
  90. });
  91. }).Start();
  92. }
  93. public static void Show()
  94. {
  95. if (window == null || !window.Activate())
  96. {
  97. window = new GlobalColorPickerWindow();
  98. }
  99. window.Opacity = 1;
  100. window.Show();
  101. Keyboard.Focus(window);
  102. }
  103. public static void ShowOrHide()
  104. {
  105. if (window == null || !window.Activate())
  106. {
  107. window = new GlobalColorPickerWindow();
  108. window.Show();
  109. Keyboard.Focus(window);
  110. }
  111. else
  112. {
  113. window.Close();
  114. }
  115. }
  116. private void MyColorPicker_SelectedColorChanged(object sender, HandyControl.Data.FunctionEventArgs<Color> e)
  117. {
  118. Show();
  119. }
  120. private void Button_MouseEnter(object sender, MouseEventArgs e)
  121. {
  122. Button btn = sender as Button;
  123. btn.Opacity = 1;
  124. }
  125. private void Button_MouseLeave(object sender, MouseEventArgs e)
  126. {
  127. Button btn = sender as Button;
  128. btn.Opacity = 0.6;
  129. }
  130. private void Button_Click(object sender, RoutedEventArgs e)
  131. {
  132. this.Close();
  133. }
  134. }
  135. }