0

I have a WinForms application. Inside my application i have a panel and that panel is supposed to turn into a different color based on where my cursor is. I want to be able to have the ability to place my cursor outside my application and still be able to find the color.

This is what i have so far:

    [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);
    }
taji01
  • 2,527
  • 8
  • 36
  • 80
  • @Taw Hi, thank you for pointing me into the right direction. In this answer here: http://stackoverflow.com/a/1483963/4767245 the programmer wrote DoAction(); and Thread.Sleep(); in the PollPixel method. Do you know why that is needed? and why i get errors when in the DoAction method? – taji01 Feb 07 '17 at 16:24
  • The DoAction simply is a stand-in for whatever you want to do with the color you grab. And the Sleep is only necessary to allow the UI to keep going; the method after all polls continuously. Not sure if you want that; since you already use a Timer I don't think you need it. So drop the`while (true)..` and call it in your `Tick`! – TaW Feb 07 '17 at 17:57

0 Answers0