C#|アプリケーションに設定機能を付ける

アプリケーションに設定機能を付けるクラスの雛形。簡単なUIも提供する。

SettingsFormのスクリーンショット

using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

public class Settings {
    private static readonly string settingFile = Application.StartupPath + "\\Settings.xml";    //設定を保存する場所を指定。
    private static Settings instance;
    private static bool loading = false;

    /// <summary>
    /// セッティングが変更されると発生
    /// </summary>
    public static event EventHandler SettingsChange;

    //シングルトン
    private Settings() { }  //コンストラクタに引数があるとシリアライズ出来ない。

    /// <summary>
    /// アプリケーションのセッティングを取得
    /// </summary>
    public static Settings Instance {
        get {
            if(instance == null) {
                loading = true;
                try {
                    XmlSerializer xs = new XmlSerializer(typeof(Settings));
                    using(StreamReader sr = new StreamReader(settingFile)) {
                        instance = ((Settings)(xs.Deserialize(sr)));
                        sr.Close();
                    }
                } catch {
                    instance = new Settings();
                }
                loading = false;
            }
            return instance;
        }
    }

    /// <summary>
    /// 設定の編集フォームを開く
    /// </summary>
    public static void OpenSettingsForm() {
        SettingsForm.Instance.Show();
        SettingsForm.Instance.Activate();
    }

    private double opacity = 0.7;                                                       //
    //[Browsable(false)]    //これを指定すると設定フォームに表示されなくなる            //
    [Category("カテゴリー"), Description("説明")]                                       //
    public double Opacity { //publicにしないとシリアライズされない                      //
        set {                                                                           //
            double preChangeValue = opacity;                                            //
            opacity = value;                                                            //これを用意したい設定の分だけ書き加える。
            if(preChangeValue != value && !loading && SettingsChange != null) {         //
                SettingsChange(instance, new EventArgs());  //変更されたら必ず呼び出す。//
            }                                                                           //
        }                                                                               //
        get {                                                                           //
            return opacity;                                                             //
        }                                                                               //
    }                                                                                   //

    /// <summary>
    /// セッティングをファイルに保存する
    /// </summary>
    public static void Save() {
        XmlSerializer xs = new XmlSerializer(typeof(Settings));
        using(StreamWriter sw = new StreamWriter(settingFile)) {
            xs.Serialize(sw, Settings.Instance);
            sw.Close();
        }
    }

    private class SettingsForm :Form {
        private static SettingsForm instance;
        private PropertyGrid propertyGrid = new PropertyGrid();

        //ここでフォームの装飾等をするといいかも。
        private SettingsForm() {
            this.Text = "設定フォーム";
            this.propertyGrid.Dock = DockStyle.Fill;
            this.Controls.Add(this.propertyGrid);
            this.propertyGrid.SelectedObject = Settings.Instance;
            Settings.SettingsChange += new EventHandler(reLoad);
        }

        public static SettingsForm Instance {
            get {
                if(instance == null || !instance.Created) instance = new SettingsForm();
                return instance;
            }
        }

        public static void ReLoad() {
            SettingsForm.Instance.reLoad(null,null);
        }

        private void reLoad(object sender,EventArgs e) {
            this.propertyGrid.RefreshTabs(PropertyTabScope.Component);
            this.propertyGrid.Refresh();
        }

        ~SettingsForm() {
            SettingsChange -= new EventHandler(reLoad);
        }
    }
}

 

このクラスは次の様に使用する。

using System;
using System.Windows.Forms;

public partial class Form1 :Form {
    public Form1() {
        InitializeComponent();

        this.Opacity = Settings.Instance.Opacity;
        Settings.SettingsChange += new EventHandler(Settings_SettingsChange);
    }

    private void button1_Click(object sender, EventArgs e) {
        Settings.OpenSettingsForm();
    }

    void Settings_SettingsChange(object sender, EventArgs e) {
        this.Opacity = ((Settings)sender).Opacity;
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
        Settings.Save();
    }
}

 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください