如何获取文件的路径拖入Win32应用程序并删除它?

H4c*_*0rD 0 c c++ winapi drag-and-drop

我有一个程序,当他们将文件放入其中时,我希望它让路径显示"路径"的消息框然后将其删除.任何人都可以阐明如何做到这一点?

NTD*_*DLS 9

首先,您需要一个可以接受删除文件的窗口.这是通过将窗口的ExStyle设置为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.
);
Run Code Online (Sandbox Code Playgroud)

其次,您需要在WinProc()回调中处理WM_DROPFILES消息.

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);
}
Run Code Online (Sandbox Code Playgroud)

第三,你需要一个能够从你的对话框中删除的任何目录中删除所有子目录和文件的函数:

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; 
} 
Run Code Online (Sandbox Code Playgroud)

最后,你需要非常小心这个片段 - 它毕竟删除了文件.