• 締切済み

[VB] ブラウザからの D&D について

こんにちは。VisualBasic2008で画像を扱うWindowsアプリケーションを開発しているのですが、 IE や FireFox などのブラウザでWebページなどを表示した際、 そのWebページに含まれる画像をフォームにドラッグ&ドロップすることによって 取得することがうまくいきません。 'Me.AllowDrop=True 'PictureAdd(Image as Bitmap) は画像を追加するものです。 Private Sub Form1_DragEnter(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter If e.Data.GetDataPresent(GetType(Bitmap)) Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub Form1_DragDrop(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop, Panel1.DragDrop Try PictureAdd(e.Data.GetData(GetType(Bitmap))) Catch ex As Exception MessageShow("エラー: " & ex.Message) End Try End Sub

みんなの回答

回答No.1

完全な回答ではありません。アドバイスというよりも参考意見として。 画像をドラッグ&ドロップしたときに、インターネット一時ファイルとして保存された画像ファイルのパスであれば取得できました。(IEのみ、FireFoxは未検証) Private Sub Form1_DragEnter(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles   If e.Data.GetDataPresent("FileName") Then     e.Effect = DragDropEffects.Copy   End If End Sub Private Sub Form1_DragDrop(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles   If e.Data.GetDataPresent("FileName") Then     Dim strFileName As String() = CType(e.Data.GetData("FileName"), String())     Dim bmpLoad As New Bitmap(strFileName(0))     PictureBox1.Image = bmpLoad   End If End Sub ただ、リンクが張られている画像などではうまく取れません。 DragEnterイベント内で   For Each strEnum As String In e.Data.GetFormats()     Call Console.WriteLine(strEnum & vbCrLf)   Next このようにすれば、取得可能なフォーマット一覧が取得できます。ドラッグするものによって結構内容が違いました。 他のアプリがどんな挙動をしているかと思い、いろんなものにドラッグしてみました。 リンクなしの画像であれば、ペイントやPhotoshopなどにドラッグ可能ですが、やはりファイルから開いていますね。 リンク付の画像はペイント、Photoshopでは無反応、Workではリンク先のURLしか出てきませんでした。 参考になれば。

talosoft
質問者

お礼

ありがとうございます。 ブラウザからのドラッグ&ドロップがこんなに大変だということを知ってびっくりしました。

関連するQ&A

  • D&Dでファイルパスを取得

    フォームにD&Dでファイルパスを取得する プログラムを作ろうと思っているのですが、うまくいきません。 ファイルをフォームにドラッグしても禁止マークがでて イベントハンドラがイベントをキャッチしてくれないようです。 なにが問題かアドバイスを頂けないでしょうか? よろしくお願いします。 Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.AllowDrop = True End Sub Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop Dim FileName As String FileName = e.Data.GetData(DataFormats.FileDrop)(0) MsgBox(FileName) End Sub End Class ---- 開発環境:VS2005 pro OS:WindowsVista

  • TextBoxへ文字列をD&Dをする方法を教えてください。

    Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter If (e.Data.GetDataPresent(DataFormats.Text)) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop TextBox1.Text = e.Data.GetData(DataFormats.Text)(0) End Sub VB.NETで上記のような方法でドラッグされたテキストを テキストボックスに表示するようにしたいのですが、 この方法だと最初の一文字しかドロップされません。 どこか修正箇所などありましたら、ご教示いただけると助かります。

  • ドラッグドロップ(AllowDrop)について

    Form1にドラッグドロップ機能を導入しようと思い AllowDropプロパティをTrueにしてDragDropイベントとDragEnterイベントを記述しました。 実際にデバックしてみるとうまく機能して安心したのですが 後で、bin/debug/AAA.exeを直接クリックしてみるとなぜか機能しません。 MyBase.LoadイベントにMyBase.AllowDrop=Trueと記述してみたいのですがダメでした。 何か設定が間違ってるのでしょうか? ぜひご回答お願いします。 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   MyBase.AllowDrop = True End Sub Private Sub drag_drop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop   Dim list As String() = CType(e.Data.GetData(DataFormats.FileDrop, False), String())   For Each a In list      MsgBox(a)   Next End Sub Private Sub drag_enter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter   e.Effect = DragDropEffects.Copy End Sub

  • VB2010平面移動

    VisualBasic2010においてMouseDown、DragDropを用いてPanelを移動させるコードを書きました。 Panel4へPanel1,Panel2,Panel3を移動させるというものです。  例) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown Panel4.AllowDrop = True End Sub Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown Panel1.DoDragDrop(Panel1, DragDropEffects.Move) End Sub Private Sub Panel2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseDown Panel2.DoDragDrop(Panel2, DragDropEffects.Move) End Sub Private Sub Panel3_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel3.MouseDown Panel3.DoDragDrop(Panel3, DragDropEffects.Move) End Sub Private Sub Panel4_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragDrop Dim srsPnl As Panel = e.Data.GetData(GetType(Panel)) Dim dstPnl As New Panel dstPnl.Size = srsPnl.Size dstPnl.Location = Panel4.PointToClient(CursorPosition) 'New Point(e.X, e.Y) dstPnl.BackColor = srsPnl.BackColor AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove Panel4.Controls.Add(dstPnl) End Sub Private Sub Panel4_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragEnter If e.Data.GetDataPresent(GetType(Panel)) Then e.Effect = DragDropEffects.Move End If End Sub Private previousPos As Point Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown previousPos = CursorPosition() End Sub Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = Windows.Forms.MouseButtons.Left Then Dim nowPos As Point = CursorPosition() DirectCast(sender, Panel).Left += nowPos.X - previousPos.X DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y Console.WriteLine(nowPos.X & "-" & previousPos.X) previousPos = nowPos End If End Sub Function CursorPosition() As Point Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10) End Function このようにした場合、初めに移動させたものが最前にきてしまいます。(画像参照) 私はPanel1(黒)を最前に持っていきたいと考えています。 もしお時間等ありましたら、お力添えをいただけると嬉しく思います。 どうかよろしくお願いします。

  • VB2010 bitmapとして取り込む

    現在、panelを移動させて図形を作成するプログラムを書いています。 このpanelを移動させて作成した図形を画像?(bitmap等)として取り込み、他のフォームで読み込ませたいと考えております。 panelを移動させるコード Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown Panel4.AllowDrop = True End Sub Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown, Panel2.MouseDown, Panel3.MouseDown sender.DoDragDrop(sender, DragDropEffects.Move) End Sub Private Sub Panel4_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragDrop Dim srsPnl As Panel = e.Data.GetData(GetType(Panel)) Dim dstPnl As New Panel dstPnl.Size = srsPnl.Size dstPnl.Location = Panel4.PointToClient(CursorPosition) 'New Point(e.X, e.Y) dstPnl.BackColor = srsPnl.BackColor AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove Panel4.Controls.Add(dstPnl) End Sub Private Sub Panel4_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragEnter If e.Data.GetDataPresent(GetType(Panel)) Then e.Effect = DragDropEffects.Move End If End Sub Private previousPos As Point Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown previousPos = CursorPosition() End Sub Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = Windows.Forms.MouseButtons.Left Then Dim nowPos As Point = CursorPosition() DirectCast(sender, Panel).Left += nowPos.X - previousPos.X DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y Console.WriteLine(nowPos.X & "-" & previousPos.X) previousPos = nowPos End If End Sub Function CursorPosition() As Point Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10) End Function 上記のコードでpanel4へpanel1,panel2,panel3を自由に移動させて任意の図形を作成することができます。 このコードを用いて画像のような図形を作った際に、その図形を他のフォームで読み込ませたいです。 わかる方がいらっしゃいましたら、お力添えしていただけると幸いです。 よろしくお願いします。

  • VB2010において同様のイベントを設定する。

    現在、VisualBasic2010を用いてPanel1~Panel4をPanel5へ移動させて図形を作成させるコードを書いております。 今後Panel1~Panel4をPanel5だけでなく、Panel6,Panel7にも同様に移動させたいと考えておりますが、やりかたがわかりません。 画像はPanel5へ移動させたものです。同様に下に配置したPanel6へも移動させたいです。 わかる方がおられましたら、お力添えいただけると助かります。 Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Panel5.AllowDrop = True End Sub Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown, Panel2.MouseDown, Panel3.MouseDown, Panel4.MouseDown sender.DoDragDrop(sender, DragDropEffects.Move) End Sub Private Sub Panel5_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel5.DragDrop Dim srsPnl As Panel = e.Data.GetData(GetType(Panel)) Dim dstPnl As New Panel dstPnl.Size = srsPnl.Size dstPnl.Location = Panel5.PointToClient(CursorPosition) 'New Point(e.X, e.Y) dstPnl.BackColor = srsPnl.BackColor AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove Panel5.Controls.Add(dstPnl) End Sub Private Sub Panel5_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel5.DragEnter If e.Data.GetDataPresent(GetType(Panel)) Then e.Effect = DragDropEffects.Move End If End Sub Private previousPos As Point Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown previousPos = CursorPosition() End Sub Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = Windows.Forms.MouseButtons.Left Then Dim nowPos As Point = CursorPosition() DirectCast(sender, Panel).Left += nowPos.X - previousPos.X DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y Console.WriteLine(nowPos.X & "-" & previousPos.X) previousPos = nowPos End If End Sub Function CursorPosition() As Point Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10) End Function End Class これが現在書いているコードです。

  • マウスで画像の移動(VB2010)

    FormにPanelを配置してそのなかにPicturBoxを配置しています。 エクスプローラから画像をドラッグアンドドロップして、マウスで画像を移動させようと考えています。 (Panelのサイズを250,250にして、1024*768ピクセルの画像の一部を窓から見ているような感じ) 下記のコードを書いたのですが、マウスを左クリックした状態のままマウスを移動させると、画像がちらつきます。 PictureBox1.Refresh()を入れて多少現象しましたが、根本的な問題の解決には至っておりません。 どなたか?詳しい方いらっしゃいましたら教えて頂けないでしょうか?宜しくお願いいたいます。 Private drawFlag As Boolean Private ptStart As Point Private ptEnd As Point Private Sub PictureBox1_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles PictureBox1.DragEnter If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub PictureBox1_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles PictureBox1.DragDrop Dim strFileName As String() = CType(e.Data.GetData(DataFormats.FileDrop), String()) Dim fi As New System.IO.FileInfo(strFileName(0)) Dim MyBmp As Bitmap = System.Drawing.Image.FromFile(strFileName(0)) PictureBox1.Image = MyBmp End Sub Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize PictureBox1.AllowDrop = True drawFlag = False End Sub Private Sub PictureBox1_MouseMove(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles PictureBox1.MouseMove If drawFlag = False Then Exit Sub End If ptEnd = e.Location Dim ptMove As Point ptMove = ptEnd - ptStart Dim MyLocation As Point MyLocation = PictureBox1.Location + ptMove PictureBox1.Location = MyLocation PictureBox1.Refresh() ptStart = ptEnd End Sub Private Sub PictureBox1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles PictureBox1.MouseDown If e.Button = Windows.Forms.MouseButtons.Left Then drawFlag = True ptStart.X = e.X ptStart.Y = e.Y End If End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles PictureBox1.MouseUp drawFlag = False End Sub

  • ドラッグ&ドロップの対象をフォルダに限定したい

    こんばんは。 テキストボックスにドラッグ&ドロップされたフォルダのパスを取得させたいのですがドラッグの対象をフォルダに限定させたい、もしくはファイルがドラッグ&ドロップされたらそのファイルのカレントディレクトリまでのパスを取得させたいのですが、下記のコードでは取得までは出来るのですがファイルがきた場合にファイルまでのパスが取得されてしまいまいます。 よろしくお願いします。 Dim ddpath As String Private Sub TxtPath_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TxtPath.DragEnter If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub TxtPath_DragDrop(ByVal sender As Object, ByVal e As _System.Windows.Forms.DragEventArgs) Handles TxtPath.DragDrop ddpath = e.Data.GetData(DataFormats.FileDrop)(0) If Dir(ddpath, FileAttribute.Directory) <> "" Then TxtPath.Text = ddpath End If End Sub

  • テキストボックスの改行について質問です。

    VB超初心者です。 現在VBで複数のCSVファイルを処理するアプリケーションを作成しています。 ドラッグドロップでファイルを認識して処理するのが目的です。 そこでまずテキストボックスにドラッグしたファイル名を表示したいと考えており、 ネットで公開されている以下のソースを利用したいと思っています。 Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop TextBox1.Text = e.Data.GetData(DataFormats.FileDrop)(0) End Sub しかし、これでは複数のファイルの表示ができませんでした。 テキストボックス内で改行させるにはどこを変更したら良いのか全く分かりません。 また、ファイル名ではなくファイルの階層を表示するのも目的と少し異なってしまいます。 調べてみても良くわかりませんでした。 そこでVBに精通している方にお願いです。 希望通りに表示する方法のアドバイスをお願いします!!

  • VisualBasic2010,Undoについて

    VisualBasic2010においてTextBoxであれば、ボタンのクリックイベント等でTextBox1.Undo()で操作を戻すことが可能ですが Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown Panel1.DoDragDrop(Panel1, DragDropEffects.Move) End Sub Private Sub Panel2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel2.DragDrop Dim srsPnl As Panel = e.Data.GetData(GetType(Panel)) Dim dstPnl As New Panel dstPnl.Size = srsPnl.Size dstPnl.Location = Panel2.PointToClient(CursorPosition) 'New Point(e.X, e.Y) dstPnl.BackColor = srsPnl.BackColor AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove Panel2.Controls.Add(dstPnl) End Sub Private Sub Panel2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel2.DragEnter If e.Data.GetDataPresent(GetType(Panel)) Then e.Effect = DragDropEffects.Move End If End Sub Private previousPos As Point Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown previousPos = CursorPosition() End Sub Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = Windows.Forms.MouseButtons.Left Then Dim nowPos As Point = CursorPosition() DirectCast(sender, Panel).Left += nowPos.X - previousPos.X DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y Console.WriteLine(nowPos.X & "-" & previousPos.X) previousPos = nowPos End If End Sub Function CursorPosition() As Point Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10) End Function によってPanel1をPanel2へ移動させるというコードを書いております。 Panel2へ移動させたPanel1を移動させる前の状態に戻したいです。 http://ameblo.jp/kazukiokumura/theme4-10011664374.html#main TextBoxでのUndoのやり方は目にすることがあるのですが、PanelでのUndoは可能なのでしょうか? お時間等ありましたらコードを作っていただけると助かります。 よろしくお願いいたします。

専門家に質問してみよう