欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Winform中实现颜色拾取器获取RGB与16进制颜色程序与源码分享

程序员文章站 2022-05-29 13:18:17
场景 效果 实现 关键代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; ......

场景

效果

Winform中实现颜色拾取器获取RGB与16进制颜色程序与源码分享

 

 

Winform中实现颜色拾取器获取RGB与16进制颜色程序与源码分享

实现

关键代码

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using system.runtime.interopservices;
namespace getcolor
{
    public partial class frmgetcolor : form
    {
        public frmgetcolor()
        {
            initializecomponent();
        }

        #region 定义快捷键
        //如果函数执行成功,返回值不为0。       
        //如果函数执行失败,返回值为0。要得到扩展错误信息,调用getlasterror。        
        [dllimport("user32.dll", setlasterror = true)]
        public static extern bool registerhotkey(
        intptr hwnd,                //要定义热键的窗口的句柄            
        int id,                     //定义热键id(不能与其它id重复)                       
        keymodifiers fsmodifiers,   //标识热键是否在按alt、ctrl、shift、windows等键时才会生效         
        keys vk                     //定义热键的内容            
    );
        [dllimport("user32.dll", setlasterror = true)]
        public static extern bool unregisterhotkey(
            intptr hwnd,                //要取消热键的窗口的句柄            
            int id                      //要取消热键的id            
        );
        //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)        
        [flags()]
        public enum keymodifiers
        {
            none = 0,
            alt = 1,
            ctrl = 2,
            shift = 4,
            windowskey = 8
        }
        #endregion

 

          [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 getcolor(point screenpoint)   
          {   
              intptr displaydc = createdc("display",null,null,intptr.zero);   
              uint   colorref   =   getpixel(displaydc,screenpoint.x,screenpoint.y);   
              deletedc(displaydc);   
              byte   red   =   getrvalue(colorref);   
              byte   green   =   getgvalue(colorref);   
              byte   blue   =   getbvalue(colorref);   
              return color.fromargb(red,   green,   blue);   
          }  

        private void frmgetcolor_load(object sender, eventargs e)
        {
            this.topmost = true;
        }

        private void checkbox1_checkedchanged(object sender, eventargs e)
        {
            if (checkbox1.checked == true)
            {
                this.topmost = true;
            }
            else
            {
                this.topmost = false;
            }
        }

        private void timer1_tick(object sender, eventargs e)
        {
            txtpoint.text=control.mouseposition.x.tostring()+","+control.mouseposition.y.tostring();
            point pt = new point(control.mouseposition.x, control.mouseposition.y);
            color cl = getcolor(pt);
            panel1.backcolor = cl;
            txtrgb.text = cl.r + "," + cl.g + "," + cl.b;
            txtcolor.text = colortranslator.tohtml(cl).tostring();
            registerhotkey(handle, 81, keymodifiers.ctrl, keys.f);
        }

        private void button1_click(object sender, eventargs e)
        {
            application.exit();
        }

     


        protected override void wndproc(ref message m)
        {
            const int wm_hotkey = 0x0312;
            //按快捷键     
            switch (m.msg)
            {
                case wm_hotkey:
                    switch (m.wparam.toint32())
                    {
                        case 81:    //按下的是ctrl+f     
                            clipboard.settext(txtcolor.text.trim());
                            break;
                    }
                    break;
            }
            base.wndproc(ref m);
        }

        private void frmgetcolor_leave(object sender, eventargs e)
        {

        }

        private void frmgetcolor_formclosed(object sender, formclosedeventargs e)
        {
            //注销id号为81的热键设定    
            unregisterhotkey(handle, 81);
        }
    }
}

 

程序与代码下载

公众号“霸道的程序猿”发送“颜色拾取器”