PixelColorPickerWindow.xaml.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using GeekDesk.Interface;
  2. using GeekDesk.Util;
  3. using HandyControl.Controls;
  4. using System;
  5. using System.Reflection;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Controls.Primitives;
  9. using System.Windows.Forms;
  10. using System.Windows.Input;
  11. using System.Windows.Interop;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using Cursors = System.Windows.Input.Cursors;
  15. using KeyEventArgs = System.Windows.Input.KeyEventArgs;
  16. using MouseEventArgs = System.Windows.Input.MouseEventArgs;
  17. namespace GeekDesk.Control.Windows
  18. {
  19. /// <summary>
  20. /// ColorPickerWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class PixelColorPickerWindow : IWindowCommon
  23. {
  24. private static int PIXEL_REC_LENGTH = 20;
  25. private static readonly int MIN_LENGTH = 10;
  26. private static readonly int MAX_LENGTH = 50;
  27. private static System.Drawing.Bitmap bgBitmap;
  28. private readonly ColorPicker colorPicker;
  29. [System.Runtime.InteropServices.DllImport("user32.dll")]
  30. private static extern bool SetProcessDPIAware();
  31. public PixelColorPickerWindow(ColorPicker colorPicker)
  32. {
  33. InitializeComponent();
  34. this.colorPicker = colorPicker;
  35. try
  36. {
  37. SetProcessDPIAware();
  38. }
  39. catch (Exception e) { }
  40. ColorPickerWindow_Init();
  41. }
  42. private void ColorPickerWindow_Init()
  43. {
  44. this.WindowState = WindowState.Normal;//还原窗口(非最小化和最大化)
  45. var screens = Screen.AllScreens;
  46. int allWidth = 0;
  47. int allHeight = 0;
  48. int x = 0;
  49. int y = 0;
  50. //获取缩放比例
  51. double scale = ScreenUtil.GetScreenScalingFactor();
  52. foreach (var screen in screens)
  53. {
  54. var rect = screen.Bounds;
  55. allWidth += rect.Width;
  56. allHeight += rect.Height;
  57. x = Math.Min(x, rect.X);
  58. y = Math.Min(y, rect.Y);
  59. }
  60. //如果主显示器是最左边和最上边,则显示主显示器的缩放比例,反之则缩放比例不添加缩放比例
  61. if (Screen.PrimaryScreen.Bounds.X != x || Screen.PrimaryScreen.Bounds.Y != y)
  62. {
  63. scale = 1;
  64. }
  65. this.Width = allWidth;
  66. this.Height = allHeight;
  67. this.Left = x;
  68. this.Top = y;
  69. DesktopBG.Width = this.Width;
  70. DesktopBG.Height = this.Height;
  71. this.Topmost = true;
  72. bgBitmap = new System.Drawing.Bitmap(
  73. (int)(Width * scale),
  74. (int)(Height * scale),
  75. System.Drawing.Imaging.PixelFormat.Format32bppArgb
  76. );
  77. using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bgBitmap))
  78. {
  79. g.CopyFromScreen(
  80. (int)this.Left,
  81. (int)this.Top,
  82. 0,
  83. 0,
  84. bgBitmap.Size
  85. );
  86. }
  87. BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(
  88. bgBitmap.GetHbitmap(),
  89. IntPtr.Zero,
  90. Int32Rect.Empty,
  91. BitmapSizeOptions.FromEmptyOptions()
  92. );
  93. DesktopBG.Source = bs;
  94. VisualBrush b = (VisualBrush)PixelBG.Fill;
  95. b.Visual = DesktopBG;
  96. Mouse.OverrideCursor = Cursors.Cross;
  97. SetPixelAbout(null);
  98. }
  99. public void OnKeyDown(object sender, KeyEventArgs e)
  100. {
  101. if (e.Key == Key.Escape)
  102. {
  103. this.DataContext = null;
  104. this.Close();
  105. }
  106. }
  107. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  108. {
  109. Mouse.OverrideCursor = null;
  110. Point pos = e.MouseDevice.GetPosition(DesktopBG);
  111. System.Drawing.Color colorD = bgBitmap.GetPixel((int)pos.X, (int)pos.Y);
  112. colorPicker.SelectedBrush = new SolidColorBrush(Color.FromArgb(colorD.A, colorD.R, colorD.G, colorD.B));
  113. DeleteObject(bgBitmap.GetHbitmap());
  114. this.Close();
  115. ClickColorPickerToggleButton(colorPicker);
  116. }
  117. public void ClickColorPickerToggleButton(ColorPicker colorPicker)
  118. {
  119. const BindingFlags InstanceBindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  120. Type type = colorPicker.GetType();
  121. FieldInfo fi = type.GetField("_toggleButtonDropper", InstanceBindFlags);
  122. ToggleButton tb = (ToggleButton)fi.GetValue(colorPicker);
  123. if (tb != null && tb.IsChecked == true)
  124. {
  125. tb.IsChecked = false;
  126. MethodInfo mi = type.GetMethod("ToggleButtonDropper_Click", InstanceBindFlags);
  127. mi.Invoke(colorPicker, new object[] { null, null });
  128. }
  129. }
  130. private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
  131. {
  132. SetPixelAbout(e);
  133. }
  134. [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  135. public static extern bool DeleteObject(IntPtr onj);
  136. private void SetPixelAbout(MouseEventArgs e)
  137. {
  138. VisualBrush b = (VisualBrush)PixelBG.Fill;
  139. Point pos;
  140. if (e == null)
  141. {
  142. pos = MouseUtil.GetMousePosition();
  143. }
  144. else
  145. {
  146. pos = e.MouseDevice.GetPosition(DesktopBG);
  147. }
  148. Rect viewBox = b.Viewbox;
  149. viewBox.Width = PIXEL_REC_LENGTH;
  150. viewBox.Height = PIXEL_REC_LENGTH;
  151. viewBox.X = pos.X - PIXEL_REC_LENGTH / 2;
  152. viewBox.Y = pos.Y - PIXEL_REC_LENGTH / 2;
  153. b.Viewbox = viewBox;
  154. double x = pos.X + 10;
  155. double y = pos.Y + 10;
  156. if (x + ColorCanvas.Width > SystemParameters.VirtualScreenWidth)
  157. {
  158. x = pos.X - ColorCanvas.Width - 10;
  159. }
  160. if (y + ColorCanvas.Height > SystemParameters.VirtualScreenHeight)
  161. {
  162. y = pos.Y - ColorCanvas.Height - 10;
  163. }
  164. Canvas.SetLeft(ColorCanvas, x);
  165. Canvas.SetTop(ColorCanvas, y);
  166. System.Drawing.Color dColor = bgBitmap.GetPixel((int)pos.X, (int)pos.Y);
  167. PixelColor_HTML.Text = "#" + dColor.Name.ToUpper();
  168. PixelColor_RGB.Text = dColor.R + "," + dColor.G + "," + dColor.B;
  169. Pixel_XY.Text = pos.X + "*" + pos.Y;
  170. SolidColorBrush scb = (SolidColorBrush)PixelColor.Fill;
  171. scb.Color = Color.FromArgb(dColor.A, dColor.R, dColor.G, dColor.B);
  172. }
  173. /// <summary>
  174. /// 滚轮控制缩放区域
  175. /// </summary>
  176. /// <param name="sender"></param>
  177. /// <param name="e"></param>
  178. private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
  179. {
  180. if (e.Delta < 0 && PIXEL_REC_LENGTH < MAX_LENGTH)
  181. {
  182. //缩小
  183. PIXEL_REC_LENGTH += 5;
  184. }
  185. else if (e.Delta > 0 && PIXEL_REC_LENGTH > MIN_LENGTH)
  186. {
  187. //放大
  188. PIXEL_REC_LENGTH -= 5;
  189. }
  190. SetPixelAbout(e);
  191. }
  192. /// <summary>
  193. /// 右键按下
  194. /// </summary>
  195. /// <param name="sender"></param>
  196. /// <param name="e"></param>
  197. private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  198. {
  199. Mouse.OverrideCursor = null;
  200. GlobalColorPickerWindow.ShowOrHide();
  201. //关闭
  202. this.Close();
  203. }
  204. }
  205. }