使用DevExpress改变WinForm皮肤(VS)

基于步入DevExpress的使用(VS),进一步使用DevExpress改变WinForm皮肤,适合初学者。

提示:

1、对于DevExpress菜单中的RepositoryItemComboBox没有EditValue属性,无法直接获取选择的值,但可以在其事件中将其转化为ComboBoxEdit控件来获取。如下:

private void repositoryItemComboBox1_SelectedValueChanged(object sender, EventArgs e)
{ 
     ComboBoxEdit riComboBox = sender as ComboBoxEdit;
     string skinName = riComboBox.EditValue.ToString();
} 

2、在设计界面代码如:BaseFormDesigner.cs中,手动给指定控件(RepositoryItemComboBox)添加事件,代码如下:

 this.repositoryItemComboBox1.SelectedValueChanged += new System.EventHandler(this.repositoryItemComboBox1_SelectedValueChanged);

  在对应的BaseForm.cs中实现其具体功能,代码如下:

        /// <summary>
        /// 手动添加的 Combobox菜单项值改变时,触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemComboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            ComboBoxEdit repositoryItemComboBox = sender as ComboBoxEdit;
            this.defaultLookAndFeel1.LookAndFeel.SkinName = repositoryItemComboBox.EditValue.ToString();     
        }

 

代码间关系:Program类主要注册要使用的皮肤及运行那个界面;CommonFunctions类主要实现共用的函数;BaseForm类继承自DevExpress.XtraEditors.XtraForm,主要实现一些基础共用的操作;SkinSubject类主要积累那些类共用BaseForm类的通用操作或共性(单例、简单观察者来实现);AppFormA、AppFormB类都继承自BaseForm类,共用BaseForm类特性。

 

具体实例代码(不含界面设计代码)如下:

 

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace TestExpressSkins
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.Skins.SkinManager.EnableFormSkins();
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new BaseForm());
            //Application.Run(new AppFormA());
            Application.Run( AppFormA.Singlon());
        }
    }
}

  

CommonFunctions.cs

using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestExpressSkins
{
    class CommonFunctions
    {
        #region 字段

        

        #endregion

        #region 单例

        private static CommonFunctions commFuncInstance = null;

        private CommonFunctions()
        {
        }

        public static CommonFunctions Singlon()
        {
            if (null == commFuncInstance)
            {
                commFuncInstance = new CommonFunctions();
            }
            return commFuncInstance;
        }

        #endregion

        #region 共有方法
        /// <summary>
        /// 皮肤全部枚举出来放到一个ComboBoxEdit中
        /// </summary>
        /// <param name="comboBoxEdit"></param>
        public void AddAppStyles2ComboBoxEdit(ComboBoxEdit comboBoxEdit)
        {
            foreach (DevExpress.Skins.SkinContainer skin in DevExpress.Skins.SkinManager.Default.Skins)
            {
                comboBoxEdit.Properties.Items.Add(skin.SkinName);
            }
        }

        /// <summary>
        /// 皮肤全部枚举出来放到一个ComboBoxEdit中
        /// </summary>
        /// <param name="repositoryItemComboBox"></param>
        public void AddAppStyles2RepositoryItemComboBox(RepositoryItemComboBox repositoryItemComboBox)
        {
            foreach (DevExpress.Skins.SkinContainer skin in DevExpress.Skins.SkinManager.Default.Skins)
            {
                repositoryItemComboBox.Items.Add(skin.SkinName);
            }
        }


        public void AddAppStyles2BarEditItem(BarEditItem barEditItem)
        {
            string editItemType = barEditItem.GetType().ToString();
            switch (editItemType)
            {
                case "RepositoryItemComboBox":
                    AddAppStyles2RepositoryItemComboBox(barEditItem.Edit as RepositoryItemComboBox);
                    break;
                case "RepositoryItem**":

                    break;
                case "RepositoryItem***":
                    
                    break;

            }
        }



        #endregion

        #region 私有方法


        public bool tmpFunc()
        {
            bool bFlag = true;

            return bFlag;
        }


        #endregion
    }
}

 SkinSubject.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestExpressSkins
{
    public class SkinSubject
    {
        #region 字段


        private List<BaseForm> forms = new List<BaseForm>();        //同一样式的对话框集合


        #endregion

        #region 单例

        private static SkinSubject subject = null;

        public static SkinSubject GetInstance()
        {
            if (subject == null) subject = new SkinSubject();
            return subject;
        }

        private SkinSubject() { }

        #endregion

        #region 共有方法
        /// <summary>
        /// 注册观察者
        /// </summary>
        /// <param name="f"></param>
        public void Register(BaseForm f)
        {
            forms.Add(f);
        }

        /// <summary>
        /// 注销观察者
        /// </summary>
        /// <param name="f"></param>
        public void UnRegister(BaseForm f)
        {
            forms.Remove(f);
        }

        /// <summary>
        /// 修改每个观察者的皮肤
        /// </summary>
        /// <param name="skinName"></param>
        public void Notify(string skinName)
        {
            foreach (BaseForm f in forms)
            {
                f.LookAndFeelControl.LookAndFeel.SkinName = skinName;
            }
        }


        #endregion

        #region 私有方法



        #endregion
    }
}

  

BaseForm.cs

using DevExpress.LookAndFeel;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestExpressSkins
{
    public partial class BaseForm : DevExpress.XtraEditors.XtraForm            //Form
    {
        #region 字段

        private CommonFunctions commFunc = null;

        #endregion

        #region 属性

        public DefaultLookAndFeel LookAndFeelControl
        {
            set
            {
            }

            get
            {
                return this.defaultLookAndFeel1;
            }
        }

        public int tempA
        {
            set
            {
            }

            get
            {
                return 1;
            }
        }

        #endregion

        #region 构造函数

        public BaseForm()
        {
            InitializeComponent();
            commFunc = CommonFunctions.Singlon();
        }

        #endregion

        #region 对话框或空间相关方法

        private void Form1_Load(object sender, EventArgs e)
        {
            commFunc.AddAppStyles2ComboBoxEdit(cmbAppStyle);
            commFunc.AddAppStyles2RepositoryItemComboBox(this.repositoryItemComboBox1);
        }

        private void cmbAppStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.defaultLookAndFeel1.LookAndFeel.SkinName = cmbAppStyle.EditValue.ToString();
        }

        /// <summary>
        /// 手动添加的 Combobox菜单项值改变时,触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemComboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            ComboBoxEdit repositoryItemComboBox = sender as ComboBoxEdit;
            this.defaultLookAndFeel1.LookAndFeel.SkinName = repositoryItemComboBox.EditValue.ToString();     
        }

        #endregion

        #region 私有方法



        #endregion
    }
}

  

AppFormA.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestExpressSkins
{
    public partial class AppFormA : BaseForm            //Form
    {
        #region 字段

        BaseForm appFormB = null;

        #endregion

        #region 属性

        public int tempA
        {
            set
            {
            }

            get
            {
                return 1;
            }
        }

        #endregion

        #region 单例

        private static AppFormA appFormAInstance = null;

        #region 构造函数

        private AppFormA()
        {
            InitializeComponent();
            SkinSubject.GetInstance().Register(this);
        }

        #endregion

        public static AppFormA Singlon()
        {
            if (null == appFormAInstance)
            {
                appFormAInstance = new AppFormA();
            }
            return appFormAInstance;
        }

        #endregion

        #region 对话框或空间相关方法

        private void bGo2B_Click(object sender, EventArgs e)
        {
            //if (null == appFormB)
            //{
            //    appFormB = new AppFormB();
            //}
            appFormB = AppFormB.Singlon();
            this.Hide();
            appFormB.Show();
        }

        #endregion

        #region 私有方法



        #endregion
    }
}

  

 

AppFormB.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestExpressSkins
{
    public partial class AppFormB : BaseForm            //Form
    {
        #region 字段

        BaseForm appFormA = null;

        #endregion

        #region 属性

        public int tempA
        {
            set
            {
            }

            get
            {
                return 1;
            }
        }

        #endregion
        
        #region 单例

        private static AppFormB appFormBInstance = null;

        #region 构造函数

        private AppFormB()
        {
            InitializeComponent();
            SkinSubject.GetInstance().Register(this);
        }

        #endregion

        public static AppFormB Singlon()
        {
            if (null == appFormBInstance)
            {
                appFormBInstance = new AppFormB();
            }
            return appFormBInstance;
        }

        #endregion

        #region 对话框或空间相关方法

        private void bGo2A_Click(object sender, EventArgs e)
        {
            //if (null == appFormA)
            //{
            //    appFormA = new AppFormA();
            //}
            appFormA = AppFormA.Singlon();
            this.Hide();
            appFormA.Show();
        }

        #endregion

        #region 私有方法



        #endregion
    }
}

  

 

使用DevExpress改变WinForm皮肤(VS)

上一篇:C#反射


下一篇:10-23C#基础--特殊集合(stack、queue、hashtable)