using System.IO;
using System.Windows.Forms;
namespace WinFormFile
{
public partial class FormProperty : Form
{
private PropertyGrid propertyFile;
private OpenFileDialog openFile;
public FormProperty()
{
InitializeComponent();
this.AllowDrop = true;
this.HelpButton = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.SizeGripStyle = SizeGripStyle.Hide;
this.StartPosition = FormStartPosition.CenterScreen;
openFile = new OpenFileDialog();
openFile.Filter = "所有文件 (*.*)|*.*";
propertyFile = new PropertyGrid();
propertyFile.Dock = DockStyle.Fill;
this.Controls.Add(propertyFile);
}
protected override void OnDragEnter(DragEventArgs e)
{
base.OnDragEnter(e);
DataObject data = e.Data as DataObject;
if (data.ContainsFileDropList())
{
string path = data.GetFileDropList()[0];
if ((File.GetAttributes(path) & FileAttributes.Directory) == 0)
propertyFile.SelectedObject = new FileInfo(path);
else
propertyFile.SelectedObject = new DirectoryInfo(path);
this.Text = propertyFile.SelectedGridItem.Value as string;
}
}
protected override void OnHelpButtonClicked(CancelEventArgs e)
{
base.OnHelpButtonClicked(e);
e.Cancel = true;
if (openFile.ShowDialog(this) == DialogResult.OK)
{
propertyFile.SelectedObject = new FileInfo(openFile.FileName);
this.Text = propertyFile.SelectedGridItem.Value as string;
}
}
}
}