|
-
Feb 19th, 2010, 07:38 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Drag And Drop Shortcut
Is it possible to drag an drop a desktop shourtcut to the form and then launch the program from there ? Is so how would this be done ?
-
Mar 3rd, 2010, 05:24 AM
#2
Thread Starter
Fanatic Member
Re: Drag Drop files from windows into VB.NET
Bump !!
Just want to raise the above question again.
-
Mar 3rd, 2010, 06:41 AM
#3
Re: Drag And Drop Shortcut
Split into its own thread and moved to VB.NET
-
Mar 3rd, 2010, 08:01 AM
#4
Re: Drag And Drop Shortcut
Check this:
vb.net Code:
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '' first allow the form to accept drop. Me.AllowDrop = True End Sub Private Sub Form3_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter '' when a file is dragged into our form, we change the mouse icon to signify that it can be dropped there. If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub Form3_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop '' when the file is dropped on the form, we get the filename and open it. Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0) Process.Start(file) End Sub
I used a form for simplicity. But you can use any control on your form instead of the form surface itself as drop target.
I also took only the first file to open for demo. If the user is dropping more than one file in a group, you might want to iterate through all the files and open all of them.
-
Mar 3rd, 2010, 08:02 AM
#5
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Thanks m8 ill give that a go and let you know how i get on.
-
Mar 3rd, 2010, 08:05 AM
#6
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
That works great , how would i display the shortcut image in a picturebox , when i drag and drop it to a picturebox on the form?
-
Mar 3rd, 2010, 08:17 AM
#7
Re: Drag And Drop Shortcut
That's a different question all together. Have a look here to see how to get the associated icon with any file type:
http://www.codeguru.com/vb/gen/vb_mi...icle.php/c5597
If you meant how to open an image file into your picture box then it's easy.
Just modify the code in my last post as follows:
vb.net Code:
Private Sub Form3_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0) 'Process.Start(file) Me.PictureBox1.Image = Image.FromFile(file) End Sub
-
Mar 3rd, 2010, 08:31 AM
#8
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
What im looking to do is , if i drop the firefox shortcut icon onto a picturebox , it displays the firefox shortcut icon.
-
Mar 3rd, 2010, 08:34 AM
#9
Re: Drag And Drop Shortcut
ok.. so ignore the code in last post. Have a look at the link that describes how to get a file's icon.
-
Mar 3rd, 2010, 08:35 AM
#10
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Ok m8 thanks alot , ill check that out now.
-
Mar 3rd, 2010, 08:41 AM
#11
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Can make head nor tail of that , but thanks anyway m8.
-
Mar 3rd, 2010, 08:47 AM
#12
Re: Drag And Drop Shortcut
Actually a file has no information about the icon that we see for that file. It is determined by file extension list that windows has.
The code in that link uses the SHGetFileInfo API to get that icon. You may google around for "SHGetFileInfo" and might hit upon an easier example from some other source. Or this forum might already have some threads dedicated to this topic; just search and see
-
Mar 3rd, 2010, 08:51 AM
#13
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
-
Mar 3rd, 2010, 09:11 AM
#14
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Can u take a look at this snippet of code m8 , can u work it out ? Ive been trying to use it but cant seem to get it working.
http://www.vbforfree.com/?p=396
-
Mar 3rd, 2010, 09:38 AM
#15
Re: Drag And Drop Shortcut
No.. that's of no help because you don't know the icon file path.
The link I had posted earlier has exactly what you need. And I think the original author has fully described step by step how to get it working. Of course instead of ListView/ImageList, you would have to redirect it towards your picturebox.
-
Mar 3rd, 2010, 09:58 AM
#16
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Yea that works , but its not exactly what im looking for.
This code add the desktop icon (small icon) to a listview and also the file path which i dont want.
Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private nIndex = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hImgSmall As IntPtr 'The handle to the system image list.
Dim hImgLarge As IntPtr 'The handle to the system image list.
Dim fName As String 'The file name to get the icon from.
Dim shinfo As SHFILEINFO
shinfo = New SHFILEINFO()
Dim openFileDialog1 As OpenFileDialog
openFileDialog1 = New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\temp\"
openFileDialog1.Filter = "All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
ListView1.SmallImageList = ImageList1
ListView1.LargeImageList = ImageList1
shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
fName = openFileDialog1.FileName
'Use this to get the small icon.
hImgSmall = SHGetFileInfo(fName, 0, shinfo, _
Marshal.SizeOf(shinfo), _
SHGFI_ICON Or SHGFI_SMALLICON)
'Use this to get the large icon.
''hImgLarge = SHGetFileInfo(fName, 0,
''ref shinfo, (uint)Marshal.SizeOf(shinfo),
''SHGFI_ICON | SHGFI_LARGEICON);
'The icon is returned in the hIcon member of the
'shinfo struct.
Dim myIcon As System.Drawing.Icon
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
ImageList1.Images.Add(myIcon) 'Add icon to
'imageList.
ListView1.Items.Add(fName, nIndex) 'Add file name and
'icon to listview.
nIndex = nIndex + 1
End If
End Sub
End Class
-
Mar 3rd, 2010, 10:30 AM
#17
Re: Drag And Drop Shortcut
ok. Try this:
vb.net Code:
Imports System.Runtime.InteropServices Public Class Form3 Private Structure SHFILEINFO Public hIcon As IntPtr ' : icon Public iIcon As Integer ' : icondex Public dwAttributes As Integer ' : SFGAO_ flags <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _ Public szDisplayName As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _ Public szTypeName As String End Structure Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As IntPtr Private Const SHGFI_ICON = &H100 Private Const SHGFI_SMALLICON = &H1 Private Const SHGFI_LARGEICON = &H0 ' Large icon Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '' first allow the form to accept drop. Me.AllowDrop = True End Sub Private Sub Form3_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter '' when a file is dragged into our form, we change the mouse icon to signify that it can be dropped there. If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub Form3_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0) Dim hImgLarge As IntPtr 'The handle to the system image list. Dim shinfo As SHFILEINFO shinfo = New SHFILEINFO() Dim openFileDialog1 As OpenFileDialog openFileDialog1 = New OpenFileDialog() shinfo.szDisplayName = New String(Chr(0), 260) shinfo.szTypeName = New String(Chr(0), 80) 'Use this to get the small icon. 'hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON) 'Use this to get the large icon. hImgLarge = SHGetFileInfo(file, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON) 'The icon is returned in the hIcon member of the shinfo struct. Dim myIcon As System.Drawing.Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon) PictureBox1.Image = myIcon.ToBitmap End Sub End Class
-
Mar 3rd, 2010, 10:38 AM
#18
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Wow , hey , that works great +1 to you , thanks alot Pradeep1210
-
Mar 3rd, 2010, 10:41 AM
#19
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
1 more thing , what i drag and drop the icon , how do i double click the picturebox to open the file , i tried this but e.data has a blue line under it.
Code:
Private Sub PictureBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
Process.Start(file)
End Sub
-
Mar 3rd, 2010, 10:52 AM
#20
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Also how would i drag n drop different images to to different pictureboxes ?
-
Mar 3rd, 2010, 01:57 PM
#21
Re: Drag And Drop Shortcut
 Originally Posted by dunlop03
1 more thing , what i drag and drop the icon , how do i double click the picturebox to open the file , i tried this but e.data has a blue line under it.
Code:
Private Sub PictureBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
Process.Start(file)
End Sub
No.. It won't work this way.
In the Form DragDrop event, you have the name of the file. Store it in some form level variable, so that it is accessible here. And then use that variable here in the PictureBox DoubleClick event.
-
Mar 3rd, 2010, 02:02 PM
#22
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
How do i do that lol ? and how can i place different images in different pictureboxes , theres 10 pictureboxes and i nee to be able to drag and drop a different icon to each picturebox.
-
Mar 3rd, 2010, 02:03 PM
#23
Re: Drag And Drop Shortcut
 Originally Posted by dunlop03
Also how would i drag n drop different images to to different pictureboxes ?
Just as I said in my post #4, the example shown was for the form for simplicity. You can use any control instead of the form.
Replace all instances of Me / Form3 with PictureBox1 (or whatever is the name of your PictureBox) and that should work the way you want it to.
-
Mar 3rd, 2010, 02:08 PM
#24
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Ok , got u on the #4 Post , but as for replace all instances of Me / Form3 with PictureBox1 , im not sure what u mean , sry. Heres what im using so far
Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As IntPtr
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' first allow the form to accept drop.
Me.AllowDrop = True
End Sub
Private Sub Form1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
'' when a file is dragged into our form, we change the mouse icon to signify that it can be dropped there.
If (e.Data.GetDataPresent(DataFormats.FileDrop)) 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
Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
Dim hImgLarge As IntPtr 'The handle to the system image list.
Dim shinfo As SHFILEINFO
shinfo = New SHFILEINFO()
Dim openFileDialog1 As OpenFileDialog
openFileDialog1 = New OpenFileDialog()
shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
'Use this to get the small icon.
'hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)#
'Use this to get the large icon.
hImgLarge = SHGetFileInfo(file, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
'The icon is returned in the hIcon member of the shinfo struct.
Dim myIcon As System.Drawing.Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
PictureBox1.Image = myIcon.ToBitmap
End Sub
End Class
-
Mar 3rd, 2010, 02:14 PM
#25
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Ive tried this for to allow drops in all the picture boxes but its not working ?
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' first allow the form to accept drop.
Me.AllowDrop = True
PictureBox1.AllowDrop = True
PictureBox2.AllowDrop = True
PictureBox3.AllowDrop = True
PictureBox4.AllowDrop = True
PictureBox5.AllowDrop = True
PictureBox6.AllowDrop = True
PictureBox7.AllowDrop = True
End Sub
-
Mar 3rd, 2010, 02:18 PM
#26
Re: Drag And Drop Shortcut
vb.net Code:
Imports System.Runtime.InteropServices Public Class Form1 Private Structure SHFILEINFO Public hIcon As IntPtr ' : icon Public iIcon As Integer ' : icondex Public dwAttributes As Integer ' : SFGAO_ flags <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _ Public szDisplayName As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _ Public szTypeName As String End Structure Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As IntPtr Private Const SHGFI_ICON = &H100 Private Const SHGFI_SMALLICON = &H1 Private Const SHGFI_LARGEICON = &H0 ' Large icon Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '' first allow the picturebox to accept drop. PictureBox1.AllowDrop = True End Sub Private Sub PictureBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragEnter '' when a file is dragged into our picturebox, we change the mouse icon to signify that it can be dropped there. If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub PictureBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0) Dim hImgLarge As IntPtr 'The handle to the system image list. Dim shinfo As SHFILEINFO shinfo = New SHFILEINFO() Dim openFileDialog1 As OpenFileDialog openFileDialog1 = New OpenFileDialog() shinfo.szDisplayName = New String(Chr(0), 260) shinfo.szTypeName = New String(Chr(0), 80) 'Use this to get the small icon. 'hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON) 'Use this to get the large icon. hImgLarge = SHGetFileInfo(file, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON) 'The icon is returned in the hIcon member of the shinfo struct. Dim myIcon As System.Drawing.Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon) PictureBox1.Image = myIcon.ToBitmap PictureBox1.Tag = file '' we save the filename in the PictureBox Tag property, to be used later when doubleclicking. End Sub Private Sub PictureBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick If Not String.IsNullOrEmpty(PictureBox1.Tag) Then Process.Start(PictureBox1.Tag) End If End Sub End Class
You would need to repeat these for all pictureboxes. Since we want the same operation for all pictureboxes, you may want to to generalize it by adding common event handlers or functions etc.
-
Mar 3rd, 2010, 02:28 PM
#27
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Amazing , thats working great , now ive another problem , how do i save it so that when i close my form , it saves all the drag and drops ?
-
Mar 3rd, 2010, 02:32 PM
#28
Re: Drag And Drop Shortcut
Since you had put the filenames in PictureBox Tag property, pick them up from there and save it wherever you like - file, registry, settings etc.
-
Mar 3rd, 2010, 02:38 PM
#29
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
How would i use "my.computer.settings" ?
-
Mar 3rd, 2010, 03:03 PM
#30
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Ive tried this , but its no use.
Code:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
My.Settings.pic1 = PictureBox1.Tag
My.Settings.pic2 = PictureBox2.Tag
My.Settings.Save()
End Sub
-
Mar 3rd, 2010, 06:24 PM
#31
Re: Drag And Drop Shortcut
You should have those pic1 and pic2 keys before trying to do that.
Check this out to see how to create those keys:
http://msdn.microsoft.com/en-us/library/25zf0ze8.aspx
-
Mar 4th, 2010, 03:17 AM
#32
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Yes ive added the to my settings but can get them to save.
Name = pic1 | Type = String | Scope = User
-
Mar 4th, 2010, 03:44 AM
#33
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Am i even saving the correct thing here. (PictureBox2.Tag) or should i be saving something else ?
-
Mar 4th, 2010, 03:49 AM
#34
Re: Drag And Drop Shortcut
What happens if you try to see it in debug window?
Code:
Debug.Print (My.Settings.pic1)
-
Mar 4th, 2010, 03:53 AM
#35
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Dam , not sure what you mean , i added that to a button and debuged it and in the immidiate window i got this "C:\Users\Richie\Desktop\Microsoft Visual Basic 2008 Express Edition.lnk"
-
Mar 4th, 2010, 03:57 AM
#36
Re: Drag And Drop Shortcut
Yes.. so that's working. It is saving the file name and path correctly.
Since you are dropping a file shortcut, it is saving the path/filename of the file shortcut.
-
Mar 4th, 2010, 03:59 AM
#37
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
do you mean is it displaying the path/filename in the picturebox when i drop it in ? No its only displaying the icon , but on double clicking it, it opens the program.
-
Mar 4th, 2010, 04:12 AM
#38
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
This is what im using in the form load event
Code:
PictureBox1.Tag = My.Settings.pic1
PictureBox2.Tag = My.Settings.pic2
-
Mar 4th, 2010, 04:15 AM
#39
Re: Drag And Drop Shortcut
 Originally Posted by dunlop03
do you mean is it displaying the path/filename in the picturebox when i drop it in ? No its only displaying the icon , but on double clicking it, it opens the program.
No.. What I mean is it is saving the file name and path in your My.Settings correctly.
You saved it in the PictureBox Tag property, so it is in the Tag property. It won't be printed in the picturebox unless you put some code to do that, and that's a totally different question.
Since your original question of dragging and dropping file into your form/PictureBox is resloved, I would advice you to mark this thread resolved and start a fresh thread, to get more attention on your new requirements.
-
Mar 4th, 2010, 04:16 AM
#40
Thread Starter
Fanatic Member
Re: Drag And Drop Shortcut
Ok m8 ill do that , thanks for that.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|