C#取鼠标点处颜色RGB

磕掉你的牙

磕掉你的牙

2016-02-19 12:26

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

//获取桌面图片类

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

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

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

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

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

展开更多 50%)
分享

猜你喜欢

C#取鼠标点处颜色RGB

编程语言 网络编程
C#取鼠标点处颜色RGB

C#如何取硬件标志

编程语言 网络编程
C#如何取硬件标志

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

C#处理鼠标和键盘事件

编程语言 网络编程
C#处理鼠标和键盘事件

利用Visual C#编程模拟鼠标操作

编程语言 网络编程
利用Visual C#编程模拟鼠标操作

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

photoshop RGB颜色模式技巧

电脑网络
photoshop  RGB颜色模式技巧

展现C#(1):C#简介

ASP
展现C#(1):C#简介

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

如何访问文本文件(3)----用MicrosoftJet访问桌面数据库(五)

如何访问文本文件(3)----用MicrosoftJet访问桌面数据库(五)

AJAX和JSP实现的基于WEB的文件上传的进度控制代码

AJAX和JSP实现的基于WEB的文件上传的进度控制代码
下拉加载更多内容 ↓