views:

102

answers:

2

I have a program and when they drop files into it I want it to get the path show a messagebox "of the path" then delete it. Can anyone shed some light on how to do this?

+2  A: 

Here is a good example.

consultutah
Im new to win32 this is kinda for people who are pretty advanced lol +1 for good example
H4cKL0rD
Still the correct answer. Keep in mind that people can drag tons of junk from anywhere to your program. You can't stop prople doing that, but you obviously must ignore it.
MSalters
+2  A: 

First of all, you'll need a window that can accept dropped files. This is accomplished by setting the ExStyle of your window to WS_EX_ACCEPTFILES:

//Create a window.
hWnd = CreateWindowEx
    WS_EX_ACCEPTFILES,      // Extended possibilites for variation.
    gsClassName,            // Classname.
    gsTitle,                // Title caption text.
    WS_OVERLAPPEDWINDOW,    // Default window.
    CW_USEDEFAULT,          // X Position.
    CW_USEDEFAULT,          // Y position.
    230,                    // Window starting width in pixils.
    150,                    // Window starting height in pixils.
    HWND_DESKTOP,           // The window is a child-window to desktop.
    (HMENU)NULL,            // No menu.
    hInstance,              // Program Instance handler.
    NULL                    // No Window Creation data.
);

Second, you'll need to handle the WM_DROPFILES message in your WinProc() callback.

if(uMessage == WM_DROPFILES)
{
    HDROP hDropInfo = (HDROP)wParam;
    char sItem[MAX_PATH];
    for(int i = 0; DragQueryFile(hDropInfo, i, (LPSTR)sItem, sizeof(sItem)); i++)
    {
        //Is the item a file or a directory?
        if(GetFileAttributes(sItem) &FILE_ATTRIBUTE_DIRECTORY)
        {
            //Delete all of the files in a directory before we can remove it.
            DeleteDirectoryRecursive(sItem);
        }
        else {
            SetFileAttributes(sItem, FILE_ATTRIBUTE_NORMAL); //Make file writable
            DeleteFile(sItem);
        }

    }
    DragFinish(hDropInfo);
}

Third, you'll need a function that can remove all of the sub-directories and files from any directories that are dropped onto your dialog:

bool DeleteDirectoryRecursive(const char *sDir)
{ 
    WIN32_FIND_DATA fdFile; 
    HANDLE hFind = NULL; 

    char sPath[2048]; 

    //Specify a file mask. *.* = We want everything! 
    sprintf(sPath, "%s\\*.*", sDir); 

    if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) 
    { 
        printf("Path not found: [%s]\n", sDir); 
        return false; 
    } 

    do 
    { 
        //Find first file will always return "."
        //    and ".." as the first two directories.
        if(strcmp(fdFile.cFileName, ".") != 0
                && strcmp(fdFile.cFileName, "..") != 0) 
        { 
            //Build up our file path using the passed in 
            //  [sDir] and the file/foldername we just found: 
            sprintf(sPath, "%s\\%s", sDir, fdFile.cFileName); 

            //Is the entity a File or Folder? 
            if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) 
            { 
                DeleteDirectoryRecursive(sPath); //Recursive call.
            } 
            else{ 
                printf("File: %s\n", sPath);
                SetFileAttributes(sPath, FILE_ATTRIBUTE_NORMAL);
                DeleteFile(sPath);
            } 
        } 
    } 
    while(FindNextFile(hFind, &fdFile)); //Find the next file. 

    FindClose(hFind); //Always, Always, clean things up! 

    SetFileAttributes(sDir, FILE_ATTRIBUTE_NORMAL);
    RemoveDirectory(sDir); //Delete the directory that was passed in.

    return true; 
} 

Lastly, you'll need to be VERY CAREFUL with this snippet - it deletes files after all.

NTDLS