一個獲取rgb的小程序,有興趣的可以交流交流,好多地方還不夠完美,不過功能可以實現(C# winForm)


界面如下:

4個label,4個textBox,一個pictureBox

 

啟動之后RGB文本框獲取焦點,移動鼠標pictureBox會顯示當前鼠標經過的地方的顏色,且幾個文本框的數值也會跟着變化,

timer的tick事件時間給的短(10),這樣就效果能好點,跟着鼠標,實時變化。

 

代碼不多,全貼出來,注釋我加了,那個取色方法還沒來得及看怎么搞的,懂的幫忙加加注釋  哈哈

里面注釋有我的問題,高手也請不要吝嗇,多多提拔我們晚輩!

 

全部代碼
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;
using System.Threading;

namespace RGBGetter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //取色方法,網上找的代碼,還不是很懂  哈哈
        private static Color GetScrPixel(Point pt)  
         {  
             var scrBound = Screen.PrimaryScreen.Bounds;  
             using (var bmp = new Bitmap(scrBound.Width, scrBound.Height))  
             {  
                 using (var g = Graphics.FromImage(bmp))  
                 {  
                     g.CopyFromScreen(scrBound.Location,scrBound.Location,scrBound.Size);
                 }  
                 System.GC.Collect();  
                 return bmp.GetPixel(pt.X, pt.Y);  
             }  
         }  

        //啟動timer,rgb文本框獲取焦點
        //rgb文本框獲取焦點是為了下面RGB_KeyDown事件里面復制做鋪墊
        //復制是寫到了rgb文本框獲取焦點時候按下Ctrl+c 復制的,我寫到winForm的KeyDown事件里面不行...還請指點
        private void Form1_Load(object sender, EventArgs e)
        {
            //這個本來是想做一個最大化給整個桌面撲一層膜,這樣焦點就一直在rgb文本框了,
            //不然點別的地方,失去焦點,就不能復制了,還得先獲取焦點再取色,有點麻煩...
            //時間有限,先做到這,以后有機會改善
            //Form1 frm1 = new Form1();
            //WindowState = FormWindowState.Maximized;

            timer1.Enabled = true;
            timer1.Interval = 10;
            timer1.Start();
            RGB.Select();       //本來直接focus()就可以,但為什么不行呢,還得加上select(),加上select(),focus()也就不用了...
            //RGB.Focus();
        }

        //取色
        private void timer_Tick(object sender, EventArgs e)  
       {

            RGB.Focus();        //這個focus()是為了在每個timer_tick時候保證rgb文本框都能獲取焦點
            int r, g, b;  
            Point p = Control.MousePosition;    //得到當前鼠標坐標 
            Color c = GetScrPixel(p);           //取色方法,傳參p 當前坐標
            r =c.R;  
            g = c.G;  
            b = c.B;  
            Red.Text = r+"";  
            Green.Text = g + "";  
            Blue.Text = b+"";  
            string res = "#" + check(Convert.ToString(r, 16)) +check( Convert.ToString(g, 16)) + check(Convert.ToString(b, 16));  //rgb文本框寫的內容
            RGB.Text = res.ToUpper();
            pictureBox1.BackColor = c;      //pictureBox  顯示當前鼠標位置的顏色
            System.GC.Collect();        //內存垃圾回收
       }  

        //這個好理解,如果是0  就給文本框顯示兩個0,如果不為0 就返回rgb
        private string check(string arg)  
       {  
            if (arg.Equals("0"))  
                return "00";  
            else 
                 return arg;  
        }

        //做的一個復制功能,兩個方法都可以實現,寫一個就行
        private void RGB_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control) && e.KeyCode == Keys.C)
            {
                RGB.Copy();         //copy()方法  復制到剪貼板
                Clipboard.SetText(RGB.Text);    //直接調剪貼板過來寫值
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM