using System.Drawing;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
class GetTopColor
{
[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
public static extern IntPtr DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hObject);
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/) [DllImport("gdi32.dll", EntryPoint = "BitBlt")]
public static extern bool BitBlt(IntPtr hdcDest, int nXDest,
int nYDest, int nWidth, int nHeight, IntPtr hdcSrc,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
int nWidth, int nHeight);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobjBmp);
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int nIndex);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
public static Bitmap GetDesktop()
{
int screenX;
int screenY;
IntPtr hBmp;
IntPtr hdcScreen = GetDC(GetDesktopWindow());
IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen);
screenX = GetSystemMetrics(0);
screenY = GetSystemMetrics(1);
hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY);
if (hBmp != IntPtr.Zero)
{
IntPtr hOldBmp = (IntPtr)SelectObject(hdcCompatible, hBmp);
BitBlt(hdcCompatible, 0, 0, screenX, screenY, hdcScreen, 0, 0, 13369376);
SelectObject(hdcCompatible, hOldBmp);
DeleteDC(hdcCompatible);
ReleaseDC(GetDesktopWindow(), hdcScreen);
Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);
DeleteObject(hBmp);
GC.Collect();
return bmp;
}
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/) return null;
}
}
}
//程序应用
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
public static extern int GetCursorPos(
ref POINTAPI lpPoint
);
[StructLayout(LayoutKind.Sequential)]//定义与API相兼容结构体,实际上是一种内存转换
public struct POINTAPI
{
public int X;
public int Y;
}
private void timer1_Tick(object sender, EventArgs e)
{
POINTAPI point = new POINTAPI();//非托管内存坐标结构体
int r = GetCursorPos(ref point);//获取坐标
using (Bitmap myBitmap = GetTopColor.GetDesktop())//将桌面保存到myBitmap中去,其实有点类似桌面截图了,当然仍旧对DirectX没有办法
{
Color myColor = myBitmap.GetPixel(
point.X, point.Y);//取指定坐标点的颜色
this.textBox1.Text = point.X.ToString() + " : " + point.Y.ToString();
this.textBox2.Text = myColor.ToString();
}
}
}
}