I'm trying to 'Drop' an image file at the current mouse location, FileType PNG (always).
With 'Drop' i mean the same type of functionality as if you were to click and hold an image, then drop it at another location when the hotkey is pressed.
This way i can paste an image i just 'snipped' and paste it as if it were a file with the appropriate filename. (this is primarily meant for pasting into wordpress, and doesn't have a plugin available to do this)
i've written code to grab an image from the clipboard and save it. But now i want to grab this image and drop it at mousepos when the hotkey is pressed.
The hotkey is working 'Asynchronous' without my app having the focus ( as intended ) but now i'm stuck at actually dropping this file when the hotkey is pressed.
My form (updated 27, January 2020):
Option Explicit
Private Sub Command1_Click()
'Placeholder: a button so i can control when the file is grabbed from
' the clipboard.
'The inputbox is required to enter a filename for this image. This is important
'since we only want to drop a file that has an appropriate filename.
Dim ImageName$
Picture1.Picture = Clipboard.GetData 'Grab image from the clipboard
''Clipboard.Clear
ImageName$ = InputBox("Provide filename") 'Enter filename for the image
FileName$ = ImageName$
Text1.Text = FileName$
End Sub
Sub On_Event_Keypress(FileName$)
'When the F9 key is pressed
Dim FilePath$, MouseX&, MouseY&
FilePath$ = App.Path + "\" 'Save the image to the current App.Path root
SavePicture Picture1.Image, FilePath$ & FileName$ & ".png" 'Save picture to Disk
FileName$ = FilePath$ & FileName$ & ".png"
Call PasteIt(FileName$) 'Load image from Imagelocation and DragAndDrop it where mouse currently is.
End Sub
Public Sub PasteIt(ByVal FileName As String)
'When F9 is pressed, the previous saved file is loaded to the OLE object.
'Then a drag action is started, but because this action will make the mouse
'jump back to the OLE control, i record the mouse position and move it back
'to where the drop action should take place.
'Not sure wheter to use OLE but this seems to reflect an actual drag action while i
'was testing. It's very much possible that this can be done using a filehandle or somesort
'im just not sure where to start to achieve the Drop action like a DragAndDrop sequence.
'Load the saved file to a control
OLE1.SourceDoc = FileName
'Get current mouse position. So the program knows where to 'drop' the dragged picture
Call GetCursorPos(Mpos)
OLE1.Drag vbBeginDrag 'Start the DragAndDrop action
SetCursorPos Mpos.X, Mpos.Y 'Mouse the mousecursor back to where the F9 key was pressed
Stop 'This is where i'm stuck and know im close. The mousecursor is holding an item, but i can't drop
'it like i was dragging it myself
End Sub
Private Sub OLE1_DragDrop(Source As Control, X As Single, Y As Single)
Stop
End Sub
Private Sub Timer1_Timer()
'Checks if the picturebox is loaded with an image
'Then listens to the F9 keypress
If Picture1.Picture Then
If GetAsyncKeyState(vbKeyF9) Then
'Hotkey pressed,do dragdrop at mousepos
Call On_Event_Keypress(Text1.Text)
End If
End If
End Sub
My module(updated 27, January 2020):
Option Explicit
Public Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Type POINTAPI
X As Long
Y As Long
End Type
Public Mpos As POINTAPI
Public FileName$
I've tried to simulate this with the following, but this didnt work out..:
Public ssfDESKTOP As Variant
Public FILE As Variant
Public Sub PasteIt(ByVal FileName As String)
ssfDESKTOP = 0
FILE = FileName
With CreateObject("Shell.Application").NameSpace(ssfDESKTOP)
With .ParseName(FILE)
.InvokeVerb "copy" 'This is a canonical verb and should work for any
'regional and language settings.
End With
End With
End Sub
But the copy
isn't taking place. When i manually click the paste button afterwards, it will paste the file in an explorer, but this is not the drop functionality i am after.
I just changed the title to better reflect the final problem i'm trying to solve.
After coming up with different phrasing for the same problem i again find myself at a dead end, just because i don't understand yet how the OLEdrag
works for the image
object. This object actually has the OLEdragdrop but im not sure what is required to call it's startdrag
and finish it with my mouse by clicking whereever i wanted to drop it.