テンプレートの編集
Visual Studioでプログラムを新規に組むときは、大抵「Windows アプリケーション」などのテンプレートを指定して作成し始めると思います。 このテンプレートを使用して作成される雛型に不満を感じている方は少なないと思います。 確かに初めのうちはわからないことだらけなので、例えば、「Windows フォーム デザイナ サポートに必要です。」 などのコメントも有り難く感じられますが、次期に鬱陶しくなってきます(^^; そこで、ここではこのテンプレートを編集して自分好みのテンプレートに編集してみます。
アイディア次第で様々なことに応用が利く
方法自体は非常に簡単です。まず、Visual Studio .NETがインストールされているフォルダ以下を次のようにたどります。 VC#\VC#Wizards\CSharpEXEWiz\Templates\1041\ (1041は言語ID)するとこのフォルダ内にfiles1.csというファイルがあります。 これを開いてみると次のように書いてあります。
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace [!output SAFE_NAMESPACE_NAME]
{
/// <summary>
/// [!output SAFE_CLASS_NAME] の概要の説明です。
/// </summary>
public class [!output SAFE_CLASS_NAME] : System.Windows.Forms.Form
{
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public [!output SAFE_CLASS_NAME]()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、
コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "[!output SAFE_CLASS_NAME]";
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new [!output SAFE_CLASS_NAME]());
}
}
}
「Windows アプリケーション」のテンプレートから生成される雛型にそっくりなコードですが、 これがテンプレートの本体です。従って、このファイルを書き換えれば、いつでも自分好みの雛型からはじめることができるということです。 自分は次のように書き換えて使用しています。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
#endregion
namespace [!output SAFE_NAMESPACE_NAME]
{
/// <summary>
/// [!output SAFE_CLASS_NAME]
/// </summary>
public class [!output SAFE_CLASS_NAME] : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
#region コンストラクタ
public [!output SAFE_CLASS_NAME]()
{
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、
コンストラクタ コードを追加してください。
//
}
#endregion
#region Dispose メンバ
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region Windows フォーム デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "[!output SAFE_CLASS_NAME]";
}
#endregion
#region メインエントリポイント
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new [!output SAFE_CLASS_NAME]());
}
#endregion
}
}
また、この他にも様々なテンプレートが存在しますが、それらも好きなようにカスタマイズできます。例えば、 「クラスの追加」テンプレートを新たに作成してデザインパターンの雛型を作成しておけば、意外と重宝すると思います。 このページが参考になるでしょう。
VS2005のコードスニペットにも期待
最後にVS2005の話題ですが、こちらも色々とIDEの強化が図られているようで、 例えば、コードスニペットという機能があります。これはコードに何度でも現れてくるような定型区をキャレットの位置に雛型として 挿入してくれる機能ですが、現在Beta版では VC#\Expansions\1041\Expansions というフォルダ内に XML形式でその本体が存在します。 従って、ここに自分独自のスニペットを登録しておけば、先のデザインパターンの雛型など、 テンプレート以上に強力なものとなるでしょう。