最近要用到devExpress,一句话,很好很强大,比起vs自带的winform界面,种类和花样要多了不少,然而,强力的功能带来了庞大的信息量,所以我打算通过一些小模块来和大家一起对它进行探讨和研究。
今天先研究一下他的换肤功能。在控件中,其默认提供了18中风格让我们选择,如下图:
下面就看看怎么实现让界面切换到选中风格的功能。先建立一个项目,引入一个button和一个grid如下图:
由于在devExpress.skins.skinManager.Default.Skins中只有13中皮肤所以我们要手动添加5种,找到Designer.cs文件,添加下列代码:
this.iPaintStyle.CategoryGuid = new System.Guid("d0173875-bf7b-4740-b252-5047db62606c");
this.iPaintStyle.LinksPersistInfo.AddRange(new DevExpress.XtraBars.LinkPersistInfo[] {
new DevExpress.XtraBars.LinkPersistInfo(this.ipsDefault),
new DevExpress.XtraBars.LinkPersistInfo(this.ipsWXP),
new DevExpress.XtraBars.LinkPersistInfo(this.ipsOXP),
new DevExpress.XtraBars.LinkPersistInfo(this.ipsO2K),
new DevExpress.XtraBars.LinkPersistInfo(this.ipsO3)});
private DevExpress.XtraBars.BarButtonItem ipsWXP; private DevExpress.XtraBars.BarButtonItem ipsOXP; private DevExpress.XtraBars.BarButtonItem ipsO2K; private DevExpress.XtraBars.BarButtonItem ipsO3; private DevExpress.XtraBars.BarButtonItem ipsDefault; this.ipsDefault = new DevExpress.XtraBars.BarButtonItem(); this.ipsWXP = new DevExpress.XtraBars.BarButtonItem(); this.ipsOXP = new DevExpress.XtraBars.BarButtonItem(); this.ipsO2K = new DevExpress.XtraBars.BarButtonItem(); this.ipsO3 = new DevExpress.XtraBars.BarButtonItem(); // // ipsDefault // this.ipsDefault.Caption = "Default"; this.ipsDefault.CategoryGuid = new System.Guid("d0173875- bf7b-4740-b252-5047db62606c"); this.ipsDefault.Description = "Default"; this.ipsDefault.Id = 15; this.ipsDefault.Name = "ipsDefault"; this.ipsDefault.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.ips_ItemClick); // // ipsWXP // this.ipsWXP.Caption = "Windows XP"; this.ipsWXP.CategoryGuid = new System.Guid("d0173875-bf7b-4740-b252-5047db62606c"); this.ipsWXP.Description = "WindowsXP"; this.ipsWXP.Id = 11; this.ipsWXP.ImageIndex = 5; this.ipsWXP.Name = "ipsWXP"; this.ipsWXP.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.ips_ItemClick); // // ipsOXP // this.ipsOXP.Caption = "Office XP"; this.ipsOXP.CategoryGuid = new System.Guid("d0173875-bf7b-4740-b252-5047db62606c"); this.ipsOXP.Description = "OfficeXP"; this.ipsOXP.Id = 12; this.ipsOXP.ImageIndex = 3; this.ipsOXP.Name = "ipsOXP"; this.ipsOXP.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.ips_ItemClick); // // ipsO2K // this.ipsO2K.Caption = "Office 2000"; this.ipsO2K.CategoryGuid = new System.Guid("d0173875-bf7b-4740-b252-5047db62606c"); this.ipsO2K.Description = "Office2000"; this.ipsO2K.Id = 13; this.ipsO2K.ImageIndex = 4; this.ipsO2K.Name = "ipsO2K"; this.ipsO2K.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.ips_ItemClick); // // ipsO3 // this.ipsO3.Caption = "Office 2003"; this.ipsO3.CategoryGuid = new System.Guid("d0173875-bf7b-4740-b252-5047db62606c"); this.ipsO3.Description = "Office2003"; this.ipsO3.Id = 14; this.ipsO3.ImageIndex = 6; this.ipsO3.Name = "ipsO3"; this.ipsO3.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.ips_ItemClick);
然后将其添加到barManager的项中
this.barManager1.Items.AddRange(new DevExpress.XtraBars.BarItem[] { this.barSubItem1, this.barSubItem2, this.ipsWXP, this.ipsOXP, this.ipsO2K, this.ipsO3, this.ipsDefault, this.iPaintStyle});
然后添加这几种风格的点击事件
private void ips_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { barManager1.GetController().PaintStyleName = e.Item.Description; InitPaintStyle(e.Item); barManager1.GetController().ResetStyleDefaults(); DevExpress.LookAndFeel.UserLookAndFeel.Default.SetDefaultStyle(); }
然后将皮肤初始化并为其注册选择事件
private void iPaintStyleStyle_Init() { BarItem item = null; for (int i = 0; i < barManager1.Items.Count; i++) { if (barManager1.Items[i].Description == barManager1.GetController().PaintStyleName) item = barManager1.Items[i]; } InitPaintStyle(item); } private void InitPaintStyle(BarItem item) { if (item == null) { iPaintStyle.ImageIndex = item.ImageIndex; iPaintStyle.Caption = item.Caption; iPaintStyle.Hint = item.Description; } } void InitSkins() { barManager1.ForceInitialize(); foreach (DevExpress.Skins.SkinContainer cnt in DevExpress.Skins.SkinManager.Default.Skins) { BarButtonItem item = new BarButtonItem(barManager1, skinMask + cnt.SkinName); iPaintStyle.AddItem(item); item.ItemClick += new ItemClickEventHandler(OnSkinClick); } } void OnSkinClick(Object sender, ItemClickEventArgs e) { string skinName = e.Item.Caption.Replace(skinMask, ""); DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName); barManager1.GetController().PaintStyleName = "Skin"; iPaintStyle.Caption = iPaintStyle.Hint = e.Item.Caption; iPaintStyle.Hint = iPaintStyle.Caption; iPaintStyle.ImageIndex = -1; }
大功告成:初始化winform的时候调用下面两个方法就可以进行换肤了
iPaintStyleStyle_Init();
InitSkins();
效果如下:
上面的是比较麻烦的办法,我们也可以用他自身集成的换肤功能来实现这一效果:在winform中拉一个ribbonControl控件,在page页中添加ribbonGalleryBarItem。
然后加入引用
using DevExpress.XtraBars.Helpers;
在窗口加载时让刚添加的item设置为皮肤选项:
private void Form1_Load(object sender, EventArgs e) { SkinHelper.InitSkinGallery(ribbonGalleryBarItem1); this.ribbonControl1.ToolbarLocation = DevExpress.XtraBars.Ribbon.RibbonQuickAccessToolbarLocation.Hidden; }
这样就可以实现下面的效果了: