[DevExpress]窗体换肤

转自:http://www.evget.com/article/2014/10/10/21667.html

最近在学习 DevExpress 这个控件,将窗体换肤的一些心得记录下来,分享给大家:

【DXperience Universal Suite下载】

1.添加引用:

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

[DevExpress]窗体换肤_第1张图片

DevExpress窗体换肤

3.在应用程序的主入口里添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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里添加代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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下添加代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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.效果:

[DevExpress]窗体换肤_第2张图片

Via博客园i小白



你可能感兴趣的:([DevExpress]窗体换肤)