目次
プログラムTips
dllを別のフォルダに置く
ソリューションエクスプローラのプロジェクト上で右クリック>追加>新しい項目>アプリケーション構成ファイル <?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" > <probing privatePath="dll" /> </assemblyBinding> </runtime> </configuration> TreeViewで複数ノードの選択
ドッキングウィンドウ(DockPanel Suite)
VisualStudio風のドッキング可能なウィンドウ(DockingWindow)を作りたい場合、 「DockPanel Suite」を使用すると楽。
ウィンドウレイアウトの保存方法
アプリケーション終了時にウィンドウレイアウトを保存し、次回起動時に復帰させることができます。 const string SAVE_LAYOUT_PATH = @"SaveLayout.xml"; /// <summary> /// フォームが読み込まれた時に呼ばれる /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainForm_Load(object sender, EventArgs e) { // フォームの生成 { // プレビュー m_previewForm = new PreviewForm(); // テクスチャ m_textureForm = new TextureForm(); // ノードツリー m_nodeForm = new NodeForm(); } // ウィンドウレイアウトXMLが存在していたら読み出して適用する if (System.IO.File.Exists(SAVE_LAYOUT_PATH)) { WeifenLuo.WinFormsUI.Docking.DeserializeDockContent deserializeDockContent = new WeifenLuo.WinFormsUI.Docking.DeserializeDockContent(GetDockContentFromPersistString); this.dockPanel1.LoadFromXml(SAVE_LAYOUT_PATH, deserializeDockContent); } else { // XMLがなければデフォルト m_previewForm.Show(this.dockPanel1, WeifenLuo.WinFormsUI.Docking.DockState.Document); m_textureForm.Show(this.dockPanel1, WeifenLuo.WinFormsUI.Docking.DockState.Float); m_nodeForm.Show(this.dockPanel1, WeifenLuo.WinFormsUI.Docking.DockState.DockLeft); } } /// <summary> /// ウィンドウの名称に対応するDockContentを返す /// </summary> /// <param name="persistString"></param> /// <returns></returns> WeifenLuo.WinFormsUI.Docking.IDockContent GetDockContentFromPersistString(string persistString) { if (persistString == typeof(PreviewForm).FullName) return m_previewForm; if (persistString == typeof(TextureForm).FullName) return m_textureForm; if (persistString == typeof(NodeForm).FullName) return m_nodeForm; return null; } /// <summary> /// フォームが閉じられた後に呼ばれる /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { this.dockPanel1.SaveAsXml(SAVE_LAYOUT_PATH); } その他のドッキングウィンドウ
描画の更新頻度を上げる
デフォルトのままでは描画の更新が毎フレーム呼ばれないので、
別の方法として、 System.Windows.Forms.Application.Idle += delegate { Invalidate(); }; という方法もある。 Dictionary の初期値設定
C# 3.0 から、以下のような感じで初期化(初期値の設定)ができるようになった。 using System.Collections.Generic; class Hoge { Dictionary<string, string> dict = new Dictionary<string, string>() { {"txt", "notepad.exe"}, {"bmp", "paint.exe"}, {"dib", "paint.exe"}, }; } ローカル変数であれば、varキーワードが使える。 public void Method() { var dict = new Dictionary<string, string>() { {"txt", "notepad.exe"}, {"bmp", "paint.exe"}, {"dib", "paint.exe"}, }; } PropertyGridコントロール
並び順を制御する
デフォルトの状態では勝手に要素がソートされてしまう為、以下のようにして /// <summary> /// プロパティグリッドのプロパティの並び順をコントロールする。 /// </summary> class PropertyGridSortTypeConverter : TypeConverter { public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { // TypeDescriptorを使用してプロパティ一覧を取得する PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes); // プロパティの並び順を指定する。 string[] sortOrder = { "FormatImageDir", "ImageDir", "PrintDataDir", "ImageNoSettingFLG", }; // ソートする。 return pdc.Sort(sortOrder); // return base.GetProperties(context, value, attributes); } /// <summary> /// GetPropertiesをサポートしていることを表明する。 /// </summary> /// <param name="context"></param> /// <returns>常にtrue</returns> public override bool GetPropertiesSupported(ITypeDescriptorContext context) { return true; } } //------------------------------------------------------------------------------------------------------------- // プロパティ定義部 [TypeConverter(typeof(PropertyGridSortTypeConverter))] public class HogeProperty { public int FormatImageDir { get; set; } public int ImageDir { get; set; } public int PrintDataDir { get; set; } public int ImageNoSettingFLG { get; set; } } 参考
AutoScroll の位置がリセットされないようにする
スクロールしたあと、別のフォームにフォーカスした後フォーカスを戻すと、 protected override Point ScrollToControl(Control activeControl) { return this.AutoScrollPosition; } 色選択コントロール
色選択のカスタムコントロール。カスタムカラーピッカー(Custom color picker control)
以下、WPF ASP.NETなど 自作参考 スライダー/トラックバーコントロール
スライダーslider と トラックバーtrackbar
リストビューコントロール
List View Control
グリッドコントロール
Grid Control
その他コントロール
難読化ツール
トラブルシューティング
|