首页 > 代码库 > DevExpress 窗体换肤

DevExpress 窗体换肤

本文参考引用:http://www.cnblogs.com/dreamflycc/archive/2012/09/14/2685308.html

注意,如果控件已汉化,与上面参考的略有不同

1.添加引用:

2.在ribbonPageGroup下新建个ribbonGalleryBarItem,并添加GalleryItemClick 事件:ribbonGalleryBarItem1_GalleryItemClick:
 
  

 
3.在应用程序的主入口里添加:
 static class Program    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()        {            DevExpress.UserSkins.BonusSkins.Register();            DevExpress.Skins.SkinManager.EnableFormSkins();            DevExpress.Skins.SkinManager.EnableMdiFormSkins();            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new Form1());        }    }

4.在窗体代码里添加命名空间using System.Xml;在事件ribbonGalleryBarItem1_Click里添加代码:

 private void ribbonGalleryBarItem1_GalleryItemClick(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e)        {            string caption = string.Empty;            if (ribbonGalleryBarItem1.Gallery == null) return;            //caption = ribbonGalleryBarItem1.Gallery.GetCheckedItems()[0].Caption;
//上面那句会获取显示的名字,因为我已汉化,所以有中文,不能匹配UserLookAndFeel.Default.SetSkinStyle中的皮肤名称
//无法达到关闭后再加载关闭前的皮肤,如已汉化,参考下面这句:
caption = ribbonGalleryBarItem1.Gallery.GetCheckedItems()[0].Tag.ToString();//获取真实的名称,没有汉化的 caption = caption.Replace("主题:", ""); XmlDocument doc = new XmlDocument(); doc.Load("SkinInfo.xml"); XmlNodeList nodelist = doc.SelectSingleNode("SetSkin").ChildNodes; foreach (XmlNode node in nodelist) { XmlElement xe = (XmlElement)node;//将子节点类型转换为XmlElement类型 if (xe.Name == "Skinstring") { xe.InnerText = caption; } } doc.Save("SkinInfo.xml"); }

5.添加命名空间using DevExpress.XtraBars.Helpers和using DevExpress.LookAndFeel;在Load下添加代码:

        public string defaultSkinName;//皮肤        private void useSkin_Load(object sender, EventArgs e)        {            SkinHelper.InitSkinGallery(ribbonGalleryBarItem1);            CheckFile();//检查文件            GetXmlSkin();//获取xml主题            UserLookAndFeel.Default.SetSkinStyle(defaultSkinName);//设置主题样式            ribbonGalleryBarItem1.Caption = "主题:" + defaultSkinName;        }        #region 检查XML文件是否存在        public void CheckFile()        {            try            {                if (System.IO.File.Exists("SkinInfo.xml") == false)                {                    //XtraMessageBox.Show("不存在XML");                    CreateXml();                }            }            catch (Exception ex)            {                XtraMessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);            }        }        #region 创建XML文件        public void CreateXml()        {            XmlDocument doc = new XmlDocument();            //建立xml定义声明            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);            doc.AppendChild(dec);            //创建根节点            XmlElement root = doc.CreateElement("SetSkin");            XmlElement rootone = doc.CreateElement("Skinstring");//皮肤            //将one,two,插入到root节点下            doc.AppendChild(root);            root.AppendChild(rootone);            doc.Save("SkinInfo.xml");        }        #endregion        #region 读取Xml节点内容        public void GetXmlSkin()        {            try            {                XmlDocument mydoc = new XmlDocument();                mydoc.Load("SkinInfo.xml");                XmlNode ressNode = mydoc.SelectSingleNode("SetSkin");                defaultSkinName = ressNode.SelectSingleNode("Skinstring").InnerText;            }            catch (Exception ex)            {                XtraMessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);            }        }        #endregion        #endregion

6.效果: