C#取指定窗口指定点颜色

再立一新坟

 

界四一类

 

//必要的空间声明
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.ComponentModel;
using System.Data;

//必要的API声明
[DllImport("user32.dll")]//取设备场景
private static extern IntPtr GetDC(IntPtr hwnd);//返回设备场景句柄
[DllImport("gdi32.dll")]//取指定点颜色
private static extern int GetPixel(IntPtr hdc, Point p);
[System.Runtime.InteropServices.DllImport("User32.dll")]
static extern int ReleaseDC( IntPtr hWnd,  IntPtr hDC);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

//必要的常量
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); //窗体置顶
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); //取消窗体置顶
public const uint SWP_NOMOVE = 0x0002; //不调整窗体位置
public const uint SWP_NOSIZE = 0x0001; //不调整窗体大小
//取色
public List<int> qs(IntPtr ID,int x,int y)
{
	List<int> result=new List<int> ();
	Point p=new Point (x,y);
	//窗口置顶
	SetWindowPos(ID, HWND_TOPMOST, 1, 1, 1, 1, SWP_NOMOVE | SWP_NOSIZE);
	IntPtr hdc = GetDC(ID);//取到设备场景(0就是全屏的设备场景)
	int c = GetPixel(hdc, p);//取指定点颜色
	//窗口取消置顶
	SetWindowPos(ID, HWND_NOTOPMOST, 1, 1, 1, 1, SWP_NOMOVE | SWP_NOSIZE);
	int r = (c & 0xFF);//转换R
	int g = (c & 0xFF00) / 256;//转换G
	int b = (c & 0xFF0000) / 65536;//转换B
	result.Add (r);
	result .Add (g);
	result .Add (b);
	ReleaseDC(IntPtr.Zero, hdc);
	return result;
	
}


 

你可能感兴趣的:(C#取指定窗口指定点颜色)