一个简单的C#背包系统

构思:总结以往游戏里面的背包栏,我们可以用一个树形结构去管理背包结构,通过创建节点的形式创建背包,管理的这些背包即是实时存在的文件夹。然后用专门的一个控件去显示背包内容或添加内容。

构思图:

控件展示:

效果展示:

添加完节点后向节点内添加文件(我这里是仅仅图片文件的背包系统)

背包文件目录:

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 背包系统
{
    public partial class Form1 : Form
    {
        //定义背包根路径
        private static String packsPath = Application.StartupPath+"\\Packs";
        //当前的背包全路径
        private static String packPath;
        public Form1()
        {
            InitializeComponent();
            Form1_Load();
        }

        //加载环境
        private void Form1_Load() {
            //判断PacksPath路径下的文件夹是否存在
            bool b = Directory.Exists(packsPath);
            if (!b)
            {
                //如果不存在就创建一个
                Directory.CreateDirectory(packsPath);
            }
            else {
                //加载已存在的节点文件
                 try
                {
                    //获取背包下的所有子背包
                    String[] packs = Directory.GetDirectories(packsPath);
                    //通过遍历添加节点
                    foreach (string pack in packs)
                    {
                        //父节点
                        String[] Names = pack.Split('\\');
                        TreeNode node = new TreeNode(Names[Names.Length-1]);
                        treeView1.Nodes.Add(node);
                        PacksNext(pack, node);
                    }
                 }
                 catch { }
            }
        }

        ///添加根节点
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                //创建节点文件夹
                Directory.CreateDirectory(packsPath + "\\" + textBox1.Text);
                TreeNode rootnode = new TreeNode();
                //添加到treeView
                rootnode.Text = textBox1.Text;
                treeView1.Nodes.Add(rootnode);
            }
            else
            {
                MessageBox.Show("请先输入节点名称!");
            }
        }

        ///添加子节点
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && treeView1.Nodes.Count > 0)
            {
                try
                {
                    //获取当前子节点的所有父节点
                    List<String> nameList = new List<string>();
                    GetParentsName(treeView1.SelectedNode  ,ref nameList);
                    String name = "";
                    for (int i = nameList.Count - 1; i >= 0; i--)
                    {
                        name += nameList[i];

                    }
                    //创建节点文件夹
                    Directory.CreateDirectory(packsPath + name + "\\"+ textBox1.Text);

                    TreeNode rootnode = new TreeNode();
                    rootnode.Text = textBox1.Text;
                    treeView1.SelectedNode.Nodes.Add(rootnode);
                }
                catch (Exception)
                {

                    MessageBox.Show("操作失败,请重试!");
                }
            }
        }

        ///删除节点
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                //获取当前子节点的所有父节点
                List<String> nameList = new List<string>();
                GetParentsName(treeView1.SelectedNode, ref nameList);
                String name = "";
                for (int i = nameList.Count - 1; i >= 0; i--)
                {
                    name += nameList[i];

                }
                try
                {
                    //删除背包,这里只允许删除空的文件夹
                    Directory.Delete(packsPath + name);
                    //清除显示
                    treeView1.SelectedNode.Remove();
                    listView1.Clear();
                }
                catch (Exception)
                {

                    MessageBox.Show("此节点下存在文件,无法删除!");
                }
            }
            catch (Exception)
            {

                MessageBox.Show("请选择要删除的节点!");
            }
        }

        //在listView1上的鼠标点击事件,用于向节点中添加内容
        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            //右击触发事件
            if (e.Button == MouseButtons.Right && packPath!=null)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "请选择要上传的图片";
                ofd.Filter = "JPG图片|*.jpg|PNG图片|*.png|Bmp图片|*.bmp";
                ofd.CheckFileExists = true;
                ofd.Multiselect = false;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    String fileName = ofd.FileName;
                    String[] paths = fileName.Split('\\');
                    try
                    {
                        File.Copy(fileName, packPath + '\\' + paths[paths.Length - 1]);
                    }
                    catch (Exception)
                    {

                        MessageBox.Show("文件名重复!","系统提示");
                    } 
                    //刷新内容
                    Load_ListView(packPath);
                }
            }
        }

        //递归,遍历背包节点
        private void PacksNext(String packNext, TreeNode parent)
        {
            try
            {
                //获取父节点目录的子目录
                String[] Packs = Directory.GetDirectories(packNext);
                //子节点
                TreeNode node = new TreeNode();
                foreach (string Pack in Packs)
                {
                    String[] Names = Pack.Split('\\');
                    node = new TreeNode(Names[Names.Length - 1]);
                    parent.Nodes.Add(node);
                    //递归
                    PacksNext(Pack, node);
                }
            }
            catch { }
        }

        //点击节点树上的子节点向listView1添加显示内容
        private void treeView1_MouseDown(object sender, MouseEventArgs e) {
            if ((sender as TreeView) != null)
            {
                listView1.Clear();
                //获取点击的节点
                TreeNode node = treeView1.GetNodeAt(e.X, e.Y);
                List<String> nameList = new List<string>();
                GetParentsName(node, ref nameList);
                String name = "";
                for (int i = nameList.Count - 1; i >= 0; i--)
                {
                    name += nameList[i];

                }
                packPath = packsPath + name;
                //加载ListView
                Load_ListView(packPath);
            }
        }

        //加载ListView
        private void Load_ListView(String packPath)
        {
            listView1.Clear();
            //获取文件夹下面的文件名
            String[] fileNames = Directory.GetFiles(packPath);
            ImageList pics = new ImageList();
            foreach (string picpath in fileNames)
            {
                //图像名字
                string picp = picpath.Split('\\')[picpath.Split('\\').Length - 1].Split('.')[0].ToString();
                //图像文件
                Image img = Image.FromFile(picpath);
                pics.Images.Add(picp, img);
                //设置 ImageList 的宽和高
                pics.ImageSize = new Size(200, 200);

            }
            listView1.LargeImageList = pics;
            listView1.BeginUpdate();
            for (int i = 0; i < fileNames.Length; i++)
            {
                ListViewItem lvi = new ListViewItem();

                lvi.ImageIndex = i;

                lvi.Text = fileNames[i].Split('\\')[fileNames[i].Split('\\').Length - 1].Split('.')[0].ToString();

                listView1.Items.Add(lvi);
            }
            listView1.EndUpdate();
        }

        //递归获取当前节点的所有父节点名
        private void GetParentsName(TreeNode Node, ref List<String> nameList)
        {
            nameList.Add("\\" + Node.Text);
            if (Node.Parent != null)
            {
                //递归
                GetParentsName(Node.Parent, ref nameList);
            }
        }
    }
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值