从文本文件中读取二进制文件并将其转换为按钮c#

时间:2017-01-24 08:43:35

标签: c# arrays winforms

我目前正在尝试制作类似于游戏的游戏" solo Noble",其中你有很多球,你需要得到最低分。我目前正在尝试制作一系列黑色&白色按钮,在外部文本文件中以二进制形式成形。我目前有这个:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication9.Properties;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            Marble();
        }
        public void Marble()
        {
            int ButtonWidth = 40;
            int ButtonHeight = 40;
            int Distance = 20;
            int start_x = 10;
            int start_y = 10;
            int y = 0;
            int x = 0;
            int delX = x + (y * 2);

            for (x = 0; x < 8; x++)
            {               
              for (y = 0; y < 8; y++)
              {
                GameButton tmpButton = new GameButton();
                tmpButton.BackColor = Color.Black;
                tmpButton.Top = start_x + (x * ButtonHeight + Distance);
                tmpButton.Left = start_y + (y * ButtonWidth + Distance);
                tmpButton.Width = ButtonWidth;
                tmpButton.Height = ButtonHeight;
                tmpButton.Text = "X: " + x.ToString() + " Y: " + y.ToString();
                tmpButton.MouseUp += TmpButton_MouseUp;
                tmpButton.Row = x;
                tmpButton.Column = y;
                tmpButton.Currentcolor = false;

                if (x == 4 && y == 6) {
                    tmpButton.BackColor = Color.White;
                }                               
                else
                {                      
                    this.Controls.Add(tmpButton);
                }                    
            }
        }
    }

    private void TmpButton_MouseUp(object sender, MouseEventArgs e)
    {
        GameButton Mygamebutton = (GameButton) sender;
        Mygamebutton.Currentcolor = !Mygamebutton.Currentcolor;
        if (Mygamebutton.Currentcolor == true)
        {
            Mygamebutton.BackColor = Color.Black;
        }
        else
        {
            Mygamebutton.BackColor = Color.White;
        }
     }
   }
}

但我想尝试这样的事情:

byte[] fileBytes = File.ReadAllBytes(inputFilename);
StringBuilder sb = new StringBuilder();
foreach(byte b in fileBytes)
{
    sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));  
}
File.WriteAllText(outputFilename, sb.ToString());

我不太确定如何将二进制数组从.txt文件转换为按钮,例如0表示没有按钮,1表示按钮。

1 个答案:

答案 0 :(得分:1)

如果您想要序列化有关按钮的信息,而不是稍后在表单上添加这些按钮,您必须了解,您要在文件中存储的内容:位置(x和y坐标),大小,文本,背景?

private void LoadButtonsInformation()
{
    using (var stream = new MemoryStream(File.ReadAllBytes(@"C:\Projects\info.bin")))
    {
        var serializer = new BinaryFormatter();
        var buttonInformations = (ButtonInformation[]) serializer.Deserialize(stream);

        var buttons= buttonInformations.Select(button => new Button
        {
            Location = new Point(button.X, button.Y),
            Text = button.Text,
            Width = button.Width,
            Height = button.Height
        }).ToArray();

        //add to form
        foreach (var button in buttons)
            Controls.Add(button);
    }
}


private void SaveButtonsInformation(params Button[] buttons)
{
    var buttonsInformation = buttons.Select(button => new ButtonInformation
    {
        X = button.Location.X,
        Y = button.Location.Y,
        Text = button.Text,
        Width = button.Width,
        Height = button.Height
    }).ToArray();

    using (Stream stream = new FileStream(@"C:\Projects\info.bin", FileMode.Create))
    {
        var serializer = new BinaryFormatter();
        serializer.Serialize(stream, buttonsInformation);
    }
}

[Serializable]
public class ButtonInformation
{
    public int X { get; set; }

    public int Y { get; set; }

    public string Text { get; set; }

    public int Width { get; set; }

    public int Height { get; set; }
}