惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
小众软件
小众软件
GbyAI
GbyAI
B
Blog RSS Feed
月光博客
月光博客
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
博客园_首页
G
Google Developers Blog

博客园 - wl666lw

MSSQL清理所有用户数据库日志(SQLSERVER2008) MSSQL一种取代游标的方案 C#直接操作并口 ProtelDXP练习作品-51单片机最小系统 单片机实现的数字钟 C#中使用 SendMessage 向非顶端窗体发送组合键 对非顶端窗口截图 对指定的网页进行截图 C# 查看网络流量信息 删除“拒绝访问”等无法删除的文件 真正彻底的删除硬盘数据 pocket pc 2003网络配置 IIS配置wap服务器 js日期正则表达式 在安装flash player 10时遇到提示“正尝试安装的adobe flash player不是最新版本”的解决方法 利用VB远线程注入技术实现键盘拦截的例子(无DLL) C#颜色和名称对照表 Windows消息大全 VB HOOK(钩子)超级无敌详细用法
C#动态获取鼠标位置的颜色
wl666lw · 2010-09-05 · via 博客园 - wl666lw

API声明:

/// <summary>
/// 获取指定窗口的设备场景
/// </summary>
/// <param name="hwnd">将获取其设备场景的窗口的句柄。若为0,则要获取整个屏幕的DC</param>
/// <returns>指定窗口的设备场景句柄,出错则为0</returns>
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);

/// <summary>
/// 释放由调用GetDC函数获取的指定设备场景
/// </summary>
/// <param name="hwnd">要释放的设备场景相关的窗口句柄</param>
/// <param name="hdc">要释放的设备场景句柄</param>
/// <returns>执行成功为1,否则为0</returns>
[DllImport("user32.dll")]
public static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

/// <summary>
/// 在指定的设备场景中取得一个像素的RGB值
/// </summary>
/// <param name="hdc">一个设备场景的句柄</param>
/// <param name="nXPos">逻辑坐标中要检查的横坐标</param>
/// <param name="nYPos">逻辑坐标中要检查的纵坐标</param>
/// <returns>指定点的颜色</returns>
[DllImport("gdi32.dll")]
public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

使用:
public
Color GetColor(int x, int y)
{
  
IntPtr hdc = GetDC(IntPtr.Zero); uint pixel = GetPixel(hdc, x, y);
   ReleaseDC(
IntPtr.Zero, hdc);
  
Color color = Color.FromArgb((int)(pixel & 0x000000FF), (int)(pixel & 0x0000FF00) >> 8, (int)(pixel & 0x00FF0000) >> 16);
  
return color;
}