- 題名: リストビューへのドロップ
- 日時: 2008/03/07 17:13:05
- ID: 21601
- この記事の返信元:
- (なし)
- この記事への返信:
- [21602] Re[1]: リストビューへのドロップ2008/03/07 17:18:42
- ツリーを表示
はじめまして、引っ込んだ略と申します。
以下のコードで試してみてください。
[ここから投稿末尾まで内容を差し替え]
コードをバージョンアップしたため、コードを全て差し替えました。
using System.IO;
public class Form2_2:Form
{
private ListView listView1 = new ListView();
public Form2_2()
{
this.Text = this.GetType().Name;
this.Size = new Size(450, 300);
this.listView1.DragEnter += new DragEventHandler(this.ListView1_DragEnter);
this.listView1.DragDrop += new DragEventHandler(this.ListView1_DragDrop);
Application.Idle+=new EventHandler(this.Application_Idle);
this.listView1.AllowDrop = true;
this.listView1.Columns.Add("ファイル名");
this.listView1.Columns.Add("パス名");
this.listView1.Columns.Add("ファイルサイズ");
this.listView1.Dock = DockStyle.Fill;
this.listView1.View = View.Details;
this.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
this.Controls.Add(listView1);
DriveInfo di=new DriveInfo("J:\\");
}
private enum DroppedType
{
None, File, Folder, ReadyDrive, NotReadyDrive
}
// コンテナの定義
private class MyData
{
public DroppedType flag;
public string filename;
public MyData(DroppedType flg,string name)
{
this.flag=flg;
this.filename=name;
}
}
private Queue<MyData> _myqueue = new Queue<MyData>();
// ここで詳細情報取得とListViewへのItem追加を行う。
private void Application_Idle(object sender, EventArgs e)
{
this.DoTask();
}
private void DoTask()
{
while (_myqueue.Count >= 1)
{
MyData md = _myqueue.Dequeue();
switch (md.flag)
{
case DroppedType.File: { this.OnDroppedFile(md.filename); break; }
case DroppedType.Folder: { this.OnDroppedFolder(md.filename); break; }
case DroppedType.ReadyDrive: { this.OnDroppedReadyDrive(md.filename);
break; }
case DroppedType.NotReadyDrive: { this.OnDroppedNotReadyDrive(md.filename);
break; }
}
}
}
private void ListView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void ListView1_DragDrop(object sender, DragEventArgs e)
{
object o = e.Data.GetData(DataFormats.FileDrop,false);
if (o == null) { return; }
string[] ssa = o as string[];
if (ssa == null){return;}
if (ssa.Length <= 0) { return; }
// 複数個のアイコンを一度にDropした場合に対応
foreach (string filename in ssa)
{
// Dropしたfilenameの種類を判断
// ※ とりあえずQueueにファイル名を格納しておき、
// ※ Appication_Idleイベントハンドラ内でその後の処理を行う。
if (File.Exists(filename) == true)
{
// ファイルをDropした
_myqueue.Enqueue(new MyData(DroppedType.File, filename));
}
else if (Path.GetPathRoot(filename) == filename)
{
DriveInfo dri = new DriveInfo(filename);
if (dri.IsReady == true)
{
// DriveアイコンをDropした
_myqueue.Enqueue(new MyData(DroppedType.ReadyDrive, filename));
}
else
{
// 準備のできていないDriveのアイコンをDropした
_myqueue.Enqueue(new MyData(DroppedType.NotReadyDrive, filename));
}
}
else if (Directory.Exists(filename) == true)
{
// フォルダをDropした
_myqueue.Enqueue(new MyData(DroppedType.Folder, filename));
}
}
}
// 詳細情報を取得してListViewに表示
private void OnDroppedFile(string name)
{
FileInfo fi = new FileInfo(name);
long filesize = fi.Length;
string[] ssb = new string[] { Path.GetFileName(name),
Path.GetDirectoryName(name), String.Format("{0} バイト", filesize) };
listView1.Items.Add(new ListViewItem(ssb));
this.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
private void OnDroppedFolder(string name)
{
DirectoryInfo di = new DirectoryInfo(name);
string[] ssb = new string[] { "(ディレクトリ)", name, "(サイズは省略)" };
listView1.Items.Add(new ListViewItem(ssb));
this.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
private void OnDroppedReadyDrive(string name)
{
DriveInfo dri = new DriveInfo(name);
DirectoryInfo di = new DirectoryInfo(name);
long freespace = dri.TotalFreeSpace;
string[] ssb = new string[] { "(ドライブ)",
name, string.Format("空き領域は {0} バイト",freespace)};
listView1.Items.Add(new ListViewItem(ssb));
this.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
private void OnDroppedNotReadyDrive(string name)
{
string[] ssb = new string[] { "(ドライブの準備ができていません)",
name, "(サイズは省略)" };
listView1.Items.Add(new ListViewItem(ssb));
this.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
}
以上です。
分類:[.NET]
2008/03/07(Fri) 17:14:09 編集(投稿者)
ファイルをリストビューへドラッグ&ドロップして、ファイル名、ファイルの場所、ファイルのサイズをリストビューで表示するようにしたいのですがどうしていいのかわかりません。
どのような処理すれば良いでしょうか?
現在は下記のような状態です。
private void ListView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void ListView1_DragDrop(object sender, DragEventArgs e)
{
string[] filename
= (string[])e.Data.GetData(DataFormats.FileDrop, false);
ListView1.Items.Add(new ListViewItem(new string[] { filename }));
}