51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

C++ 复制文件到剪切板

具体实现 {#具体实现}

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | bool CopyFilesToClipboard(const vector<string>& paths) { UINT uDropEffect; HGLOBAL hGblEffect; LPDWORD lpdDropEffect; DROPFILES stDrop; HGLOBAL hGblFiles; LPSTR lpData; uDropEffect = RegisterClipboardFormat(_T("DropEffect")); hGblEffect = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(DWORD)); if (hGblEffect) { lpdDropEffect = (LPDWORD)GlobalLock(hGblEffect); if (lpdDropEffect) { *lpdDropEffect = DROPEFFECT_COPY;//复制; 剪贴则用DROPEFFECT_MOVE } GlobalUnlock(hGblEffect); } else { return ""; } stDrop.pFiles = sizeof(DROPFILES); stDrop.pt.x = 0; stDrop.pt.y = 0; stDrop.fNC = FALSE; stDrop.fWide = FALSE; int filesNameLen = 1; for (auto path : paths) { filesNameLen += strlen(path.c_str()) + 1; } hGblFiles = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(DROPFILES) + filesNameLen); if (hGblFiles) { lpData = (LPSTR)GlobalLock(hGblFiles); if (lpData) { int offset = sizeof(DROPFILES); memcpy(lpData, &stDrop, sizeof(DROPFILES)); for (auto path : paths) { strcpy_s(lpData + offset, strlen(path.c_str()) + 1, path.c_str()); offset += strlen(path.c_str()) + 1; } } GlobalUnlock(hGblFiles); } else { return ""; } if (OpenClipboard(NULL)) { EmptyClipboard(); SetClipboardData(CF_HDROP, hGblFiles); SetClipboardData(uDropEffect, hGblEffect); CloseClipboard(); return ""; } else { return ""; } } |

使用 {#使用}

|-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 | int main() { std::vector<std::string> paths; paths.push_back("D:\\vsdemo\\test.txt"); paths.push_back("C:\\test1.txt"); CopyFilesToClipboard(paths); } |

参考链接:https://www.jianshu.com/p/1b05226293df


赞(1)
未经允许不得转载:工具盒子 » C++ 复制文件到剪切板