DevExpress控件的主题不仅多种多样,而且非常美观大方,以对完美观念不强的我们绝对是个惊喜,当然了,对于我们的用户也是一个福音,因为他们再对不用抱怨我人软件界面是如何的粗造了。
l 如何打造美丽的界面?
l 如何快速的更换漂亮的主题?
l 如何快速的使用类似于Offcie2007的操作面板?
......
本节课程将一一解答,带您进入美轮美奂的DevExpress世界!
1. 如何建造可换主题的窗口程序
当我们打开或新建一个“Windows程序”,系统默认会有一个From,名称为From1。请注意,此时的”Form1”继承的是Windows 自带的Form,即”System.Windows.Forms”。而这个Form是不会应用DevExpress的主题样式的。
那么我们怎么样才可以让Form应用这些样式呢?
当我们安装完DevExpress控件包之后,在添加新项窗口中我们会看到多出的几项:如图:
分别是:DevExpress Form v9.1、DevExpress RibbomForm v9.1和DevExpressUsercontrol v9.1,其中“v9.1”是版本号。
当我们在新建“Form”时,选择DevExpress Form为模板,就可以应用DevExpress的漂亮主题了。此时,From继承的是“DevExpress.XtraEditors.XtraForm”。默认情况下,系统主题有:
Caramel
Money Twins
Lilian
The Asphalt World
iMaginary
Black
Blue
这几种主题,具体效果自己写个例子看看吧! 但DevExpress提供了更多的主题, 我们如果应用的话,必须添加以下引用:
DevExpress..OfficeSkins.v9.1;
DevExpress.BonusSkins.v9.1;
除此之外,必须在“Program.cs”文件在添加如下代码:
1. DevExpress.UserSkins.OfficeSkins.Register();
2. DevExpress.UserSkins.BonusSkins.Register();
3. Application.EnableVisualStyles();
4. Application.SetCompatibleTextRenderingDefault(false);
5. if (!DevExpress.Skins.SkinManager.AllowFormSkins)
6. DevExpress.Skins.SkinManager.EnableFormSkins();
7. Application.Run(new frmMain());
其中“OfficeSkins”中的主题有:
Office 2007 Blue
Office 2007 Black
Office 2007 Silver
Office 2007 Green
Office 2007 Pink
“BonusSkins”的主题有:
Coffee
Liquid Sky
London Liquid Sky
Glass Oceans
Stardust
Xmas 2008 Blue
Valentine
McSkin
Summer 2008
Pumpkin
Dark Side
Springtime
2. 如何快速的更换漂亮的主题
通过“foreach(SkinContainer cnt in SkinManager.Default.Skins){}”可以获取现有所有遥主题名称,可以把获取的主题添另到菜单上,让用户选择自己喜爱的主题样式。
通过对“DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName”设置主题名称,就可以轻松的更换主题,是不是很方便?!
下面就写一个例子,展示一下它们漂亮的“皮肤”吧!
3. 添加类似于Office2007的窗口
Microsoft Office 2007窗口的操作面板非常漂亮,值得让人欣喜的是DevExpress里不仅有“RibbonControl”这样的控件,还有现成的Form 模板直接使用,添加一个:
添加新项—选项“DevExpress RibbomForm v9.1”,OK。这么简单!!!
以下是主要代码及运行效果:
1. public frmMain()
2. {
3. InitializeComponent();
4. BarSubItem skinBar = new BarSubItem(barManager1, "Look and Feel");
5. this.mainMenuBar.AddItem(skinBar);
6.
7.
8.
9. string bonusSkinNames = ";Coffee;Liquid Sky;London Liquid Sky;Glass Oceans;Stardust;Xmas 2008 Blue;Valentine;McSkin;Summer 2008;Pumpkin;Dark Side;Springtime";
10. BarSubItem BonusSkins = new BarSubItem(barManager1, "Bonus Skins");
11. BarSubItem OfficeSkins = new BarSubItem(barManager1, "Office Skins");
12. BarSubItem Skins = new BarSubItem(barManager1, "Skins");
13. foreach (SkinContainer cnt in SkinManager.Default.Skins)
14. {
15. BarButtonItem subMenu = new BarButtonItem(barManager1, cnt.SkinName);
16. subMenu.ItemClick += new ItemClickEventHandler(subMenu_ItemClick);
17. if (cnt.SkinName.IndexOf("Office") > -1)
18. {
19. OfficeSkins.AddItem(subMenu);
20. }
21. else if (bonusSkinNames.IndexOf(string.Format(";{0}", cnt.SkinName)) > -1)
22. {
23. BonusSkins.AddItem(subMenu);
24. }
25. else
26. {
27. Skins.AddItem(subMenu);
28. }
29. }
30. string[] OtherSkins = new string[] { "Default", "Flat", "Ultra Flat", "Style3D", "Office2003" };
31. for (int i = 0; i < OtherSkins.Length; i++)
32. {
33. BarButtonItem subMenu = new BarButtonItem(barManager1, OtherSkins[i]);
34. subMenu.ItemClick += new ItemClickEventHandler(subMenu_ItemClick);
35. skinBar.AddItem(subMenu);
36. }
37. skinBar.AddItem(Skins);
38. skinBar.AddItem(OfficeSkins);
39. skinBar.AddItem(BonusSkins);
40.
41.
42.
43. }
44.
45. void subMenu_ItemClick(object sender, ItemClickEventArgs e)
46. {
47. switch (e.Item.Caption)
48. {
49. case "Style3D":
50. // DevExpress.LookAndFeel.UserLookAndFeel.Default.Style = LookAndFeelStyle.Style3D;
51. DevExpress.LookAndFeel.UserLookAndFeel.Default.SetStyle3D();
52. break;
53. case "Office2003":
54. // DevExpress.LookAndFeel.UserLookAndFeel.Default.Style = LookAndFeelStyle.Office2003;
55. DevExpress.LookAndFeel.UserLookAndFeel.Default.SetOffice2003Style();
56. break;
57. case "Ultra Flat":
58. // DevExpress.LookAndFeel.UserLookAndFeel.Default.Style = LookAndFeelStyle.UltraFlat;
59. DevExpress.LookAndFeel.UserLookAndFeel.Default.SetUltraFlatStyle();
60. break;
61. case "Flat":
62. // DevExpress.LookAndFeel.UserLookAndFeel.Default.Style = LookAndFeelStyle.Flat;
63. DevExpress.LookAndFeel.UserLookAndFeel.Default.SetFlatStyle();
64. break;
65. default:
66. // DevExpress.LookAndFeel.UserLookAndFeel.Default.Style = LookAndFeelStyle.Skin;
67. DevExpress.LookAndFeel.UserLookAndFeel.Default.SetDefaultStyle();
68. break;
69. }
70. DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = e.Item.Caption;
71.
72. //
73. }