我有一个WinForms应用程序。在我的应用程序内部我有一个面板,该面板应该根据我的光标所在地变成不同的颜色。我希望能够将光标放在我的应用程序之外,并且仍然能够找到颜色。
这是我到目前为止所做的:
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
Point p = Control.MousePosition;
IntPtr dc = GetDC(GetDesktopWindow());
private void timer1_Tick(object sender, EventArgs e)
{
Color c = ColorTranslator.FromWin32(GetPixel(dc, p.X, p.Y));
panel1.BackColor = c;
Console.WriteLine(c);
panel1.Refresh();
ReleaseDC(GetDesktopWindow(), dc);
}