c# 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, g, b);

        }

  測試例子:

        private void button1_Click(object sender, EventArgs e)
        {
            //測試X在200,Y在120 到500 的顏是否不等於 Color.FromArgb(255, 246, 246, 246);
            string d = DateTime.Now.ToLongTimeString();
            Color cl = Color.FromArgb(255, 246, 246, 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 ;

        }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM