画像のドラッグ&ドロップ(1)
同じプロジェクトを使用して、デフォルトのForm1にPictureBoxコントロールをドラッグ&ドロップします(図5を参照)。

Form1_Loadイベントで、PictureBoxコントロールのプロパティを次のように設定します。
Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load '---set the control to allow drop--- TextBox1.AllowDrop = True '---set the control to allow drop--- With PictureBox1 .AllowDrop = True .BorderStyle = BorderStyle.FixedSingle .SizeMode = PictureBoxSizeMode.StretchImage End With ...
ドロップの実装
前の例と同じように、マウスがPictureBoxコントロール上にあるときはマウスポインタが適切に変更されるように、DragEnterイベントにコードを記述します。今回の処理で1つだけ異なるのは、PictureBoxコントロールにドロップされるデータのタイプが画像かどうかをチェックする点です。
Private Sub PictureBox1_DragEnter( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms. _ DragEventArgs) _ Handles PictureBox1.DragEnter '---if the data to be dropped is an image format--- If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then '---determine if this is a copy or move--- If (e.KeyState And CtrlMask) = CtrlMask Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.Move End If '---change the border style of the control--- PictureBox1.BorderStyle = BorderStyle.Fixed3D End If End Sub
DragDropイベントでは、まず、ドロップされたオブジェクトのタイプが画像であることを確認します。画像である場合は、ドロップされた画像をPictureBoxコントロールに割り当てます。
Private Sub PictureBox1_DragDrop( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms. _ DragEventArgs) _ Handles PictureBox1.DragDrop '---if the data to be dropped is a bitmap format--- If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then '---set the control to display ' the text being dropped--- PictureBox1.Image = e.Data.GetData( _ DataFormats.Bitmap) End If '---set the border style back to its original--- PictureBox1.BorderStyle = BorderStyle.FixedSingle End Sub
最後にDragLeaveイベントで、PictureBoxの境界線のスタイルを元のスタイルに変更します。
Private Sub PictureBox1_DragLeave( _ ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles PictureBox1.DragLeave '---set the borderstyle back to ' its original--- PictureBox1.BorderStyle = _ BorderStyle.FixedSingle End Sub
F5キーを押してアプリケーションをテストします。Microsoft Wordから画像をドラッグしてPictureBoxコントロールにドロップすると、その画像が表示されます。面白いことに、ワードパッドから画像をドラッグ&ドロップしてもPictureBoxに画像は表示されません。これについては後ほど説明します。
ドラッグの実装
PictureBoxコントロールに表示された画像を別の場所、例えば別のコントロールやMicrosoft Word(またはワードパッド)にドラッグできるようにするには、もう少しコードを追加する必要があります。
まず、MouseDownイベントハンドラに次のコードを記述します。PictureBoxコントロールの画像をクリックすると、このイベントが発生します。
Private Sub PictureBox1_MouseDown( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms. _ MouseEventArgs) _ Handles PictureBox1.MouseDown If PictureBox1.Image IsNot _ Nothing Then PictureBox1.DoDragDrop( _ PictureBox1.Image, _ DragDropEffects.Move Or _ DragDropEffects.Copy) End If End Sub
このコードでは、PictureBoxコントロールに表示された画像をコピーするために、PictureBoxコントロールのDoDragDrop()メソッドを使用します。このメソッドの2番目のパラメータには、生じる可能性のあるドラッグ操作のタイプ(ここでは移動またはコピー)を指定します。
作業はこれで完了です。この時点で、PictureBoxコントロールに表示された画像を別のコントロールにドラッグ&ドロップできるようになります。今度は、画像をワードパッド上にドラッグできることが分かるはずです。ただし、先ほどとは逆に、Microsoft Word上にはドラッグできません。
ここまでのアプリケーションの動作をまとめてみましょう。
- Microsoft Wordから画像をドラッグして、
PictureBoxコントロールにドロップできます。しかし、ワードパッドからドラッグ&ドロップすることはできません。 PictureBoxコントロールからWord上に画像をドラッグしても、Wordは画像を受け入れません。しかし、同じ画像をワードパッド上にドラッグ&ドロップできます。
なぜWordからは画像をドロップできて、ワードパッドからはできないのでしょうか。その理由を理解するために、アプリケーションに少し手を加えて、ドロップ操作によってPictureBoxコントロールに渡されるデータのタイプを具体的に確認できるようにしましょう。
