C#获取屏幕鼠标坐标点颜色

[DllImport( "user32.dll" )]
        private  static extern IntPtr GetDC(IntPtr hwnd);
 
        [DllImport( "gdi32.dll" )]
        private  static extern int GetPixel(IntPtr hdc,Point p);
 
        public  static Color getColor(Point p)
        {
 
            // 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);
            return  Color.FromArgb(r,b);
 
        }

  测试例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private  void button1_Click(object sender,EventArgs e)
{
     //测试X在200,Y在120 到500 的颜是否不等于 Color.FromArgb(255,246,246);
     string  d = DateTime.Now.ToLongTimeString();
     Color cl = Color.FromArgb(255,246);
     Point p =  new  Point(200,0);
    for  (int h = 120; h < 500; h+=8) {
         p.Y = h;
 
     if (getColor(p).Equals(cl)== false  ){
 
           Text = "" + h;
        break ;
      }
 
     }
     
     Text = d + ":" +  DateTime.Now.ToLongTimeString() + "  " + p ;
 
}

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...