WinForm中使用DXperience控件中XtraForm,如何实现换肤

     在DevExpress.XtraEditors.XtraForm中,窗体的样式和皮肤有UseDefaultLookAndFeel属性。设置为false,就可以直接再属性里面修改样式和皮肤了。如果设置为true,可以再里面放控件defaultLookAndFeel,设置 defaultLookAndFeel的样式和皮肤,XtraForm窗体的样式和皮肤就可以随着改变。

    我试着定义一个XFrmBase窗体,再窗体上房一个控件:defaultLookAndFeel1,定义为Protected。然后其它窗体都从这个窗体上继承。发现再代码的任何一个地方,修改defaultLookAndFeel1的属性,所有窗体的样式和皮肤跟着改变。然后再把皮肤和样式保存在XML文件中,就实现了所谓的换肤功能。

具体步骤如下:
1. 定义一个XFrmBase窗体,在这个窗体上放defaultLookAndFeel1控件,修改为Protected。在XFrmBase.Designer.cs中,把
//this.defaultLookAndFeel1.LookAndFeel.SkinName = ...;
//this.defaultLookAndFeel1.LookAndFeel.Style = ...;
这两行注释掉,然后其它所有的窗体都从这个窗体继承。

2. 定义两个XML文件:
StyleXML.xml
<?xml version="1.0" encoding="utf-8"?>
<xml>
  <Style>
    <StyleValue value="">Flat</StyleValue>
    <StyleValue value="">UltraFlat</StyleValue>
    <StyleValue value="IsSelected">Style3D</StyleValue>
    <StyleValue value="">Office2003</StyleValue>
    <StyleValue value="">WinXp</StyleValue>
    <StyleValue value="">Skin</StyleValue>
  </Style>
</xml>
SkinXML.xml
<?xml version="1.0" encoding="utf-8"?>
<xml>
  <Skin>
    <SkinValue value="IsSelected">Caramel</SkinValue>
    <SkinValue value="">Money Twins</SkinValue>
    <SkinValue value="">Lilian</SkinValue>
    <SkinValue value="">The Asphalt World</SkinValue>
    <SkinValue value="">iMaginary</SkinValue>
    <SkinValue value="">Black</SkinValue>
    <SkinValue value="">Blue</SkinValue>
  </Skin>
</xml>

3. 两个操作XML文件的类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.IO;

namespace FlowerWin.Classes
{
    public class XmlControl
    {
        protected string strXmlFile;
        protected XmlDocument objXmlDoc = new XmlDocument();

        public XmlControl(string xmlFile)
        {
            try
            {
                objXmlDoc.Load(xmlFile);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            strXmlFile = xmlFile;
        }

        public DataView GetData(string xmlPathNode)
        {
            DataSet ds = new DataSet();
            StringReader read = new StringReader(objXmlDoc.SelectSingleNode(xmlPathNode).OuterXml);
            //string test = objXmlDoc.SelectSingleNode(xmlPathNode).OuterXml;
            ds.ReadXml(read);
            return ds.Tables[0].DefaultView;
        }

        public DataTable GetData(string xmlPathNode, string attrib)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("NodeName", typeof(string));
            dt.Columns.Add(attrib, typeof(string));

            XmlNodeList xnList = objXmlDoc.SelectNodes(xmlPathNode);
            foreach (XmlNode xn in xnList)
            {
                if (xn.Attributes[attrib] != null)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = xn.InnerText;
                    dr[1] = xn.Attributes[attrib].Value;
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        public void Replace(string xmlPathNode, string content)
        {
            objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
        }

        public void Delete(string node)
        {
            string mainNode = node.Substring(0, node.LastIndexOf("/"));
            objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(node));
        }

        public void InsertNode(string mainNode, string childNode, string element, string content)
        {
            XmlNode objRootNode = objXmlDoc.SelectSingleNode(mainNode);
            XmlElement objChildNode = objXmlDoc.CreateElement(childNode);
            objRootNode.AppendChild(objChildNode);
            XmlElement objElement = objXmlDoc.CreateElement(element);
            objElement.InnerText = content;
            objChildNode.AppendChild(objElement);
        }

        public void InsertElement(string mainNode, string element, string attrib, string attribContent, string content)
        {
            XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
            XmlElement objElement = objXmlDoc.CreateElement(element);
            objElement.SetAttribute(attrib, attribContent);
            objElement.InnerText = content;
            objNode.AppendChild(objElement);
        }

        public void InsertElement(string mainNode, string element, string content)
        {
            XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
            XmlElement objElement = objXmlDoc.CreateElement(element);
            objElement.InnerText = content;
            objNode.AppendChild(objElement);
        }

        public string[] GetNodeList(string xmlPathNode)
        {
            XmlNodeList xnList = objXmlDoc.SelectNodes(xmlPathNode);
            string[] strArr = new string[xnList.Count];
            for (int i = 0; i < xnList.Count; i++)
            {
                strArr[i] = xnList[i].InnerText;
            }
            return strArr;
        }

        public string GetNodeStr(string xmlPathNode, string attrib, string attribContent)
        {
            string xnStr = "";
            XmlNodeList xnList = objXmlDoc.SelectNodes(xmlPathNode);
            foreach (XmlNode xn in xnList)
            {
                if (xn.Attributes[attrib] != null)
                {
                    if (xn.Attributes[attrib].Value == attribContent)
                    {
                        xnStr = xn.InnerText;
                        break;
                    }
                }
            }
            return xnStr;
        }

        public void SaveNode(string xmlPathNode, string nodeName)
        {
            XmlNodeList xnList = objXmlDoc.SelectNodes(xmlPathNode);
            foreach (XmlNode xn in xnList)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("value") == "IsSelected")
                {
                    xe.SetAttribute("value", "");
                }
                if (xe.InnerText == nodeName)
                {
                    xe.SetAttribute("value", "IsSelected");
                }
            }

            Save();
        }

        public void Save()
        {
            try
            {
                objXmlDoc.Save(strXmlFile);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            objXmlDoc = null;
        }

    }
}
///////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using DevExpress.LookAndFeel;

namespace FlowerWin.Classes
{
    public class StyleSkin
    {
        private static string xmlStyle = Common.GetXmlDirectory() + "StyleXml.xml";
        private static string xmlSkin = Common.GetXmlDirectory() + "SkinXml.xml";

        public static string GetSelectedStyle()
        {
            XmlControl xmlControl = new XmlControl(xmlStyle);
            string styleName = xmlControl.GetNodeStr("xml/Style/StyleValue", "value", "IsSelected");
            return styleName;
        }

        public static void GetLookFeelStyle(DevExpress.LookAndFeel.DefaultLookAndFeel dlf, string styleName)
        {
            string skinName = GetSelectedSkin();
            switch (styleName)
            {
                case "Flat":
                    dlf.LookAndFeel.SetFlatStyle();
                    break;
                case "UltraFlat":
                    dlf.LookAndFeel.SetUltraFlatStyle();
                    break;
                case "Style3D":
                    dlf.LookAndFeel.SetStyle3D();
                    break;
                case "Office2003":
                    dlf.LookAndFeel.SetOffice2003Style();
                    break;
                case "WinXp":
                    dlf.LookAndFeel.SetWindowsXPStyle();
                    break;
                case "Skin":
                    dlf.LookAndFeel.SetSkinStyle(skinName);
                    break;
                default:
                    dlf.LookAndFeel.SetDefaultStyle();
                    break;
            }
        }

        public static DevExpress.LookAndFeel.LookAndFeelStyle GetLookFeelStyle(string styleName)
        {
            DevExpress.LookAndFeel.LookAndFeelStyle lfStyle;
            switch (styleName)
            {
                case "Flat":
                    lfStyle = LookAndFeelStyle.Flat;
                    break;
                case "UltraFlat":
                    lfStyle = LookAndFeelStyle.UltraFlat;
                    break;
                case "Style3D":
                    lfStyle = LookAndFeelStyle.Style3D;
                    break;
                case "Office2003":
                    lfStyle = LookAndFeelStyle.Office2003;
                    break;
                case "Skin":
                    lfStyle = LookAndFeelStyle.Skin;
                    break;
                default:
                    lfStyle = LookAndFeelStyle.Office2003;
                    break;
            }
            return lfStyle;
        }

        public static string GetSelectedSkin()
        {
            XmlControl xmlControl = new XmlControl(xmlSkin);
            string skinName = xmlControl.GetNodeStr("xml/Skin/SkinValue", "value", "IsSelected");
            return skinName;
        }

        public static DataTable GetStytleList()
        {
            XmlControl xmlControl = new XmlControl(xmlStyle);
            DataTable dt = xmlControl.GetData("xml/Style/StyleValue", "value");
            return dt;
        }

        public static DataTable GetSkinList()
        {
            XmlControl xmlControl = new XmlControl(xmlSkin);
            DataTable dt = xmlControl.GetData("xml/Skin/SkinValue", "value");
            return dt;
        }

        public static void SaveSelectedStlye(string styleName)
        {
            XmlControl xmlControl = new XmlControl(xmlStyle);
            xmlControl.SaveNode("xml/Style/StyleValue", styleName);
        }

        public static void SaveSelectedSkin(string skinName)
        {
            XmlControl xmlControl = new XmlControl(xmlSkin);
            xmlControl.SaveNode("xml/Skin/SkinValue", skinName);
        }
    }
}
///////////////////////////////////
4. 在最先启动的窗体中设置defaultLookAndFeel1
string styleName = StyleSkin.GetSelectedStyle();
 //defaultLookAndFeel1.LookAndFeel.Style = YarnNew.Classes.StyleSkin.GetLookFeelStyle(styleName);
defaultLookAndFeel1.LookAndFeel.SkinName = StyleSkin.GetSelectedSkin();

5.最后是定义一个窗体,设置和保存样式及皮肤。
具体代码略。


http://blog.csdn.net/patrickpan/archive/2007/08/08/1731352.aspx

posted on 2008-01-18 15:31  巍巍边疆  阅读(2329)  评论(0编辑  收藏  举报