如何通过 win32 API 函数复制文件并在我的桌面中通过 (Ctrl+v) 粘贴 [英] How to copy files by win32 API functions and paste by (Ctrl+v) in my Desktop

查看:188
本文介绍了如何通过 win32 API 函数复制文件并在我的桌面中通过 (Ctrl+v) 粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 win32 API 函数复制一个文件,然后在我的其他 windows 文件夹的桌面中通过 (Ctrl+c) 将其粘贴".

I want to copy a file by win32 API Functions and "paste" it by (Ctrl+c) in my desktop of other folder of windows.

我知道 CopyFileEx 并用它复制文件,但此功能复制和粘贴.我只想在我的 main() 程序中复制文件(通过 win32 API 函数)并将其粘贴"到桌面或 Windows 的其他文件夹中.

I Know CopyFileEx and copies file with it but this function copy and paste. I want to just copy file in my main() program (by win32 API function) and "paste" it in desktop or other folder of windows.

我不想使用 SHFileOperation 函数.

I don't want to use SHFileOperation function.

推荐答案

如果您希望在粘贴剪贴板内容时将实际文件复制到目标文件夹,您需要使用CF_HDROP 格式,例如:

If you want the actual file to be copied to the destination folder when the clipboard content is pasted to it, you need to put the path string onto the clipboard using the CF_HDROP format, eg:

// error handling omitted for brevity...

LPWSTR path = ...;
int size = sizeof(DROPFILES)+((lstrlenW(path)+2)*sizeof(WCHAR));
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, size);
DROPFILES *df = (DROPFILES*) GlobalLock(hGlobal);
ZeroMemory(df, size);
df->pFiles = sizeof(DROPFILES);
df->fWide = TRUE;
LPWSTR ptr = (LPWSTR) (df + 1);
lstrcpyW(ptr, path);
GlobalUnlock(hGlobal);
SetClipboardData(CF_HDROP, hGlobal);

更新:或者,请参阅The Old New Thing"博客上的以下文章:

Update: Alternatively, see the following article on the "The Old New Thing" blog:

将文件复制到剪贴板以便粘贴它进入资源管理器或电子邮件或其他

它使用 GetUIObjectOfFile()OleSetClipboard() 而不是 CF_HDROP.但最终效果是相似的 - 一个放置在剪贴板上的对象代表目标文件,资源管理器在粘贴操作期间识别该对象,因此它将文件复制到正在粘贴的文件夹中.

It uses GetUIObjectOfFile() and OleSetClipboard() instead of CF_HDROP. But the end effect is similar - an object placed on the clipboard that represents the target file, and Explorer recognizes that object during a paste operation so it will copy the file to the folder being pasted in.

这篇关于如何通过 win32 API 函数复制文件并在我的桌面中通过 (Ctrl+v) 粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆