VS.NETのデフォルトの状態ではツールボックスに表示されていないのですが、PropertyGridという便利なコントロールがあります。
このコントロールはオブジェクトが持っているプロパティを表示/設定できるようにしてくれます。
サンプルでどんなことができるか確かめてみましょう。
-------------------------------------------------------
using System;
using System.Windows.Forms;
class WinSample : Form
{
private Button mybutton;
public static void Main()
{
Application.Run(new WinSample());
}
public WinSample()
{
this.Text = "ベースウィンドウ";
this.mybutton = new Button();
this.mybutton.Location = new System.Drawing.Point(200, 200);
this.mybutton.Text = "ボタン";
this.mybutton.Click += new EventHandler(this.button_Click);
this.Controls.Add(mybutton);
}
// FormをクリックしたときはFormのプロパティを表示
protected override void OnClick(EventArgs e)
{
ShowPropertyDialog(this);
}
// ボタンをクリックしたときはボタンのプロパティを表示
private void button_Click(object sender, EventArgs e)
{
ShowPropertyDialog(mybutton);
}
private void ShowPropertyDialog(Object obj)
{
PropertyDialog pd = new PropertyDialog();
pd.SelectedObject = obj;
pd.ShowDialog();
pd.Dispose();
}
}
class PropertyDialog : Form
{
private PropertyGrid pg;
// PropertyGridに表示するオブジェクトの設定
public Object SelectedObject
{
set { this.pg.SelectedObject = value; }
}
public PropertyDialog()
{
this.Text = "プロパティダイアログ";
this.pg = new PropertyGrid();
this.pg.CommandsVisibleIfAvailable = true;
this.pg.Dock = System.Windows.Forms.DockStyle.Fill;
this.Controls.Add(pg);
}
}
-------------------------------------------------------
このサンプルでは、ボタンをクリックするとボタンのプロパティ、WindowをクリックするとWindowのプロパティが表示されます。
実行中のボタン、Windowのプロパティの設定をGUIで簡単に変更することができ、例えば WindowのBackgroundImageを変えてみたりすると、結構感動ものだったりします(^^;
VS.NETがなくても、とりあえずこれを応用すればWindow上のコントロールの配置をいろいろ試すこともできますね。
Frameworkのクラスだけでなく、自分で定義したクラスもPropertyGridを利用することができます。
-------------------------------------------------------
using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
class WinSample : Form
{
private Button mybutton;
private MyLine myLine;
public static void Main()
{
Application.Run(new WinSample());
}
public WinSample()
{
this.Text = "ベースウィンドウ";
this.mybutton = new Button();
this.mybutton.Location = new System.Drawing.Point(200, 200);
this.mybutton.Text = "設定";
this.mybutton.Click += new EventHandler(this.button_Click);
this.Controls.Add(mybutton);
// 線の初期設定
this.myLine = new MyLine();
myLine.StartP = new Point(0,0);
myLine.EndP = new Point(this.ClientSize.Width, this.ClientSize.Height);
}
// ボタンをクリックして線のプロパティを変更
private void button_Click(object sender, EventArgs e)
{
ShowPropertyDialog(myLine);
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
myLine.Draw(e.Graphics);
}
private void ShowPropertyDialog(Object obj)
{
PropertyDialog pd = new PropertyDialog();
pd.SelectedObject = obj;
pd.ShowDialog();
pd.Dispose();
}
}
class PropertyDialog : Form
{
private PropertyGrid pg;
public Object SelectedObject
{
set { this.pg.SelectedObject = value; }
}
public PropertyDialog()
{
this.Text = "プロパティダイアログ";
this.pg = new PropertyGrid();
this.pg.CommandsVisibleIfAvailable = true;
this.pg.Dock = System.Windows.Forms.DockStyle.Fill;
this.Controls.Add(pg);
}
}
// ウィンドウ上に表示される線のクラス
class MyLine
{
private Point startP;
private Point endP;
private int linew = 1;
[CategoryAttribute("位置")]
public Point StartP
{
get { return startP; }
set { startP = value; }
}
[CategoryAttribute("位置")]
public Point EndP
{
get { return endP; }
set { endP = value; }
}
[CategoryAttribute("太さ")]
public int LineWidth
{
get { return linew; }
set { linew = value; }
}
// 与えられた領域に線を描く
public void Draw(Graphics g)
{
Pen pen = new Pen(Color.Blue, linew);
g.DrawLine(pen, startP, endP);
pen.Dispose();
}
}
-------------------------------------------------------
Window上に引かれている線の位置と太さを実行中に自由に変えることができるのが確かめられると思います。
あおい情報システム株式会社 小野修司(どっとねっとふぁん)