Get filename of dragged icon on Windows

Hi Experts,

Is there a way to subscribe to file drag events in Windows, in order to obtain the full path to the file that was dragged?   i.e., before the file is dropped?

Thanks,
Mike
LVL 1
threadyAsked:
Who is Participating?
 
jkrCommented:
It ist, see http://www.codeproject.com/Articles/485/The-Complete-Idiots-Guide-to-Writing-Shell-Exten#dragenter ("The Complete Idiot's Guide to Writing Shell Extensions - Part VI - Section 'DragEnter()'") - the snippet there has all the code you need:

STDMETHODIMP CSendToShlExt::DragEnter (
    IDataObject* pDataObj,
    DWORD        grfKeyState,
    POINTL       pt,
    DWORD*       pdwEffect )
{
  AFX_MANAGE_STATE(AfxGetStaticModuleState());  // init MFC
 
COleDataObject dataobj;
TCHAR          szItem[MAX_PATH];
UINT           uNumFiles;
HGLOBAL        hglobal;
HDROP          hdrop;
 
  dataobj.Attach ( pDataObj, FALSE );
 
  // Read the list of items from the data object.  They're stored in HDROP
  // form, so just get the HDROP handle and then use the drag 'n' drop APIs
  // on it.
  hglobal = dataobj.GetGlobalData ( CF_HDROP );
 
  if ( NULL != hglobal )
    {
    hdrop = (HDROP) GlobalLock ( hglobal );
 
    uNumFiles = DragQueryFile ( hdrop, 0xFFFFFFFF, NULL, 0 );
 
    for ( UINT uFile = 0; uFile < uNumFiles; uFile++ )
      {
      if ( 0 != DragQueryFile ( hdrop, uFile, szItem, MAX_PATH ))
        m_lsDroppedFiles.AddTail ( szItem );
      }
 
    GlobalUnlock ( hglobal );
    }

Open in new window


Everything else is covered in this article and the rest of the series.

Since you tagged this Q 'C#' as well, also check out the .NET version at http://www.codeproject.com/Articles/529515/NET-Shell-Extensions-Shell-Drop-Handlers (".NET Shell Extensions - Shell Drop Handlers")
0
 
Éric MoreauSenior .Net ConsultantCommented:
is that C++ or C# that you need? Winforms or WPF?
0
 
threadyAuthor Commented:
C#   :-)
Thanks jkr! ! !
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
threadyAuthor Commented:
I'll try this tomorrow morning!
0
 
Éric MoreauSenior .Net ConsultantCommented:
Winforms or WPF?
0
 
threadyAuthor Commented:
Wpf...
0
 
Éric MoreauSenior .Net ConsultantCommented:
No need to use Shell. Here is a WPF sample:

public MainWindow()
{
	InitializeComponent();
	TextBox1.AllowDrop = true;
	TextBox1.PreviewDragEnter += TextBox1_DragEnter;
}

private void TextBox1_DragEnter(object sender, DragEventArgs e)
{
	Label1.Content = string.Empty;

	if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
	{
		string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
		foreach (string filename in filenames)
		{
			Label1.Content += filename + "  ";
		}
	}
	e.Effects = DragDropEffects.All;
	e.Handled = true;
}

Open in new window

0
 
threadyAuthor Commented:
I don't want to detect drops, I want to detect the start of drag only.
0
 
Éric MoreauSenior .Net ConsultantCommented:
My code lets you preview before you drop. The drop is not handled. What are your trying to do exactly?
0
 
threadyAuthor Commented:
Ok, I read it too quick.  Thanks a lot for your help.  I'd love to be able to tell you but I should not because of work...  Sorry.  I'll try tomorrow morning.  Thanks again!
0
 
threadyAuthor Commented:
I don't think your method will work Eric, I don't have a textbox to preview..  I really only want Windows messages with no ui involved from my app...
0
 
threadyAuthor Commented:
jkr, I don't think I can get the shell method to work either.  My program is minimized and the drop target is not known and the drop target is irrelevant (it's another unknown application)....
0
 
Éric MoreauSenior .Net ConsultantCommented:
What are your trying to do exactly?
0
 
threadyAuthor Commented:
lol... You're persistent Eric!  I suppose I can tell you I'm trying to get the filename of a file that is dropped into a browser.
0
 
Éric MoreauSenior .Net ConsultantCommented:
I am trying to understand your process (and I am sure others are trying to). By better understanding the process, we might be able to propose better solution!

So you have an application and you want to detect what is being drag-and-drop to a browser but this browser is not in your application. Right?
0
 
threadyAuthor Commented:
Correct
0
 
Éric MoreauSenior .Net ConsultantCommented:
Then I am clueless!
0
 
threadyAuthor Commented:
Thanks for trying  :)
Unfortunately I don't think it's possible.
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
threadyAuthor Commented:
That's not ok for commercial software.  We don't know the source window, so we'd have to invade the system and do a little too much spying.  If we owned the source then that would be ok...
0
 
threadyAuthor Commented:
Then again, if we owned the source, we wouldn't even have to do that...
0
 
LeeTutorretiredCommented:
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
0
 
jkrCommented:
Evering is there that is needed and since LeeTutor only knows Delete Requests, objecting is mandantory.
0
 
threadyAuthor Commented:
Thanks for your answers!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.