DevExpressを使ってWinForm肌を変える(VS)

12440 ワード

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、設計インタフェースコード例えば:BaseFormDesign.csで、指定したコントロール(Repository ItemComboBox)に手動でイベントを追加します.コードは以下の通りです.
 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

    }

}