C# uses code to set custom colors in the background, get any point color of the screen, color value conversion, clipboard setting value, tabcontorl option setting

label.BackColor = Color.FromName("rgb string(25,25,26)");//Set the background with a custom color in the background

Validation and conversion of hexadecimal color values ​​to rgb:

if (Regex.IsMatch(txt, "^([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$"))//txt不包含#号
        {
                    isox = false;//Is it a hexadecimal color value
                    int r = Convert.ToInt32(txt.Substring(0,2),16);
                    int g = Convert.ToInt32(txt.Substring(2, 2), 16);
                    int b = Convert.ToInt32(txt.Substring(4, 2), 16);
                    return "("+r+","+g+","+b+")";
                }

Convert rgb color values ​​to hex:

if ((Regex.IsMatch(txt_r.Text.Trim(),"^(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|[0-9])$")&& Regex.IsMatch(txt_g.Text.Trim(), "^(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|[0-9])$")&& Regex.IsMatch(txt_b.Text.Trim(), "^(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|[0-9])$")))
                    {//Three sets of values
                        isox = true;
                        string r = Convert.ToInt32(txt_r.Text.Trim()).ToString("X2");
                        string g = Convert.ToInt32(txt_g.Text.Trim()).ToString("X2");
                        string b = Convert.ToInt32(txt_b.Text.Trim()).ToString("X2");
                        toolStripStatusLabel5.Text = "rgb("+ txt_r.Text.Trim()+","+ txt_g.Text.Trim()+","+ txt_b.Text.Trim() + ") color value conversion completed!";
                        return "#" + r + g + b;
                    }

The problem of getting the value of the color dialog:

if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                lbl_x_yansezhi.Text = colorDialog1.Color.R + "," + colorDialog1.Color.G + "," + colorDialog1.Color.B;
            }

Use the windows api to get the color value of any point on the screen (sorted by Internet search):

[DllImport("user32.dll")]//Get device scene
        private static extern IntPtr GetDC(IntPtr hwnd);//Return the handle of the device scene
        [DllImport("gdi32.dll")]//Get the specified point color
        private static extern int GetPixel(IntPtr hdc, Point p);
        private void timer1_Tick(object sender, EventArgs e)
        {
            Point p = new Point(MousePosition.X, MousePosition.Y);//Get vertex coordinates
            IntPtr hdc = GetDC(new IntPtr(0));//Get the device scene (0 is the full screen device scene)
            int c = GetPixel(hdc, p);//Get the color of the specified point
            int r = (c & 0xFF);//Convert R
            int g = (c & 0xFF00) / 256;//Convert G
            int b = (c & 0xFF0000) / 65536;//Convert B
           pal_z_yanse.BackColor = Color.FromArgb(r, g, b);
            lbl_z_rgb.Text = r + "," + g + "," + b;
            string rr = Convert.ToInt32(r).ToString("X2");
            string gg = Convert.ToInt32(g).ToString("X2");
            string bb = Convert.ToInt32(b).ToString("X2");
            lbl_z_ox.Text = "#" + rr + gg + bb;//The color value obtained
        }

Window top property: TopMost = true;

The tabcontrol control adjusts the currently displayed options window:

 this.tabControl1.SelectedIndex = index;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649456&siteId=291194637