How to capture a folder address with drag and drop ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: How to capture a folder address with drag and drop ?

  1. #1
    Join Date
    May 2002
    Posts
    1,723

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,352

    Re: How to capture a folder address with drag and drop ?

    Mike, maybe you explain the problem a bit more clear?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,957

    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

  4. #4
    Join Date
    May 2002
    Posts
    1,723

    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
    Attached Files Attached Files
    Last edited by Mike Pliam; April 24th, 2013 at 08:06 PM.
    mpliam

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,957

    Re: How to capture a folder address with drag and drop ?

    Quote Originally Posted by Mike Pliam View Post
    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

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,216

    Re: How to capture a folder address with drag and drop ?

    Quote Originally Posted by Igor Vartanov View Post
    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

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: How to capture a folder address with drag and drop ?

    Quote Originally Posted by Arjay View Post
    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.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,216

    Re: How to capture a folder address with drag and drop ?

    Quote Originally Posted by Eri523 View Post
    ... 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center