【使い方】
例:Explorer からファイルをドラッグ&ドロップしてその内容を読込む。
/// <summary>
/// ドラッグ&ドロップの開始イベントを処理します。
/// </summary>
protected override void OnDragEnter( DragEventArgs e )
{
//ユーザーがファイルをドラッグしているところかどうかを確認します。
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
//Debug.WriteLine("OnDragEnter:");
e.Effect = e.AllowedEffect & DragDropEffects.Copy;
}
//基本コントロールのイベント起動
base.OnDragEnter( e );
}
/// <summary>
/// ドラッグ&ドロップイベントを処理します。
/// </summary>
protected override void OnDragDrop( DragEventArgs e )
{
//ユーザーがファイルをドラッグしているところかどうかを確認します。
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
//ドラッグしたファイル名を取得する
string[] strFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
//Debug.WriteLine("OnDragDrop:" + strFiles.Length.ToString());
//ドラッグしたファイル数を確認、複数ファイルの場合、メッセージを表示してExit
if ( strFiles.Length > 1 )
{
MessageBox.Show("複数のファイルがドラッグ&ドロップされました、何れか1つで再度実行して下さい。", "エラー",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
//ファイルストリームでイメージデータ読み込みの初期化
FileStream fs = new FileStream(strFiles[0], FileMode.Open, FileAccess.Read);
//イメージデータ読み込みに必要なByteサイズを設定
sourceImage = new byte[fs.Length];
//イメージデータをバイナリ ラージ オブジェクト (BLOB)としてByte変数に読み込み
fs.Read(sourceImage, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
}
catch
{
sourceImage = null;
MessageBox.Show("ドラッグしたファイルの読込みができません。", "エラー",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//基本コントロールのイベント起動
base.OnDragDrop(e);
}
|