ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – Drag Drop Win API 32

2019-07-29 09:18:16  阅读:251  来源: 互联网

标签:ole c api winapi


我正在尝试将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

不同的是,他只是移动文本,而我正在尝试移动文件列表(比如在窗口中选择图标并拖动到视频播放器上).

解决方法:

您没有为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;
}

标签:ole,c,api,winapi
来源: https://codeday.me/bug/20190729/1570191.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有