WPF如何获取鼠标坐标的颜色RGB
c# WPF 怎么获取 鼠标坐标的颜色RGB 找了半年了都找不到
GDI+的CopyScreen就可以了.
namespace CS { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { [DllImport("gdi32")] private static extern int GetPixel(int hdc, int nXPos,int nYPos); [DllImport("user32")] private static extern int GetWindowDC(int hwnd); [DllImport("user32")] private static extern int ReleaseDC(int hWnd, int hDC);
private SolidColorBrush GetPixelColor(Point point) { int lDC = GetWindowDC( 0 ); int intColor = GetPixel( lDC, (int)point.X, (int)point.Y ); ReleaseDC( 0, lDC ); byte b = (byte)( ( intColor >> 0x10 ) & 0xffL ); byte g = (byte)( ( intColor >> 8 ) & 0xffL ); byte r = (byte)( intColor & 0xffL ); Color color = Color.FromRgb( r, g, b ); textBox1.Text =b.ToString()+" "+g.ToString()+" "+r.ToString(); return new SolidColorBrush(color); } public MainWindow() { InitializeComponent();
}
public byte b;
public static double MousePositionX { get; set; }
public static double MousePositionY { get; set; }
private void Container_MouseMove(object sender, MouseEventArgs e) { rectangle1.Fill = GetPixelColor(e.GetPosition(null)); }
} }
GetPixcelColor要求传入的参数是屏幕坐标,要转换下: private void Container_MouseMove(object sender, MouseEventArgs e) { var pos = e.GetPosition(null); pos = this.PointToScreen(pos); rectangle1.Fill = GetPixelColor(pos); }
|