城里的
乡下鱼

换肤功能的实现

一、在项目下新建RibbonForm  命名为:useSkin
二、添加引用DevExpress.OfficeSkins
      DevExpress.UserSkins.BonusSkins

三、将ribbonPage1的Text属性设置为:皮肤

      ribbonPageGroup1的Text属性设置为:更换皮肤

      在ribbonPageGroup1下新建个ribbonGalleryBarItem1,将其Caption的属性设置为:请选择您喜欢的皮肤,并添GalleryItemClick    事件:ribbonGalleryBarItem1_GalleryItemClick

四、在应用程序的主入口里添加:

复制代码
static void Main()
        {
            //皮肤
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.Skins.SkinManager.EnableFormSkins();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Exercises.useSkin());
        }
复制代码

 

 

五、添加命名空间using System.Xml;

在事件ribbonGalleryBarItem1_Click里添加代码:

复制代码
private void ribbonGalleryBarItem1_Click(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e)
        {
            string name = string.Empty;
            string caption = string.Empty;
            if (ribbonGalleryBarItem1.Gallery == null) return;
            caption = ribbonGalleryBarItem1.Gallery.GetCheckedItems()[0].Caption;//主题的描述
            caption = caption.Replace("主题:", "");
            //name = bsiPaintStyle.Manager.PressedLink.Item.Tag.ToString();//主题的名称
            ribbonGalleryBarItem1.Caption = "主题:" + caption;

            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");
            //XtraMessageBox.Show("您选择了主题:" + caption);
        }
复制代码

添加命名空间

using DevExpress.XtraBars.Helpers;
using DevExpress.LookAndFeel;

六、添加命名空间using DevExpress.XtraEditors;

在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
复制代码

好了,现在就可以为自己的窗体选择喜欢的皮肤了。

看一下效果吧。。。。

       

 

 

天再高,踮起脚尖就能更接近阳光
posted on 2017-11-30 14:16  城里的乡下鱼  阅读(378)  评论(0编辑  收藏  举报