PixelColorPickerWindow.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. SetProcessDPIAware();
  37. ColorPickerWindow_Init();
  38. }
  39. private void ColorPickerWindow_Init()
  40. {
  41. this.WindowState = WindowState.Normal;//还原窗口(非最小化和最大化)
  42. this.Width = SystemParameters.VirtualScreenWidth;
  43. this.Height = SystemParameters.VirtualScreenHeight;
  44. this.Left = SystemParameters.VirtualScreenLeft;
  45. this.Top = SystemParameters.VirtualScreenTop;
  46. DesktopBG.Width = this.Width;
  47. DesktopBG.Height = this.Height;
  48. this.Topmost = true;
  49. bgBitmap = new System.Drawing.Bitmap(
  50. Screen.AllScreens[0].Bounds.Width,
  51. Screen.AllScreens[0].Bounds.Height,
  52. System.Drawing.Imaging.PixelFormat.Format32bppArgb
  53. );
  54. using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bgBitmap))
  55. {
  56. g.CopyFromScreen(
  57. 0,
  58. 0,
  59. 0,
  60. 0,
  61. bgBitmap.Size
  62. );
  63. }
  64. BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(
  65. bgBitmap.GetHbitmap(),
  66. IntPtr.Zero,
  67. Int32Rect.Empty,
  68. BitmapSizeOptions.FromEmptyOptions()
  69. );
  70. DesktopBG.Source = bs;
  71. VisualBrush b = (VisualBrush)PixelBG.Fill;
  72. b.Visual = DesktopBG;
  73. Mouse.OverrideCursor = Cursors.Cross;
  74. SetPixelAbout(null);
  75. }
  76. public void OnKeyDown(object sender, KeyEventArgs e)
  77. {
  78. if (e.Key == Key.Escape)
  79. {
  80. this.DataContext = null;
  81. this.Close();
  82. }
  83. }
  84. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  85. {
  86. Mouse.OverrideCursor = null;
  87. Point pos = e.MouseDevice.GetPosition(DesktopBG);
  88. System.Drawing.Color colorD = bgBitmap.GetPixel((int)pos.X, (int)pos.Y);
  89. colorPicker.SelectedBrush = new SolidColorBrush(Color.FromArgb(colorD.A, colorD.R, colorD.G, colorD.B));
  90. DeleteObject(bgBitmap.GetHbitmap());
  91. this.Close();
  92. ClickColorPickerToggleButton(colorPicker);
  93. }
  94. public void ClickColorPickerToggleButton(ColorPicker colorPicker)
  95. {
  96. const BindingFlags InstanceBindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  97. Type type = colorPicker.GetType();
  98. FieldInfo fi = type.GetField("_toggleButtonDropper", InstanceBindFlags);
  99. ToggleButton tb = (ToggleButton)fi.GetValue(colorPicker);
  100. if (tb != null && tb.IsChecked == true)
  101. {
  102. tb.IsChecked = false;
  103. MethodInfo mi = type.GetMethod("ToggleButtonDropper_Click", InstanceBindFlags);
  104. mi.Invoke(colorPicker, new object[] { null, null });
  105. }
  106. }
  107. private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
  108. {
  109. SetPixelAbout(e);
  110. }
  111. [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  112. public static extern bool DeleteObject(IntPtr onj);
  113. private void SetPixelAbout(MouseEventArgs e)
  114. {
  115. VisualBrush b = (VisualBrush)PixelBG.Fill;
  116. Point pos;
  117. if (e == null)
  118. {
  119. pos = MouseUtil.GetMousePosition();
  120. }
  121. else
  122. {
  123. pos = e.MouseDevice.GetPosition(DesktopBG);
  124. }
  125. Rect viewBox = b.Viewbox;
  126. viewBox.Width = PIXEL_REC_LENGTH;
  127. viewBox.Height = PIXEL_REC_LENGTH;
  128. viewBox.X = pos.X - PIXEL_REC_LENGTH / 2;
  129. viewBox.Y = pos.Y - PIXEL_REC_LENGTH / 2;
  130. b.Viewbox = viewBox;
  131. double x = pos.X + 10;
  132. double y = pos.Y + 10;
  133. if (x + ColorCanvas.Width > SystemParameters.VirtualScreenWidth)
  134. {
  135. x = pos.X - ColorCanvas.Width - 10;
  136. }
  137. if (y + ColorCanvas.Height > SystemParameters.VirtualScreenHeight)
  138. {
  139. y = pos.Y - ColorCanvas.Height - 10;
  140. }
  141. Canvas.SetLeft(ColorCanvas, x);
  142. Canvas.SetTop(ColorCanvas, y);
  143. System.Drawing.Color dColor = bgBitmap.GetPixel((int)pos.X, (int)pos.Y);
  144. PixelColor_HTML.Text = "#" + dColor.Name.ToUpper();
  145. PixelColor_RGB.Text = dColor.R + "," + dColor.G + "," + dColor.B;
  146. Pixel_XY.Text = pos.X + "*" + pos.Y;
  147. SolidColorBrush scb = (SolidColorBrush)PixelColor.Fill;
  148. scb.Color = Color.FromArgb(dColor.A, dColor.R, dColor.G, dColor.B);
  149. }
  150. /// <summary>
  151. /// 滚轮控制缩放区域
  152. /// </summary>
  153. /// <param name="sender"></param>
  154. /// <param name="e"></param>
  155. private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
  156. {
  157. if (e.Delta < 0 && PIXEL_REC_LENGTH < MAX_LENGTH)
  158. {
  159. //缩小
  160. PIXEL_REC_LENGTH += 5;
  161. }
  162. else if (e.Delta > 0 && PIXEL_REC_LENGTH > MIN_LENGTH)
  163. {
  164. //放大
  165. PIXEL_REC_LENGTH -= 5;
  166. }
  167. SetPixelAbout(e);
  168. }
  169. }
  170. }