C#取鼠标点处颜色RGB

2016-02-19 12:26 48 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);

        [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);

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

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

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

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

延伸阅读
   1 绪论 c# 是一种简练,时髦(?),面向对象(object oriented),类型可靠(type-safe)的 编程语言。它(发音:C sharp)是从c/c++发展而来的(?俺觉得更象是java),和c/c++ 是一个语系。所以,很容易被c/c++的程序员接受。c#的目标是结合Visual Basic的高产和 C++质朴的力量。 c#将会是vs7的一分子。vs7还支持vb,vc和...
About program language such as C++, C#, Java and Delphi, how to choose a good one for a freshman ? This view has pointed by many people here and different person have different ideas. In my opnion, , C# is the first choose for anyone. Why? Because it's different from any other language. C# comes from C++ and Java i...
标签: 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...

经验教程

615

收藏

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