⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 settingform.cs

📁 陈广老师讲课的源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyTetris
{
    public partial class SettingForm : Form
    {
        private bool[,] struArr = new bool[5, 5];
        private Color blockColor = Color.Red;
        private Config config = new Config();
        private MainForm mf;

        public SettingForm(MainForm mf)
        {
            InitializeComponent();
            this.mf=mf;
        }      

        private void SettingForm_Load(object sender, EventArgs e)
        {
            config.LoadFromXml();   //读取xml文件
            BlockInfoArr info = config.Info;
               //读方块样式并加入到listView
            ListViewItem lvi=new ListViewItem();
            for (int i = 0; i < info.Length;i++ )
            {
                lvi = listVBlock.Items.Add(info[i].GetIdStr());
                lvi.SubItems.Add(info[i].GetColorStr());
            }
            
            //读取按键信息
            txtLeft.Text = ((Keys)config.LeftKey).ToString();
            txtLeft.Tag = config.LeftKey;
            txtRight.Text = ((Keys)config.RightKey).ToString();
            txtRight.Tag = config.RightKey;
            txtDown.Text = ((Keys)config.DownKey).ToString();
            txtDown.Tag = config.DownKey;
            txtDrop.Text = ((Keys)config.DropKey).ToString();
            txtDrop.Tag = config.DropKey;
            txtDeasil.Text=((Keys)config.DeasilKey).ToString();
            txtDeasil.Tag=((Keys)config.DeasilKey).ToString();
            txtDiseconomics.Text=((Keys)config.DiseconomicsKey).ToString();
            txtDiseconomics.Tag=config.DiseconomicsKey;

            //读取环境信息
            txtHGrid.Text=config.CoorWidth.ToString();
            txtVGrid.Text=config.CoorHeight.ToString();
            txtGridPix.Text=config.RectPix.ToString();

        }

        private void labMode_Paint(object sender, PaintEventArgs e)
        {
            Graphics gra = e.Graphics;
            gra.Clear(Color.Black);
            Pen pen = new Pen(Color.White);
            for (int i = 31; i < 156;i+=31 )  //画横线
            {
                gra.DrawLine(pen,1,i,155,i);
            }
            for (int i = 31; i < 156; i += 31)  //画竖线
            {
                gra.DrawLine(pen, i, 1, i, 155);
            }
            //填充方块
            SolidBrush sb = new SolidBrush(blockColor);
            for (int i = 0; i < 5;i++ )
            {
                for (int j = 0; j < 5;j++ )
                {
                    if (struArr[i, j])
                        gra.FillRectangle(sb, 31 * i + 1, 31 * j+1, 30, 30);
                }
            }
        }

        private void labMode_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)      
                return;
            int xPos, yPos;     //表示数组下标
            xPos = e.X / 31;    //将屏幕坐标转换成数组下标
            yPos = e.Y / 31;
            struArr[xPos,yPos]=!struArr[xPos,yPos];
            bool b = struArr[xPos, yPos];
            Graphics gra = labMode.CreateGraphics();
            SolidBrush sb = new SolidBrush(b?blockColor:Color.Black);   //创建刷子并为之加颜色
            gra.FillRectangle(sb,31*xPos+1,31*yPos+1,30,30);  //为点击的方块加色
            gra.Dispose();  // 释放画布的资源
        }

        private void labColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();      //打开颜色对话框
            blockColor = colorDialog1.Color;
            labColor.BackColor = colorDialog1.Color;
            labMode.Invalidate();   
        }

        private void btnAddBlock_Click(object sender, EventArgs e)
        {
            bool isEmpty = false;   // 查找图案是否为空
            foreach(bool i in struArr)
            {
                if (i)
                {
                    isEmpty = true;
                    break;
                }
            }
            if (!isEmpty)
            {
                MessageBox.Show("方块图案为空,请用鼠标点击左边绘图窗口绘制方块!","提示窗口",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }

            DialogResult result;
            result = MessageBox.Show("你确认要添加方块信息?", "提示窗口", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.Cancel)
                return;

            StringBuilder sb = new StringBuilder(25);
            foreach(bool i in struArr)
            {
                sb.Append(i?"1":"0");
            }
            string blockString = sb.ToString();
            //
            foreach (ListViewItem item in listVBlock.Items)
            {
                if(item.SubItems[0].Text==blockString)
                {
                    MessageBox.Show("该方块已经存在!","提示窗口",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
            }

            //添加新方块到listView
            ListViewItem newItem = new ListViewItem();
            newItem = listVBlock.Items.Add(blockString);
            newItem.SubItems.Add(Convert.ToString(blockColor.ToArgb()));
        }

        private void listVBlock_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if(e.IsSelected)    //避免重复执行
            {
                blockColor = Color.FromArgb(int.Parse(e.Item.SubItems[1].Text));    //将字符串信息转换成颜色
                labColor.BackColor = blockColor;
                string s = e.Item.SubItems[0].Text;     //获取方块图案字符串信息
                for (int i = 0; i < s.Length;i++ )      //将方块的字符串信息转换成二维数组的信息
                {
                    struArr[i / 5, i % 5] = (s[i] == '1') ? true : false;
                }
                labMode.Invalidate();
            }
        }

        private void btnDelBlock_Click(object sender, EventArgs e)
        {
            if(listVBlock.SelectedItems.Count==0)   //判断是否有选项选中
            {
                MessageBox.Show("请你在右边列表中选择要删除的方块!", "提示窗口", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult result;
            result=MessageBox.Show("你确认要删除所选的方块吗?","提示窗口",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
            if (result == DialogResult.Cancel)
                return;
            listVBlock.Items.Remove(listVBlock.SelectedItems[0]);
            btnClearLabMode.PerformClick();
        }

        private void btnClearLabMode_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    struArr[i, j] = false;
                }
            }
            labMode.Invalidate();
        }

        private void btnUpdateBlock_Click(object sender, EventArgs e)
        {
            if (listVBlock.SelectedItems.Count == 0)   //判断是否有选项选中
            {
                MessageBox.Show("请你在右边列表中选择要修改的方块!", "提示窗口", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }            

            bool isEmpty = false;   // 查找图案是否为空
            foreach (bool i in struArr)
            {
                if (i)
                {
                    isEmpty = true;
                    break;
                }
            }
            if (!isEmpty)
            {
                MessageBox.Show("方块图案为空,请用鼠标点击右边列表选择方块!", "提示窗口", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DialogResult result;
            result = MessageBox.Show("你确认要修改所选的方块吗?", "提示窗口", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.Cancel)
                return;

            StringBuilder sb = new StringBuilder(25);
            foreach(bool i in struArr)      //将方块图案信息存成字符串
            {
                sb.Append(i?"1":"0");
            }
            listVBlock.SelectedItems[0].SubItems[0].Text = sb.ToString();   //将改变后的方块样式放到列表中
            listVBlock.SelectedItems[0].SubItems[1].Text = Convert.ToString(blockColor.ToArgb());   //将改变后的方块颜色放到列表中


        }

        private void txtDiseconomics_KeyDown(object sender, KeyEventArgs e)
        {
            //屏蔽一些功能键(不适用的键)
            if ((e.KeyValue >= 37 && e.KeyValue <= 40) || (e.KeyValue >= 48 && e.KeyValue <= 57) || (e.KeyValue >= 65 && e.KeyValue <= 90) )
            {
                try
                {
                    foreach (Control c in gbKeySet.Controls)        //取消重复的按键
                    {
                        Control tempC = c as TextBox;
                        if (tempC != null && ((TextBox)tempC).Text != "")
                        {

                            if (((int)((TextBox)tempC).Tag) == e.KeyValue)
                            {
                                ((TextBox)tempC).Text = "";
                                ((TextBox)tempC).Tag = Keys.None;
                            }
                        }
                        ((TextBox)sender).Text = e.KeyCode.ToString();
                        ((TextBox)sender).Tag = (object)e.KeyValue;
                    }
                }catch(Exception ex)
                {
                }
            }
            
        }

        private void labBackColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();            
            labBackColor.BackColor = colorDialog1.Color;  //将颜色对话框的颜色作为背景色
        }

        private void btnSaveConfig_Click(object sender, EventArgs e)
        {
            int height = 0;
            int width = 0;
            int pix = 0;
            try
            {
                height = Convert.ToInt32(this.txtVGrid.Text);
                width = Convert.ToInt32(this.txtHGrid.Text);
                pix = Convert.ToInt32(this.txtGridPix.Text);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            if (width>30 || height>40 || pix>20)
            {
                MessageBox.Show("你输入的垂直/水平格子数或是格子像素太大\n请重新输入!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                BlockInfoArr info = new BlockInfoArr();
                foreach (ListViewItem item in listVBlock.Items)
                {
                    info.AddBlock(item.SubItems[0].Text, item.SubItems[1].Text);
                }
                config.Info = info;
                config.LeftKey = (Keys)txtLeft.Tag;
                config.RightKey = (Keys)txtRight.Tag;
                config.DownKey = (Keys)txtDown.Tag;
                config.DropKey = (Keys)txtDrop.Tag;
                config.DeasilKey = (Keys)txtDeasil.Tag;
                config.DiseconomicsKey = (Keys)txtDiseconomics.Tag;

                config.CoorHeight = int.Parse(txtVGrid.Text);
                config.CoorWidth = int.Parse(txtHGrid.Text);
                config.RectPix = int.Parse(txtGridPix.Text);
                config.BackColor = labBackColor.BackColor;               


                DialogResult result;
                result = MessageBox.Show("你确认要保存所有设置?", "提示窗口", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.Cancel)
                    return;

                config.SaveToXml();

                mf.DownKey = config.DownKey;
                mf.DropKey = config.DropKey;
                mf.LeftMoveKey= config.LeftKey;
                mf.RightMoveKey = config.RightKey;
                mf.DeasilRotateKey = config.DeasilKey;
                mf.DisecRotateeKey = config.DiseconomicsKey;
                mf.PaletteHeight = config.CoorHeight;
                mf.PaletteWidth = config.CoorWidth;
                mf.pbRun.BackColor = config.BackColor;
                mf.labReady.BackColor = config.BackColor;               
                mf.RectPix = config.RectPix;
              
                //确定窗体大小
                mf.Width = mf.PaletteWidth * mf.RectPix + 145;
                mf.Height = mf.PaletteHeight * mf.RectPix + 80;
              
                //确定画布大小
                mf.pbRun.Width = mf.PaletteWidth * mf.RectPix;
                mf.pbRun.Height = mf.PaletteHeight * mf.RectPix;

                //设置下一个方块显示控件的位置
                mf.panel4.Left = mf.pbRun.Location.X + mf.pbRun.Width + 5;
                mf.panel4.Top = mf.pbRun.Location.Y;                

                //设置状态显示控件的位置、大小
                mf.palShowStatus.Left = mf.pbRun.Location.X + mf.pbRun.Width + 5;
                mf.palShowStatus.Top = mf.panel4.Height + 15;
                mf.palShowStatus.Height = mf.pbRun.Height - mf.panel4.Height - 10;
                mf.palShowStatus.Width = mf.panel4.Width ;

                this.Close();

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnEsc_Click(object sender, EventArgs e)
        {
            this.Close();
        }
           

        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -