PixelColorPickerWindow.xaml.cs 7.3 KB

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