Winform in real time and display the RGB color picker hexadecimal color (with Codes)

Scenes

effect

 

 

Note:

Blog home page:
https://blog.csdn.net/badao_liumang_qizhi
public concern number of
programs overbearing ape
acquisition-related programming e-books, tutorials and push for free download.

achieve

Create a form page design layout follows

 

 

Add a Timer on page

Then modify the code for this page

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 定义快捷键
        //If the function succeeds, the value returned is not zero.       
        // If the function fails, the return value is zero. To get extended error information, call GetLastError.        
        [The DllImport ( " User32.dll " , the SetLastError = to true )]
         public  static  extern  BOOL RegisterHotKey ( 
        IntPtr the hWnd,                 // handle to define a hotkey window             
        int ID,                      // define a hotkey ID (can not be duplicated with other ID)                        
        KeyModifiers fsModifiers,    // identifies whether hotkey take effect when you press Alt, Ctrl, Shift, Windows and other key          
        keys VK                      // content defined hotkeys             
     );
        [DllImport ( " User32.dll " , the SetLastError = to true )]
         public  static  extern  BOOL UnregisterHotKey ( 
            IntPtr the hWnd,                 // to cancel hot key window handle             
            int ID                       // To cancel a hot key ID             
        );
         // defines auxiliary name of the key (character into a digital memory in order, may also be used directly remove this enumeration value)         
        [the Flags ()]
         public  enum KeyModifiers 
        { 
            None = 0 , 
            the Alt = . 1 , 
            the 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();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.ShowDialog();
        }

        private void label3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.mrbccd.com");
        }
        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(objectSENDER, EventArgs E) 
        { 

        }

        Private  void FrmGetColor_FormClosed ( Object SENDER, FormClosedEventArgs E) 
        { 
            // cancellation Id No. 81 as a hot key set     
            UnregisterHotKey (the Handle, 81 ); 
        } 
    } 
}

 

Download

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12243099

Guess you like

Origin www.cnblogs.com/badaoliumangqizhi/p/12467231.html