アプリ版:「スタンプのみでお礼する」機能のリリースについて

VisualBasic2008を使い、画像のドラッグ&ドロップをしたい
まだVB2008の勉強を始めたばかりの初心者です。

PictureBox1~PictureBox4に表示されている画像をドラッグ&ドロップでPictureBox5にコピーして表示させたいのですが、どのようなプログラムにすればいいのかが分かりません。

PictureBox5には何度でもPictureBox1~PictureBox4の画像をドラッグ&ドロップで入れ替えができ、PictureBox1~PictureBox4の画像はそのままになるようにしたいです。

よろしくお願いします。

A 回答 (1件)

PictureBoxのAllowDropプロパティを Trueにすれば D&D(ドラッグ&ドロップ)が可能になります


D&Dを行うには 元のPictureBox1-4のMouseDownイベントで DoDragDropを使えば実行できるでしょう

' 送り手側
private Sub PictureBox_MouseDown(ByVal s as Object, byVal e as MouseEventArgs) _
  Handler PictureBox1.MouseDown, PictureBox2.MouseDown,
  PictureBox3.MouseDown, PictureBox4.MouseDown

  Dim oPic as PictureBox = CType(s, PictureBox )
  ' 受け側のD&Dを許可
  PictureBox5.AllowDrop = True
  ' D&Dを実行
  oPic.DoDragDrop( oPic.Image, DragDropEffect.Copy )
End Sub

' 受けて側
Private Sub PictureBox5_DragEnter( s as Object, ByVal e as DragEventArgs )
  ' もしD&Dのデータがビットマップなら
  if e.Data.GetDataPresent("Bitmap") then
   ' カーソルを変更
e.Effect = DragDropEffect.Copy
  else
   ' カーソルを禁止マークに
e.Effect = DragDropEffect.None
  end if
End Sub

Private Sub PIctureBox5_DragDrop( s as Object, ByVal e as DragEventArgs )
  ' もしD&Dのデータがビットマップなら
  if e.Data.GetDataPresent("Bitmap") then
    PictureBox5.Image = e.Data.GetData("Bitmap")
  end if
End Sub

といった具合で出来そうです
エラー処理を何もしていませんの必要な箇所の修正をしましょう
字下げ(インデント)には全角スペースを使っていますので エラーになるようでしたら置換してください
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!