mercredi 11 mars 2015

incorrect cursor position for color picker

I tried the code below to get the color that the mouse is on it but the problem that it return with incorrect color that the mouse not on it !! but this color is on another location not what the mouse is above it



private void MouseMoveTimer_Tick(object sender, EventArgs e)
{
var c = GetColorAt(MousePosition.X,MousePosition.Y);
this.BackColor = c;
}
[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 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);
}


any suggestion are most welcome. thanks in advance


Aucun commentaire:

Enregistrer un commentaire