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 ?
Printable View
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 ?
Bump !!
Just want to raise the above question again.
Split into its own thread and moved to VB.NET
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.
Thanks m8 ill give that a go and let you know how i get on.
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?
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
What im looking to do is , if i drop the firefox shortcut icon onto a picturebox , it displays the firefox shortcut icon.
ok.. so ignore the code in last post. Have a look at the link that describes how to get a file's icon.
Ok m8 thanks alot , ill check that out now.
Can make head nor tail of that , but thanks anyway m8.
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 :)
Ok ill try that , thanks
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
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.
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
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
Wow , hey , that works great +1 to you , thanks alot Pradeep1210
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
Also how would i drag n drop different images to to different pictureboxes ?
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.
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.
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
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
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.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
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 ?
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.
How would i use "my.computer.settings" ?
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
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
Yes ive added the to my settings but can get them to save.
Name = pic1 | Type = String | Scope = User
Am i even saving the correct thing here. (PictureBox2.Tag) or should i be saving something else ?
What happens if you try to see it in debug window?
Code:Debug.Print (My.Settings.pic1)
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"
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.
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.
This is what im using in the form load event
Code:PictureBox1.Tag = My.Settings.pic1
PictureBox2.Tag = My.Settings.pic2
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.
Ok m8 ill do that , thanks for that.