我正在为 C# 中的 ledcube 制作应用程序。它现在所做的只是允许用户单击一些按钮来更改所需 LED 的颜色。我试图暗示一个功能,它允许用户将当前配置(颜色)保存到文件中,以后可以由编程器读取。保存的文件必须包含每个 LED 的二进制代码(LED 是否打开,以及哪种颜色:关闭、红色、绿色、橙色)。我在想 1 个 LED 可以包含 00 - 11 之间的值(所以 0 和 3)。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Button[,] laag_1 = new Button[8, 8];
Button[,] laag_2 = new Button[8, 8];
Button[,] laag_3 = new Button[8, 8];
Button[,] laag_4 = new Button[8, 8];
Button[,] laag_5 = new Button[8, 8];
Button[,] laag_6 = new Button[8, 8];
Button[,] laag_7 = new Button[8, 8];
Button[,] laag_8 = new Button[8, 8];
public Form1()
{
InitializeComponent();
for (int x = 0; x < laag_1.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
laag_1[x, y] = new Button();
laag_1[x, y].SetBounds((50 * x) + 100, (50 * y) + 30, 40, 40);
laag_1[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_1[x, y]);
laag_1[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_2.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
laag_2[x, y] = new Button();
laag_2[x, y].SetBounds((50 * x) + 520, (50 * y) + 30, 40, 40);
laag_2[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_2[x, y]);
laag_2[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_3.GetLength(0); x++)
{
for (int y = 0; y < laag_3.GetLength(1); y++)
{
laag_3[x, y] = new Button();
laag_3[x, y].SetBounds((50 * x) + 940, (50 * y) + 30, 40, 40);
laag_3[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_3[x, y]);
laag_3[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_4.GetLength(0); x++)
{
for (int y = 0; y < laag_4.GetLength(1); y++)
{
laag_4[x, y] = new Button();
laag_4[x, y].SetBounds((50 * x) + 1360, (50 * y) + 30, 40, 40);
laag_4[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_4[x, y]);
laag_4[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_5.GetLength(0); x++)
{
for (int y = 0; y < laag_5.GetLength(1); y++)
{
laag_5[x, y] = new Button();
laag_5[x, y].SetBounds((50 * x) + 100, (50 * y) + 520, 40, 40);
laag_5[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_5[x, y]);
laag_5[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_6.GetLength(0); x++)
{
for (int y = 0; y < laag_6.GetLength(1); y++)
{
laag_6[x, y] = new Button();
laag_6[x, y].SetBounds((50 * x) + 520, (50 * y) + 520, 40, 40);
laag_6[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_6[x, y]);
laag_6[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_7.GetLength(0); x++)
{
for (int y = 0; y < laag_7.GetLength(1); y++)
{
laag_7[x, y] = new Button();
laag_7[x, y].SetBounds((50 * x) + 940, (50 * y) + 520, 40, 40);
laag_7[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_7[x, y]);
laag_7[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_8.GetLength(0); x++)
{
for (int y = 0; y < laag_8.GetLength(1); y++)
{
laag_8[x, y] = new Button();
laag_8[x, y].SetBounds((50 * x) + 1360, (50 * y) + 520, 40, 40);
laag_8[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_8[x, y]);
laag_8[x, y].BackColor = Color.Black;
}
}
this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
LoadFromFile();
}
void btnEvent_click(object sender, EventArgs e)
{
Control ctrl = ((Control)sender);
switch (ctrl.BackColor.Name)
{
case "Red":
ctrl.BackColor = Color.Green;
break;
case "Black":
ctrl.BackColor = Color.Red;
break;
case "Green":
ctrl.BackColor = Color.Yellow;
break;
case "Yellow":
ctrl.BackColor = Color.Black;
break;
default:
ctrl.BackColor = Color.Black;
break;
}
}
void SaveEventHandler(object sender, EventArgs e)
{
SaveToFile();
}
private const string filePath = @"C:\testmap\laag_1.txt";
private void LoadFromFile()
{
if (!System.IO.File.Exists(filePath))
return;
byte[] data = System.IO.File.ReadAllBytes(filePath);
if (data == null || data.Length != laag_1.GetLength(0) * laag_1.GetLength(1) * 2)
return;
for (int x = 0; x < laag_1.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
int position = (y * laag_1.GetLength(0) + x);
string value = ((char)data[2 * position]).ToString() + ((char)data[2 * position + 1]).ToString();
Color color;
switch (value)
{
case "01":
color = Color.Red;
break;
case "00":
color = Color.Black;
break;
case "10":
color = Color.Green;
break;
case "11":
color = Color.Yellow;
break;
default:
color = Color.Black;
break;
}
laag_1[x, y].BackColor = color;
}
}
}
private void SaveToFile()
{
Dictionary<Form1, int> d = new Dictionary<Form1, int>();
byte[] data = new byte[laag_1.GetLength(0) * laag_1.GetLength(1) * 2];
for (int x = 0; x < laag_1.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
int position = (y * laag_1.GetLength(0) + x);
string value;
switch (laag_1[x, y].BackColor.Name)
{
case "Red":
value = "01";
break;
case "Black":
value = "00";
break;
case "Green":
value = "10";
break;
case "Yellow":
value = "11";
break;
default:
value = "00";
break;
}
data[2 * position] = (byte)value[0];
data[2 * position + 1] = (byte)value[1];
}
}
System.IO.File.WriteAllBytes(filePath, data);
}
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < laag_1.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
laag_1[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_2.GetLength(0); x++)
{
for (int y = 0; y < laag_2.GetLength(1); y++)
{
laag_2[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_3.GetLength(0); x++)
{
for (int y = 0; y < laag_3.GetLength(1); y++)
{
laag_3[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_4.GetLength(0); x++)
{
for (int y = 0; y < laag_4.GetLength(1); y++)
{
laag_4[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_5.GetLength(0); x++)
{
for (int y = 0; y < laag_5.GetLength(1); y++)
{
laag_5[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_6.GetLength(0); x++)
{
for (int y = 0; y < laag_6.GetLength(1); y++)
{
laag_6[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_7.GetLength(0); x++)
{
for (int y = 0; y < laag_7.GetLength(1); y++)
{
laag_7[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_8.GetLength(0); x++)
{
for (int y = 0; y < laag_8.GetLength(1); y++)
{
laag_8[x, y].BackColor = Color.Black;
}
}
}
}
}