C#获取屏幕鼠标所指点的颜色

有时候要获取屏幕某一点的坐标颜色值,可以如下实现:

在VS2012中创建一个C#的Windows窗口应用程序,然后在Form上添加PictureBox和Button两个控件,并加入以下代码。

        //需要这个命名空间:
        //using System.Runtime.InteropServices;

        [DllImport("user32.dll")]//取设备场景
        private static extern IntPtr GetDC(IntPtr hwnd);//返回设备场景句柄
        [DllImport("gdi32.dll")]//取指定点颜色
        private static extern int GetPixel(IntPtr hdc, Point p);

        private void button1_Click(object sender, EventArgs e)
        {
            Timer tim = new Timer();
            tim.Interval = 1;
            tim.Tick += delegate
            {
                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);
            };
            tim.Start();
        }
 

运行后点击Button移动鼠标就能获得屏幕点的坐标了。

时间: 2025-01-02 01:34:10

C#获取屏幕鼠标所指点的颜色的相关文章

用C++获取屏幕上某点的颜色

假定坐标点 x=50,y=50. 输出 RGB 用 16 进制数.Afxwin.h 你建项目时可得.#include <Afxwin.h>#include <Windows.h>#pragma comment (lib, "User32.lib") int main(void){HWND hWnd = ::GetDesktopWindow();HDC hdc = ::GetDC(hWnd);// HDC hdc = ::GetDC(NULL);int x=50,

获取屏幕上某点的像素值

获取屏幕DC.得到当前鼠标所在的像素值.分解出像素值中的红.绿.蓝三色既可 关键代码实现: 1.获取屏幕DC HDC hDC = ::GetDC(NULL); //获取屏幕DC 2.获取当前鼠标位置像素值 CPoint pt;GetCursorPos(&pt); //得到当前鼠标所在位置 COLORREF clr = ::GetPixel(hDC, pt.x, pt.y); //获取当前鼠标点像素值 3.分解出像素点中的红.绿.蓝颜色值 CString ClrText;ClrText.Forma

cocos2d-x JS 获取屏幕大小或中点

以一张背景图为例: var HelloWorldLayer = cc.Layer.extend({ ctor:function () { this._super(); var bg = new cc.Sprite(res.HelloWorld_png); var size = cc.director.getWinSize();//获取屏幕大小 bg.x = size.width / 2; // x轴/2即为x轴中点 bg.y = size.height / 2; // y轴/2即为y轴中点 th

ios 获取屏幕的属性和宽度

1.app尺寸,去掉状态栏 CGRect r = [ UIScreen mainScreen ].applicationFrame; r=0,20,320,460 另外:self.view.bounds.size 2.屏幕尺寸 CGRect rx = [ UIScreen mainScreen ].bounds; r=0,0,320,480 3.状态栏尺寸 CGRect rect; rect = [[UIApplication sharedApplication] statusBarFrame]

javascript获取屏幕的可用宽度和高度

说明 获取屏幕的可用宽度和高度 示例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>显示浏览器窗口的高度和宽度</title> <link rel="styleshee

Android分享笔记(1) 获取屏幕尺寸,包括状态栏

一大波干货来袭,屏幕尺寸各种获得,状态栏尺寸可正确获得,亲测. package com.elyar.app.util.measure; import java.lang.reflect.Field; import android.app.Activity; import android.util.DisplayMetrics; public class DisplayMeasure {  /**   * Note:个人经验不服来辩<br>   * 只有activity可以使用getWindow

wift - 使用UIScreen类获取屏幕大小尺寸

UISreen类代表了屏幕,开发中一般用来获取屏幕相关的属性,例如获取屏幕的大小. 1 2 3 4 5 6 7 //获取屏幕大小 var screenBounds:CGRect = UIScreen.mainScreen().bounds println(screenBounds) //iPhone6输出:(0.0,0.0,375.0,667.0) //获取屏幕大小(不包括状态栏高度) var viewBounds:CGRect = UIScreen.mainScreen().applicati

CSS改变被鼠标选中的文字颜色及背景

页面中的文字被鼠标选中的文字颜色及背景怎么设置? ::selection { background:#e96147;  color:red; } ::-moz-selection { background:#e96147;  color:red; } ::-webkit-selection { background:#e96147;  color:red; } 希望对大家有帮助

Android获取屏幕尺寸

有些时候,我们需要获取Android手机或Pad的屏幕的物理尺寸,以便于界面的设计或是其他功能的实现.下面就介绍讲一讲如何获取屏幕的物理尺寸:从网上找过不少资料,发现获取屏幕尺寸并不是很复杂的编程操作,下面的代码即可获取屏幕的尺寸.在一个Activity的onCreate方法中,写入如下代码: DisplayMetrics metric = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metric)