DevExpress增加修改皮膚菜單

當我們在新建“Form”時,選擇DevExpress Form爲模板,就可以應用DevExpress的漂亮主題了。此時,From繼承的是“DevExpress.XtraEditors.XtraForm”。

或者如果是Form,則修改基類爲XtraForm或RibbonForm

 public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm

所有DevExpress .NET WinForms控件都支持皮膚技術。皮膚實際上是一種決定控件元素在其可能的狀態(正常、熱追蹤、選擇、按下等)被繪製的一組位圖集合。皮膚能夠保證在不同的系統下都有相同的外觀,DevExpress.Utils庫下有幾款皮膚,即Caramel、Money Twins、Lilian、iMaginary、Black、Blue,另外在DevExpress.BonusSkins庫下面還有Coffee、Liquid Sky、London Liquid Sky、Glass Oceans、Stardust、Xmas 2008 Blue、Valentine、McSkin幾款皮膚,DevExpress.OfficeSkins庫下面有Office 2007 Blue、Office 2007 Black、Office 2007 Silver、Office 2007 Green、Office 2007 Pink等皮膚。
如何使用這些豐富的皮膚呢?
首先要添加對類庫的引用
前面提到DevExpress.Utils、DevExpress.BonusSkins和DevExpress.OfficeSkins類庫下具有的皮膚,所以要添加對這些類庫的應用。

【原創】DevExpress Skin 皮膚機理 - Castor - 趁年輕,多折騰~~

接下來,需要註冊這些皮膚,註冊皮膚需要在程序入口處添加,而且要在窗口出現之前。雙擊打開Program.cs,將Main函數修改如下:
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    //下面是註冊皮膚
    DevExpress.UserSkins.BonusSkins.Register();
    DevExpress.UserSkins.OfficeSkins.Register();

    Application.Run(new XtraForm1());
}
這樣才能在程序中正常地使用皮膚。請注意我這裏使用的是DevExpress下的XtraForm窗體,而不是VS自動提供的Form類,因爲XtraForm具有LookAndFeel屬性。
爲了演示一下不同的皮膚,我們可以給窗體添加一個ListBoxControl,並在Items中輸入如下內容:
Caramel
Money Twins
Lilian
iMaginary
Black
Blue
Coffee
Liquid Sky
London Liquid Sky
Glass Oceans
Stardust
Xmas 2008 Blue
Valentine
McSkin
Office 2007 Blue
Office 2007 Black
Office 2007 Silver
Office 2007 Green
Office 2007 Pink

當然了,這些代碼也是可以在運行時添加的,ListBoxControl和ListBox用法差不多,具體用法就不囉嗦了。
給窗體添加一個DefaultLookAndFeel控件,該控件類似一個全局變量,決定了控件使用的默認皮膚。

雙擊列表控件,添加事件處理代碼:
private void listBoxControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    defaultLookAndFeel1.LookAndFeel.UseWindowsXPTheme = false;
    defaultLookAndFeel1.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Skin;
    string SkinName = listBoxControl1.SelectedItem.ToString();
    defaultLookAndFeel1.LookAndFeel.SkinName = SkinName;
}

運行就可以發現點擊不同的名稱,就會出現不一樣的皮膚:

【原創】DevExpress Skin 皮膚機理 - Castor - 趁年輕,多折騰~~

【原創】DevExpress Skin 皮膚機理 - Castor - 趁年輕,多折騰~~

【原創】DevExpress Skin 皮膚機理 - Castor - 趁年輕,多折騰~~

最後再強調一次,使用皮膚一定要將LookAndFeel屬性設置如下: 
UseDefaultLookAndFeel和UseWindowsXPTheme屬性必須爲false,Style 屬性必須設置爲LookAndFeelStyle.Skin。然後才能通過SkinName 屬性實現皮膚樣式的修改。

標題欄皮膚

DevExpress窗體(包括XtraForm,RibbonForm,XtraMessageBox以及其他由DevExpress顯示的對話框、消息框等)都支持標題欄皮膚
設置標題欄皮膚很簡單,你只要在Program.cs下的Main中添加一條語句即可:
 DevExpress.Skins.SkinManager.EnableFormSkins(); //需要在Run前面調用
 Application.Run(new XtraForm1());
可以對比一下添加該語句和沒有該語句的效果:

【原創】DevExpress標題欄皮膚 - Castor - 趁年輕,多折騰~~

 沒有使用DevExpress.Skins.SkinManager.EnableFormSkins()方法

【原創】DevExpress標題欄皮膚 - Castor - 趁年輕,多折騰~~

 使用DevExpress.Skins.SkinManager.EnableFormSkins()方法

當然,也可以在Run後面調用,但是在使用了EnableFormSkins之後需要調用DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged方法,例如將代碼加入到前面的listBoxControl1的SelectedIndexChanged事件中:
private void listBoxControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    defaultLookAndFeel1.LookAndFeel.UseWindowsXPTheme = false;
    defaultLookAndFeel1.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Skin;
    DevExpress.Skins.SkinManager.EnableFormSkins();
    DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged();

    string SkinName = listBoxControl1.SelectedItem.ToString();
    defaultLookAndFeel1.LookAndFeel.SkinName = SkinName;
}
運行可以發現效果是一樣的。
如果要禁用標題欄皮膚,可以使用SkinManager.DisableFormSkins方法,然後再調用LookAndFeelHelper.ForceDefaultLookAndFeelChanged方法,這樣可以在運行時控制是否使用標題欄皮膚。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章