原創(chuàng)|其它|編輯:郝浩|2012-10-09 13:47:45.000|閱讀 457 次
概述:在DevExpress.XtraEditors.XtraForm中,窗體的樣式和皮膚有UseDefaultLookAndFeel屬性。設(shè)置為false,就可以直接再屬性里面修改樣式和皮膚了。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在DevExpress.XtraEditors.XtraForm中,窗體的樣式和皮膚有UseDefaultLookAndFeel屬性。設(shè)置為false,就可以直接再屬性里面修改樣式和皮膚了。如果設(shè)置為true,可以再里面放控件defaultLookAndFeel,設(shè)置 defaultLookAndFeel的樣式和皮膚,XtraForm窗體的樣式和皮膚就可以隨著改變。
我試著定義一個(gè)XFrmBase窗體,再窗體上房一個(gè)控件:defaultLookAndFeel1,定義為Protected。然后其它窗體都從這個(gè)窗體上繼承。發(fā)現(xiàn)再代碼的任何一個(gè)地方,修改defaultLookAndFeel1的屬性,所有窗體的樣式和皮膚跟著改變。然后再把皮膚和樣式保存在XML文件中,就實(shí)現(xiàn)了所謂的換膚功能。
具體步驟如下:
1. 定義一個(gè)XFrmBase窗體,在這個(gè)窗體上放defaultLookAndFeel1控件,修改為Protected。在XFrmBase.Designer.cs中,把
//this.defaultLookAndFeel1.LookAndFeel.SkinName = ...; //this.defaultLookAndFeel1.LookAndFeel.Style = ...;
這兩行注釋掉,然后其它所有的窗體都從這個(gè)窗體繼承。
2. 定義兩個(gè)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. 兩個(gè)操作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. 在最先啟動(dòng)的窗體中設(shè)置defaultLookAndFeel1
string styleName = StyleSkin.GetSelectedStyle(); //defaultLookAndFeel1.LookAndFeel.Style = YarnNew.Classes.StyleSkin.GetLookFeelStyle(styleName); defaultLookAndFeel1.LookAndFeel.SkinName = StyleSkin.GetSelectedSkin();
5.最后是定義一個(gè)窗體,設(shè)置和保存樣式及皮膚。
具體代碼略。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載