基本信息
源码名称:C# 颜色查看器(抓取电脑任意文件的色值)
源码大小:0.22M
文件格式:.rar
开发语言:C#
更新时间:2019-04-01
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MyHelper.Common;

namespace MyHelper.GetColor
{
    public partial class GetColorForm : Form
    {
        public GetColorForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)   //显示16进制代码颜色
        {
            Color c = this.colorHx16toRGB(this.textBox_16进制.Text);
            this.label_颜色显示.BackColor = c;
            this.textBox_R.Text = c.R.ToString();
            this.textBox_G.Text = c.G.ToString();
            this.textBox_B.Text = c.B.ToString();
            HSLColor hsl = new HSLColor(c);
            this.textBox_H.Text = hsl.Hue   "";
            this.textBox_S.Text = ((int)(hsl.Saturation * 100))   "";
            this.textBox_L.Text = ((int)(hsl.Lightness * 100))   "";
        }

        private void button2_Click(object sender, EventArgs e)   //显示RGB值颜色
        {
            
                Color c=Color.FromArgb(Int32.Parse(this.textBox_R.Text), Int32.Parse(this.textBox_G.Text), Int32.Parse(this.textBox_B.Text));
                this.label_颜色显示.BackColor = c;
                this.textBox_16进制.Text = ColorTranslator.ToHtml(c);
                HSLColor hsl = new HSLColor(c);
                this.textBox_H.Text = hsl.Hue   "";
                this.textBox_S.Text = ((int)(hsl.Saturation * 100))   "";
                this.textBox_L.Text = ((int)(hsl.Lightness * 100))   "";
        }

        private void button3_Click(object sender, EventArgs e)  //选择颜色
        {
            this.colorDialog1.AllowFullOpen = true;
            colorDialog1.AnyColor = true;
            colorDialog1.FullOpen = true;
            colorDialog1.SolidColorOnly = false;
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox_16进制.ForeColor = this.colorDialog1.Color;
                this.label_颜色显示.BackColor = this.colorDialog1.Color;
                // String str1 = this.colorDialog1.Color.Name.ToString();
                // this.textBox1.Text = "#" str1.Substring(2);
                this.textBox_16进制.Text= ColorTranslator.ToHtml(this.colorDialog1.Color);
                this.textBox_R.Text = this.colorDialog1.Color.R.ToString();
                this.textBox_G.Text = this.colorDialog1.Color.G.ToString();
                this.textBox_B.Text = this.colorDialog1.Color.B.ToString();
            }
        }

        private void button_开始_Click(object sender, EventArgs e)  //开始获取
        {
            HotKey.RegisterHotKey(this.Handle, 12455, true, true, false, false, Keys.P);
            this.TopMost = true;
            this.timer1.Start();
            this.button_开始.Enabled = false;
            this.button_停止.Enabled = true;
        }
        private void button_停止_Click(object sender, EventArgs e)
        {
            HotKey.UnregisterHotKey(this.Handle,12455);
            this.timer1.Stop();
            this.TopMost = false;
            this.button_停止.Enabled = false;
            this.button_开始.Enabled = true;
        }
      
        private void Form2_Load(object sender, EventArgs e)
        {
            this.button_停止.Enabled = false;
           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           
            // this.textBox5.Text = "X:"   System.Windows.Forms.Control.MousePosition.X.ToString()   "   Y:"  
            //   System.Windows.Forms.Control.MousePosition.Y.ToString();
            Color sc = this.GetColorOfScreen(System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y);
            this.label_颜色显示.BackColor = sc;
            this.textBox_R.Text = sc.R.ToString();
            this.textBox_G.Text = sc.G.ToString();
            this.textBox_B.Text = sc.B.ToString();
            this.textBox_16进制.Text = ColorTranslator.ToHtml(sc);
            this.textBox_16进制.ForeColor = sc;
            HSLColor hsl = new HSLColor(sc);
            this.textBox_H.Text = hsl.Hue   "";
            this.textBox_S.Text = ((int)(hsl.Saturation * 100)) "";
            this.textBox_L.Text = ((int)(hsl.Lightness * 100))   "";
        }

        [DllImport("gdi32.dll")]
        static public extern uint GetPixel(IntPtr hDC, int XPos, int YPos);

        [DllImport("gdi32.dll")]
        static public extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData);

        [DllImport("gdi32.dll")]
        static public extern bool DeleteDC(IntPtr DC);

        static public byte GetRValue(uint color)
        {
            return (byte)color;
        }

        static public byte GetGValue(uint color)
        {
            return ((byte)(((short)(color)) >> 8));
        }

        static public byte GetBValue(uint color)
        {
            return ((byte)((color) >> 16));
        }

        static public byte GetAValue(uint color)
        {
            return ((byte)((color) >> 24));
        }

        public Color GetColorOfScreen(int x, int y)
        {
            IntPtr displayDC = CreateDC("DISPLAY", null, null, IntPtr.Zero);
            uint colorref = GetPixel(displayDC, x, y);
            DeleteDC(displayDC);
            byte Red = GetRValue(colorref);
            byte Green = GetGValue(colorref);
            byte Blue = GetBValue(colorref);
            return Color.FromArgb(Red, Green, Blue);
        }


        public System.Drawing.Color colorHx16toRGB(string strHxColor)
        {
            try
            {
                if (strHxColor.Length == 0)
                {//如果为空
                    return System.Drawing.Color.FromArgb(0, 0, 0);//设为黑色
                }
                else
                {//转换颜色
                    return System.Drawing.Color.FromArgb(System.Int32.Parse(strHxColor.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
                }
            }
            catch
            {//设为黑色
                return System.Drawing.Color.FromArgb(0, 0, 0);
            }
        }


        private void button7_Click(object sender, EventArgs e)   //根据HSL显示
        {
            HSLColor hsl = new HSLColor(Convert.ToInt32(this.textBox_H.Text), Convert.ToDouble(this.textBox_S.Text) / 100, Convert.ToDouble(this.textBox_L.Text) / 100);
            Color c=hsl.Color;
            this.label_颜色显示.BackColor = c;
            this.textBox_R.Text = c.R "";
            this.textBox_G.Text = c.G   "";
            this.textBox_B.Text = c.B   "";
            this.textBox_16进制.Text = ColorTranslator.ToHtml(c);
        }

        protected override void WndProc(ref Message SystemMessage)
        {//实现在系统关闭前给予用户提示      

            switch (SystemMessage.Msg)
            {
                case 0x0312:
                    {
                        switch (SystemMessage.WParam.ToInt32())
                        {
                            case 12455:
                                {
                                    button_停止_Click(null,null);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                default:
                    base.WndProc(ref SystemMessage);
                    break;
            }

        }

    }
}