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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
is that C++ or C# that you need? Winforms or WPF?
0
threadyAuthor Commented:
C#   :-)
Thanks jkr! ! !
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.