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

來源:互聯網
上載者:User

介面如下:

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);    //直接調剪貼簿過來寫值            }        }    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.