首页  编辑  

取得屏幕某一点的颜色

Tags: /C#/API调用/   Date Created:

using System . Runtime . InteropServices;

[ DllImport ( "user32.dll" )]

public static extern IntPtr GetDC( IntPtr hwnd);

[ DllImport ( "user32.dll" )]

public static extern Int32 ReleaseDC( IntPtr hwnd, IntPtr hdc);

[ DllImport ( "gdi32.dll" )]

public static extern uint GetPixel( IntPtr hdc, int nXPos, int nYPos);

private void button1_Click( object sender , EventArgs e)

{

    IntPtr vDC = GetDC( IntPtr . Zero);

    int vPixel = ( int )GetPixel(vDC, Cursor . Position . X, Cursor . Position . Y);

    Color vColor = Color . FromArgb(

       (vPixel & 0x000000FF ) >> 0 ,

       (vPixel & 0x0000FF00 ) >> 8 ,

       (vPixel & 0x00FF0000 ) >> 16 );

   ReleaseDC( IntPtr . Zero, vDC);

   button1 . ForeColor = vColor;

}