-
April 24th, 2013, 12:00 AM
#1
How to capture a folder address with drag and drop ?
I am certain that this is possible, but cannot figure out how to do it.
Any ideas greatly appreciated.
mpliam
-
April 24th, 2013, 01:11 AM
#2
Re: How to capture a folder address with drag and drop ?
Mike, maybe you explain the problem a bit more clear?
Victor Nijegorodov
-
April 24th, 2013, 02:19 AM
#3
Re: How to capture a folder address with drag and drop ?
Last edited by Igor Vartanov; April 24th, 2013 at 02:21 AM.
Best regards,
Igor
-
April 24th, 2013, 11:55 AM
#4
Re: How to capture a folder address with drag and drop ?
While those references are generally informative, they don't really tell you how to get a folder address when you drag a folder onto a window. In fact, there so wordy and convoluted and lacking in example that they are IMHO misleading. It is really not all that difficult to set up a drag and drop window. I have used the following code to drag/drop files onto a static window. Note that here I used a derived CStatic class to implement dragging onto a CStatic window, but the principle for any window should remain largely the same.
Code:
void CStaticDragDrop::OnDropFiles(HDROP hDropInfo)
{
TRACE0("*********** OnDropFiles ************************\n");
UINT i = 0;
vector<CString> m_vcsPathnames.resize(0);
// query to collect the dragged in file path names
UINT nFiles = ::DragQueryFile(hDropInfo, (UINT) -1, NULL, 0);
for (i = 0; i < nFiles; i++)
{
TCHAR szFileName[_MAX_PATH];
::DragQueryFile(hDropInfo, i, szFileName, _MAX_PATH);
m_vcsPathnames.push_back(szFileName);
}
::DragFinish(hDropInfo);
// dump the pathnames
for(size_t i = 0; i < m_vcsPathnames.size(); i++) { TRACE1("m_vcsPathnames[%d] =: ", i); OutputDebugString(m_vcsPathnames[i]); TRACE0("\n"); }
This works fine with both files and with folders but not with a CRichEditView derived window ( ASSERT(nFileCount != 0); ), even though the correct folder name shows up as a single element of the m_vcsPathnames vector.
So my question boils down to how does one set this code up so that it can discriminate between a file (with a suffix) and a folder (no suffix)? What is the most clever and efficient way to programatically tell 'C:/.../mywhatever' from 'C/.../mywhatever/my.txt' ?
For any interested, here's a small demo to illustrate the point. To use the class in your app:
- create a dialog based app
- add CStaticDragDrop.h and CStaticDragDrop.cpp to your project
- add a CStatic derived control to your dialog interface
- rename the control id IDC_STATIC_TARGET (or some such but must match that in CStaticDragDrop.cpp)
- add a variable to the control and relabel it CStaticDragDrop
- set Notify and Accept Files true (default is false)
- compile and run
- files will have suffixes, folders will not
- check for the presence of '.' using int n = CString::Find(_T("."), 0); if (n >= 0) it's a file, else it's a folder
Last edited by Mike Pliam; April 24th, 2013 at 08:06 PM.
mpliam
-
April 25th, 2013, 01:33 AM
#5
Re: How to capture a folder address with drag and drop ?
Originally Posted by Mike Pliam
So my question boils down to how does one set this code up so that it can discriminate between a file (with a suffix) and a folder (no suffix)? What is the most clever and efficient way to programatically tell 'C:/.../mywhatever' from 'C/.../mywhatever/my.txt' ?
So your real question essentially has nothing to do with drag-n-drop.
The most reliable way to tell files from folders is to query the file system object attributes and see if FILE_ATTRIBUTE_DIRECTORY is set.
GetFileAttributes function
Code:
BOOL isFolder(LPCTSTR path)
{
DWORD attr = GetFileAttributes(path);
if (DWORD(-1) == attr) // path not exist
return FALSE;
return FILE_ATTRIBUTE_DIRECTORY & attr;
}
Best regards,
Igor
-
April 25th, 2013, 01:32 PM
#6
Re: How to capture a folder address with drag and drop ?
Originally Posted by Igor Vartanov
So your real question essentially has nothing to do with drag-n-drop.
The most reliable way to tell files from folders is to query the file system object attributes and see if FILE_ATTRIBUTE_DIRECTORY is set.
GetFileAttributes function
Code:
BOOL isFolder(LPCTSTR path)
{
DWORD attr = GetFileAttributes(path);
if (DWORD(-1) == attr) // path not exist
return FALSE;
return FILE_ATTRIBUTE_DIRECTORY & attr;
}
Yep, this is the way to go.
Mike, this code is flawed because not all files have extensions.
Code:
- check for the presence of '.' using int n = CString::Find(_T("."), 0); if (n >= 0) it's a file, else it's a folder
-
April 25th, 2013, 02:43 PM
#7
Re: How to capture a folder address with drag and drop ?
Originally Posted by Arjay
Yep, this is the way to go.
Mike, this code is flawed because not all files have extensions.
[...]
... and some folders do have extensions. (Just for completeness. )
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
April 25th, 2013, 03:18 PM
#8
Re: How to capture a folder address with drag and drop ?
Originally Posted by Eri523
... and some folders do have extensions. (Just for completeness. )
Right you are, sir.
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
|