.
這支程式算是娛樂用的,不過剛好可以練習一下色碼 R G B 的組成
應用方面可以做成像封面圖一樣的電視牆,分成一格一格的
或是霓虹燈、指定某功能達成時讓畫面變色等,其實應用也蠻多,看需求使用
- 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.Threading;
-
- namespace RandomColor
- {
- public partial class Form1 : Form
- {
- private int _R, _G, _B;
- private Thread _th;
- private string _color_string = "";
- private delegate void updateUI(string str, Control ctl);
- private delegate void updateUI2(Control ctl);
- //private Form1 _F = new Form1();
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- _th = new Thread(randomColor);
- _th.Start();
- }
-
- private void randomColor()
- {
- while(true)
- {
- Random r = new Random(Guid.NewGuid().GetHashCode());
- _R = r.Next(255);
- _G = r.Next(255);
- _B = r.Next(255);
- _color_string = _R + ","+ _G + "," + _B;
- updateForm(this);
- updateTextBox(_color_string + "\n", richTextBox1);
- Thread.Sleep(500);
- }
- }
-
- private void updateForm(Control ctl)
- {
- if (ctl.InvokeRequired)
- {
- updateUI2 ui = new updateUI2(updateForm);
- ctl.Invoke(ui, ctl);
- }
- else
- {
- ctl.BackColor = Color.FromArgb(_R, _G, _B);
- }
- }
-
-
- private void updateTextBox(string str, Control ctl)
- {
- if(ctl.InvokeRequired)
- {
- updateUI ui = new updateUI(updateTextBox);
- ctl.Invoke(ui, str, ctl);
- }
- else
- {
- ctl.Text = str;
- }
- }
-
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- _th.Abort();
- _th.Join();
- }
-
-
- }
- }
執行結果
每秒隨機生成一種顏色,並將色碼成現在畫面上