C#取鼠标点处颜色RGB

2016-02-19 12:26 42 1 收藏

最近很多朋友喜欢上设计,但是大家却不知道如何去做,别担心有图老师给你解答,史上最全最棒的详细解说让你一看就懂。

【 tulaoshi.com - 编程语言 】

//获取桌面图片类

using System;
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);

(本文来源于图老师网站,更多请访问https://www.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);

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

        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;
            }

            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();
            }
        }
    }
}

来源:https://www.tulaoshi.com/n/20160219/1600895.html

延伸阅读
在过去的二十年里,C和C++已经成为在商业软件的开发领域中使用最广泛的语言。它们为程序员提供了十分灵活的操作,不过同时也牺牲了一定的效率。与诸如Microsoft? Visual Basic? 等语言相比,同等级别的C/C++应用程序往往需要更长时间来开发。由于C/C++语言的复杂性,许多程序员都试图寻找一种新的语言,希望能在功能与效率之间找到一个更...
华华鼠标点击器怎么使用?   打开华华鼠标自动点击器,设置定时点击事件。点击确定。 还能添加多坐标点同时点击,设置确定即可。 到底有多方便还要等你亲自使用才发现哦~         注 :更多精彩教程请关注图老师电脑教程栏目
标签: flash教程
前面基础篇里我们讨论了怎么设置指定的颜色,我觉得不能算是很难的。但是一位朋友通过qq告诉我,他觉得有一点吃力。对此我很抱歉,以前我一直觉得有些ction的基础问题是不用多解释的,大家查查参考手册就是,现在看来朋友们的实际情况还是差别很大的,以后我会在这些方面注意一些,每一个新语句都加上注释。 上次说得是颜色设置的基础...
状态模式主要解决当控制一个对象状态的转换的条件表达过于复杂的情况,使得状态的转换不依赖于整体的操作。本文将通过一个具体的例子说明状态模式的应用。假设下面一个场景:      一个新任务提交后,先是收集数据,数据收集完成后等等分配一台机器,分配到机器后就可以将此任务部署至此机器后就可以通知相关模块开始工作...
What is a GUID For those of you who don''t know, a GUID (pronounced goo''id - Globally unique identifier) is a 128-bit integer that can be used to uniquely identify something. You may store users or products in your database and you want somehow uniquely identify each row in the database. A common approach is to crea...

经验教程

817

收藏

18
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部