C#皮肤更换功能

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.Xml.Linq;
using System.IO;

namespace MySkinDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();    
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadSkin();
            LoadFile();//加载皮肤菜单
        }
        #region 皮肤处理
        /// <summary>
        /// 加载皮肤
        /// </summary>
        private void LoadSkin()
        {
            string skinFilePath = Application.StartupPath + "\\SkinFile.xml";
            skinEngine1.SkinFile = XDocument.Load(skinFilePath).Element("skinFile").Element("path").Value;      
        }
        /// <summary>
        /// 加载文件
        /// </summary>
        private void LoadFile()
        {
            string path = Application.StartupPath + "\\Skins";
            DirectoryInfo di = new DirectoryInfo(path);//获取文件夹信息
            ToolStripMenuItem tsmiFile = null;
            foreach (FileInfo file in di.GetFiles().Where(x => x.Extension == ".ssk"))
            {
                tsmiFile = new ToolStripMenuItem();//创建菜单对象
                tsmiFile.Text = file.Name.Substring(0, file.Name.IndexOf(".ssk"));
                this.tsmiSkin.DropDownItems.Add(tsmiFile);
                tsmiFile.Click += new System.EventHandler(this.file_Click);
            }
        }
        /// <summary>
        /// 点击事件
        /// </summary>
        private void file_Click(object sender, EventArgs e)
        {
            string value = sender.ToString();
            string FullName = Application.StartupPath + "\\Skins\\" + value + ".ssk";
            skinEngine1.SkinFile = FullName;
            UpdateXmlElement(FullName); 
        }
        /// <summary>
        /// 修改xml文件节点
        /// </summary>
        private void UpdateXmlElement(string value)
        {
            ///加载XML文件
            string xmlFilePath = Application.StartupPath + "\\SkinFile.xml";
            XElement xe = XElement.Load(xmlFilePath);
            ///查找被替换的元素
            IEnumerable<XElement> element = xe.Elements("path");
            ///替换为新元素,并保存
            if (element.Count() > 0)
            {
                XElement first = element.First();
                ///设置新的属性
                first.SetValue(value);
            }
            xe.Save(xmlFilePath);
        }
        #endregion
        private void button1_Click(object sender, EventArgs e)
        {
            new Form2().Show();
        }     
    }
}

效果如图:

posted @ 2012-08-14 09:22  HSHE  阅读(452)  评论(0编辑  收藏  举报