/////////////////////
//C# PropertyGrid.cs
/////////////////////
using System;
using System.IO;				//FileInfoオブジェクト取得の為
using System.Windows.Forms;
using System.Drawing;

namespace PropertyGridTest
{
	public partial class App : Form
	{
		PropertyGrid propGrid;
		Button exitBtn;

		[STAThread]
		public static void Main()
		{
			Application.Run(new App());
		}

		public App()
		{
			//this.AutoSize = true;	//trueにすると縮まらなくなる
			this.Width = 640;
			this.Height = 480;
			this.Text = "Property Grid";
			this.Load += App_Load;
		}

		private void App_Load(object sender, EventArgs e)
		{
			//Exitボタン
			exitBtn = new Button();
			exitBtn.Location = new Point(ClientSize.Width - exitBtn.Width - 10, ClientSize.Height - exitBtn.Height - 10);
			exitBtn.Text = "Exit";
			exitBtn.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			exitBtn.Click += exitBtn_Click;
			this.Controls.Add(exitBtn);

			//プロパティグリッド
			propGrid = new PropertyGrid();
			propGrid.Location = new Point(10, 10);
			propGrid.Size = new Size(ClientSize.Width - 20, ClientSize.Height - exitBtn.Height - 30);
			propGrid.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			//再帰的にフックを掛ける（但し全てのコントロールが反応する訳ではない）
			HookChilden(propGrid);
			//propGrid.SelectedObject = this;
			propGrid.SelectedObject = propGrid;
			//Drag and Dropでの処理
			propGrid.AllowDrop = true;
			propGrid.DragEnter += PropGrid_DragEnter;
			propGrid.DragDrop += PropGrid_DragDrop;
			this.Controls.Add(propGrid);
		}

		//終了処理
		private void exitBtn_Click(object sender, EventArgs e)
		{
			DialogResult dr = MessageBox.Show("終了しますか？", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
			if(dr == DialogResult.Yes)
				Close();	//終了する
		}

		//propGrid.MouseDown += PropertyGrid_MouseDown;	//または
		//propGrid.MouseDown += new MouseEventHandler(PropertyGrid_MouseDown);
		//propGrid.Click += PropertyGrid_Click;
		//↑のストレートな形ではPropertyGridの子（孫...）コントロールに徹底できないので、
		//次に様にHoolChildrenを使って総ての子（孫...）コントロールに徹底する
		//再帰的に子や孫コントロールへフック
		private void HookChilden(Control ctrl)
		{
			ctrl.MouseDown += PropertyGrid_MouseDown;
			propGrid.Click += PropertyGrid_Click;
			foreach (Control child in ctrl.Controls)
			{
				HookChilden(child);
			}
		}

		private void PropertyGrid_MouseDown(object sender, MouseEventArgs e)
		{	//MessageBox.Show("MouseDown");
			FileInformation();
		}

		private void PropertyGrid_Click(object sender, EventArgs e)
		{	//MessageBox.Show("Click");
			FileInformation();
		}

		private void FileInformation()
		{
			//ファイルを選択するダイアログを開く
			using(OpenFileDialog openFileDialog = new OpenFileDialog())
			{
				openFileDialog.Filter = "すべてのファイル (*.*)|*.*";
				if(openFileDialog.ShowDialog() == DialogResult.OK)
				{
					string filePath = openFileDialog.FileName;
					//FileInfoオブジェクトを作成
					FileInfo fileInfo = new FileInfo(filePath);
					//PropertyGridにFileInfoオブジェクトを設定
					propGrid.SelectedObject = fileInfo;
				}
			}
		}

		private void PropGrid_DragEnter(object sender, DragEventArgs e)
		{
			if(e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				e.Effect = DragDropEffects.Copy;
			}
		}

		private void PropGrid_DragDrop(object sender, DragEventArgs e)
		{
			string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
			if(files.Length > 0)
			{
				FileInfo fileInfo = new FileInfo(files[0]);
				propGrid.SelectedObject = fileInfo;
			}
		}
	}
}
