一个获取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);    //直接调剪贴板过来写值
            }
        }
    }
}

 

posted on 2012-06-27 09:52  Xw_hacker  阅读(2128)  评论(5编辑  收藏  举报

导航