api函數:
1 [DllImport("user32.dll")]//取設備場景 2 private static extern IntPtr GetDC(IntPtr hwnd);//返回設備場景句柄 3 4 [DllImport("gdi32.dll")]//取指定點顏色 5 private static extern int GetPixel(IntPtr hdc, Point p);
主要方法:
Timer tim = new Timer(); tim.Interval = 1; tim.Tick += delegate { Point p = new Point(MousePosition.X, MousePosition.Y);//取置頂點坐標 IntPtr hdc = GetDC(new IntPtr(0));//取到設備場景(0就是全屏的設備場景) int c = GetPixel(hdc, p);//取指定點顏色 int r = (c & 0xFF);//轉換R int g = (c & 0xFF00) / 256;//轉換G int b = (c & 0xFF0000) / 65536;//轉換B pictureBox1.BackColor = Color.FromArgb(r, g, b); }; tim.Start();
效果演示: