拖放 Win API 32

3
我正在尝试在我的程序中拖放ListView项目到另一个程序(就像将路径拖放到类似于VLC的东西,它会播放视频文件)。我正在使用CF_HDROP剪贴板格式。CopySelection是设置STGMEDIUM hglobal变量为DROPFILES结构的函数。
void CopySelection(HWND hwndList, STGMEDIUM &stgmed)
{
    HGLOBAL hMem;
    DROPFILES  *ptr;
    DROPFILES dfiles;
    POINT p;

    // get the selection inside the list control
    int iPos = SendMessage(hwndList, LVM_GETNEXTITEM, (WPARAM)-1,(LPARAM)LVNI_SELECTED);
    cout << "iPos: " << iPos << endl;
    LVITEM item;
    char buffer[256];
    string fileDir = "";
    item.iItem = iPos;
    item.iSubItem = 1;
    item.cchTextMax = 256;
    item.pszText = buffer;
    item.mask = LVIF_TEXT;

    ListView_GetItem(hwndList, &item);
    fileDir += string(item.pszText);
    fileDir += "\\";
    item.iItem = iPos;
    item.iSubItem = 0;

    ListView_GetItem(hwndList, &item);
    fileDir += string(item.pszText);
    item.iItem = iPos;
    item.iSubItem = 2;

    ListView_GetItem(hwndList, &item);
    fileDir += string(item.pszText);

    cout << "fileDir: " << fileDir << endl;

    hMem = GlobalAlloc(GHND, sizeof(DROPFILES));
    ptr  = (DROPFILES *)GlobalLock(hMem);

    dfiles.fNC = TRUE;
    dfiles.fWide = FALSE;
    memcpy((void*)&dfiles.pFiles, (fileDir.c_str()+'\0'), fileDir.size()+1);

    GetCursorPos(&p);
    dfiles.pt=p;

    // copy the selected text and nul-terminate
    memcpy(ptr, (void*)&dfiles, sizeof(DROPFILES));

    GlobalUnlock(hMem);

    stgmed.hGlobal = hMem;

    //return hMem;
}

但是这似乎会导致段错误。以下是调用它的MouseMove列表消息代码:

case WM_MOUSEMOVE:
{
    // stop drag-drop from happening when the mouse is released.
    if(fMouseDown)
    {
        IDataObject *pDataObject;
        IDropSource *pDropSource;
        DWORD        dwEffect;
        DWORD        dwResult;

        FORMATETC fmtetc = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
        STGMEDIUM stgmed = { TYMED_HGLOBAL   , { 0 }, 0 };

        // transfer the current selection into the IDataObject
        CopySelection(hwnd, stgmed);
        cout << "DO WE?" << endl;

        // Create IDataObject and IDropSource COM objects
        CreateDropSource(&pDropSource);
        CreateDataObject(&fmtetc, &stgmed, 1, &pDataObject);
        //
        //  ** ** ** The drag-drop operation starts here! ** ** **
        //
        //dwResult = DoDragDrop(pDataObject, pDropSource, DROPEFFECT_COPY|DROPEFFECT_MOVE, &dwEffect);
        dwResult = DoDragDrop(pDataObject, pDropSource, DROPEFFECT_COPY, &dwEffect);

        // success!
        if(dwResult == DRAGDROP_S_DROP)
        {
            if(dwEffect & DROPEFFECT_MOVE)
            {
                // remove selection from list control
            }
            else if(dwEffect & DROPEFFECT_LINK)
            {
            }
        }
        // cancelled
        else if(dwResult == DRAGDROP_S_CANCEL)
        {
        }

        pDataObject->Release();
        pDropSource->Release();

        ReleaseCapture();
        fMouseDown = FALSE;
        fDidDragDrop = TRUE;
    }

代码已正确格式化(我已检查),但不确定为什么它不起作用。我是否使用了正确的OLE剪贴板格式来实现这一点?我不确定要使用哪个,而我找到的文档也不是很好。
谢谢, 罗布
附言:我已尝试调整此示例: http://www.catch22.net/tuts/drop-source 区别在于,他只是移动文本,而我正在尝试移动文件列表(就像在Windows中选择图标并将其拖到vid播放器上)。

你能够附加调试器并查看它在哪里崩溃吗? - ta.speot.is
我会尝试,但我使用Boost::filesystem中的路径类和try-catch异常循环,但由于某种可恶的原因,Code:Blocks附带的调试器无法看到我处理这些异常,因此它会在每个我无法访问的目录(通常是系统目录)上停止。如果有人能帮我解决这个问题,那么这将使事情变得更加容易。 - user1853098
好的,它不喜欢这一行: memcpy(ptr,(void*)&dfiles,sizeof(DROPFILES)); 我试图将DROPFILES结构体复制到我拥有的HGLOBAL ptr中。 - user1853098
第一个 memcpy() 将数据复制到了无效的内存地址,因此第二个 memcpy() 很可能尝试访问由第一个 memcpy() 损坏的内存。 - Remy Lebeau
1个回答

8
您没有为HGLOBAL块分配足够的内存。您只分配了足以容纳DROPFILES本身的内存,但未分配足够的内存来容纳与之配对的文件名。即使您正确地分配了内存,也没有正确使用DROPFILES::pFiles字段。它需要指定从DROPFILES结构开始的文件名列表的偏移量,但您将其视为内存地址。

尝试使用以下方法:
HGLOBAL CopySelection(HWND hwndList)
{
    // get the selection inside the list control

    int iPos = SendMessage(hwndList, LVM_GETNEXTITEM, (WPARAM)-1,(LPARAM)LVNI_SELECTED);
    if (iPos == -1)
        return NULL;

    cout << "iPos: " << iPos << endl;

    LVITEM item = {0};
    char buffer[256];
    string fileDir;

    item.cchTextMax = 256;
    item.pszText = buffer;
    item.mask = LVIF_TEXT;

    item.iItem = iPos;
    item.iSubItem = 1;
    ListView_GetItem(hwndList, &item);
    fileDir = item.pszText;
    fileDir += "\\";

    item.iItem = iPos;
    item.iSubItem = 0;
    ListView_GetItem(hwndList, &item);
    fileDir += item.pszText;

    item.iItem = iPos;
    item.iSubItem = 2;
    ListView_GetItem(hwndList, &item);
    fileDir += item.pszText;

    cout << "fileDir: " << fileDir << endl;

    // +2 = the filename's null terminator and the file list's null terminator
    HGLOBAL hMem = GlobalAlloc(GHND, sizeof(DROPFILES) + fileDir.length() + 2);
    if (!hMem)
        return NULL;

    DROPFILES *dfiles = (DROPFILES*) GlobalLock(hMem);
    if (!dfiles)
    {
        GlobalFree(hMem);
        return NULL;
    }

    dfiles->pFiles = sizeof(DROPFILES);
    GetCursorPos(&(dfiles->pt));
    dfiles->fNC = TRUE;
    dfiles->fWide = FALSE;
    memcpy(&dfiles[1], fileDir.c_str(), fileDir.length());

    GlobalUnlock(hMem);
    return hMem;
}

.

case WM_MOUSEMOVE:
{
    // stop drag-drop from happening when the mouse is released.
    if (fMouseDown)
    {
        IDataObject *pDataObject;
        IDropSource *pDropSource;
        DWORD        dwEffect;
        DWORD        dwResult;

        FORMATETC fmtetc = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
        STGMEDIUM stgmed = { TYMED_HGLOBAL   , { 0 }, 0 };

        // transfer the current selection into the IDataObject
        stgmed.hGlobal = CopySelection(hwnd);
        if (stgmed.hGlobal)
        {
            cout << "DO WE?" << endl;

            // Create IDataObject and IDropSource COM objects
            CreateDropSource(&pDropSource);
            CreateDataObject(&fmtetc, &stgmed, 1, &pDataObject);
            //
            //  ** ** ** The drag-drop operation starts here! ** ** **
            //
            //dwResult = DoDragDrop(pDataObject, pDropSource, DROPEFFECT_COPY|DROPEFFECT_MOVE, &dwEffect);
            dwResult = DoDragDrop(pDataObject, pDropSource, DROPEFFECT_COPY, &dwEffect);

            // success!
            if(dwResult == DRAGDROP_S_DROP)
            {
                if(dwEffect & DROPEFFECT_MOVE)
                {
                    // remove selection from list control
                }
                else if(dwEffect & DROPEFFECT_LINK)
                {
                }
            }
            // cancelled
            else if(dwResult == DRAGDROP_S_CANCEL)
            {
            }

            pDataObject->Release();
            pDropSource->Release();

            ReleaseCapture();
            fMouseDown = FALSE;
            fDidDragDrop = TRUE;
        }
    }

如果您想一次拖动多个选定的文件,请尝试以下方法:
HGLOBAL CopySelection(HWND hwndList)
{
    vector<string> files;
    UINT len = 0;

    // get the selection inside the list control

    int iPos = -1;
    do
    {
        int iPos = SendMessage(hwndList, LVM_GETNEXTITEM, iPos, LVNI_SELECTED);
        if (iPos == -1)
            break;

        LVITEM item = {0};
        char buffer[256];
        string fileDir;

        item.cchTextMax = 256;
        item.pszText = buffer;
        item.mask = LVIF_TEXT;

        item.iItem = iPos;
        item.iSubItem = 1;
        ListView_GetItem(hwndList, &item);
        fileDir = item.pszText;
        fileDir += "\\";

        item.iItem = iPos;
        item.iSubItem = 0;
        ListView_GetItem(hwndList, &item);
        fileDir += item.pszText;

        item.iItem = iPos;
        item.iSubItem = 2;
        ListView_GetItem(hwndList, &item);
        fileDir += item.pszText;

        files.push_back(fileDir);

        // +1 = the filename's null terminator
        len += (fileDir.length() + 1);

        cout << "iPos: " << iPos << ", fileDir: " << fileDir << endl;
    }
    while (true);

    if (files.empty())
        return NULL;

    // +1 = the file list's null terminator
    HGLOBAL hMem = GlobalAlloc(GHND, sizeof(DROPFILES) + len + 1);
    if (!hMem)
        return NULL;

    DROPFILES *dfiles = (DROPFILES*) GlobalLock(hMem);
    if (!dfiles)
    {
        GlobalFree(hMem);
        return NULL;
    }

    dfiles->pFiles = sizeof(DROPFILES);
    GetCursorPos(&(dfiles->pt));
    dfiles->fNC = TRUE;
    dfiles->fWide = FALSE;

    char *pFile = (char*) &dfiles[1];
    for (vector<string>::size_type i = 0; i < files.size(); ++i)
    {
        string &fileDir = files[i];

        // +1 = the filename's null terminator
        len = (fileDir.length() + 1);

        memcpy(pFile, fileDir.c_str(), len);
        pFile += len;
    }

    GlobalUnlock(hMem);
    return hMem;
}

非常感谢您!但是我对此有些不确定:dfiles->pFiles = sizeof(DROPFILES);这句话的意思是“在这个结构体大小之后的内存中查找我们的文件列表”,对吗?但是这句话:memcpy(&dfiles[1], fileDir.c_str(), fileDir.length());将char*复制到结构体之后。我想这就是为什么我的C内存分配知识不够扎实的原因——&dfiles[1]是指数组吗?如果是,那么它是一个dropfiles数组吗?但是在分配的内存中只有DROPFILE和字符串长度(加上2个字符)。 - user1853098
感谢解释。我可以接受实际指针数组,但是内存分配让我很困扰。 :) - user1853098
你为所有空终止符分配了足够的空间,并使用GHND来初始化它们,因此这不是问题。在运行时,'\\'字面量在内存中算作一个字符,因此这也不是问题。因此,最有可能的情况是您没有提供正确的文件路径值,或者VLC无法访问这些文件。 - Remy Lebeau
嗨,Remy,问题在于我如何添加字符串文字(nulls)。所以现在我可以拖放到每个媒体播放器(VLC,Media Player Class,甚至iTunes等) - 除了Windows Media Player 10。我正在使用所有的drop_effects(似乎允许目标选择哪种效果更好 - 移动,复制或链接)。我怀疑它可能无法工作,因为我在拖放时没有显示图标,或者它需要CF_HDROP之外的其他东西。有人愿意尝试吗? - user1853098
每个支持文件名拖放的应用程序都应该支持 CF_HDROP,因为Microsoft这样说。但是如果有疑问,请尝试支持更多格式。请参阅 Shell Clipboard FormatsHandling Shell Data Transfer Scenarios - Remy Lebeau
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接