SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

japan.internet.com翻訳記事

Windowsフォームアプリケーションにドラッグ&ドロップ機能を実装する

わずかな手間でユーザビリティがぐっと向上

ファイルのドラッグ&ドロップ

 Windowsアプリケーション共通の操作の一つに、アプリケーション上にファイルをドラッグ&ドロップする操作があります。例えば、フォルダ上にファイルをドラッグ&ドロップしたり、Windows Media Player上に音楽ファイルをドラッグして再生したことがあるのではないでしょうか。

 サンプルアプリケーションに手を加えて、ユーザーがWindowsエクスプローラから画像ファイルをドラッグして、PictureBoxコントロールにドロップできるようにしましょう。そのためには次の2つのステップが必要です。

  • まず、DragEnterイベントを修正します(リスト2を参照)。WindowsエクスプローラからドラッグされるファイルのデータタイプはFileDropです。従って、リスト2のコードでは、このデータタイプをDragEnterイベントハンドラでチェックします。
  • 次に、DragDropイベントをリスト3のように変更し、画像ファイルを開いてその内容をPictureBoxコントロールに表示できるようにします。
リスト2
Private Sub PictureBox1_DragEnter( _
   ByVal sender As Object, _
   ByVal e As System.Windows.Forms. _
   DragEventArgs) _
   Handles PictureBox1.DragEnter

   Dim formats As String() = e.Data.GetFormats
   '---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
   ElseIf (e.Data.GetDataPresent( _
      DataFormats.FileDrop)) Then
      '---if this is a file drop---
      e.Effect = DragDropEffects.All
   ElseIf (e.Data.GetDataPresent( _
      DataFormats.Rtf)) 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
リスト3
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 image format---
   If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
      '---set the control to
      ' display the bitmap being dropped---
      PictureBox1.Image = e.Data.GetData( _
         DataFormats.Bitmap)
   ElseIf (e.Data.GetDataPresent( _
      DataFormats.FileDrop)) Then
      Dim files() As String
      files = e.Data.GetData( _
         DataFormats.FileDrop)
      For Each file As String In files
         file = UCase(file)
         If file.EndsWith(".GIF") Or _
            file.EndsWith(".JPG") Or _
            file.EndsWith(".BMP") Then
            PictureBox1.Image = New Bitmap(file)
         End If
         System.Threading.Thread.Sleep(2000)
            Application.DoEvents()
      Next
   ElseIf (e.Data.GetDataPresent(DataFormats.Rtf)) Then
      '---display the rich text---
      Console.WriteLine(e.Data.GetData( _
         DataFormats.Rtf))
   End If
   '---set the borderstyle back to its original---
   PictureBox1.BorderStyle = BorderStyle.FixedSingle
End Sub

 必要な作業はこれだけです。これで、Windowsエクスプローラから画像ファイルをドラッグしてPictureBoxコントロールにドロップし、その内容を表示できます。今回変更したコードでは、PictureBoxコントロールに1つまたは複数のファイルをドロップできることに注意してください。複数ファイルをドロップした場合、それぞれの画像が2秒間隔で1つずつ表示されます。

次のページ
特別なコントロールにドラッグ&ドロップを実装

この記事は参考になりましたか?

japan.internet.com翻訳記事連載記事一覧

もっと読む

この記事の著者

japan.internet.com(ジャパンインターネットコム)

japan.internet.com は、1999年9月にオープンした、日本初のネットビジネス専門ニュースサイト。月間2億以上のページビューを誇る米国 Jupitermedia Corporation (Nasdaq: JUPM) のニュースサイト internet.comEarthWeb.com からの最新記事を日本語に翻訳して掲載するとともに、日本独自のネットビジネス関連記事やレポートを配信。

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

Wei-Meng Lee(Wei-Meng Lee)

Microsoft MVP受賞者。Microsoft社の最新テクノロジー実地研修を専門とするDeveloper Learning Solutions社を創設。.NETとワイヤレステクノロジーの開発者、指導者として知られる。国際的なカンファレンスでたびたび講演し、.NET、XML、ワイヤレステクノロジーに関す...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/2653 2008/07/18 10:02

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー