码迷,mamicode.com
首页 > Windows程序 > 详细

DevExpress 窗体换肤

时间:2014-07-31 12:44:26      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   文件   

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

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

1.添加引用:

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

bubuko.com,布布扣
 
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.效果:

bubuko.com,布布扣

 

 

DevExpress 窗体换肤,布布扣,bubuko.com

DevExpress 窗体换肤

标签:des   style   blog   http   color   java   os   文件   

原文地址:http://www.cnblogs.com/huangzhenhua/p/3880244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!