【发布时间】:2020-01-23 09:22:47
【问题描述】:
如何才能读取应用程序中某个位置的颜色代码? (红圈)
然后我可以输出richtextbox中的值?
感谢您的帮助。
【问题讨论】:
如何才能读取应用程序中某个位置的颜色代码? (红圈)
然后我可以输出richtextbox中的值?
感谢您的帮助。
【问题讨论】:
您可以使用 GetPixel 函数获取像素的颜色:
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowDC(IntPtr window);
[DllImport("gdi32.dll", SetLastError = true)]
public static extern uint GetPixel(IntPtr dc, int x, int y);
[DllImport("user32.dll", SetLastError = true)]
public static extern int ReleaseDC(IntPtr window, IntPtr dc);
public static Color GetColorAt(int x, int y)
{
IntPtr desk = GetDesktopWindow();
IntPtr dc = GetWindowDC(desk);
int a = (int) GetPixel(dc, x, y);
ReleaseDC(desk, dc);
return Color.FromArgb(255, (a >> 0) & 0xff, (a >> 8) & 0xff, (a >> 16) & 0xff);
}
【讨论】:
textBox1.Text = GetColorAt(x, y).ToString();,但我仍然收到错误消息