首页 > 技术文章 > C++打开剪切板,获取剪切板数据

lhwblog 2017-02-21 20:28 原文

 1 if (::OpenClipboard(NULL) && ::IsClipboardFormatAvailable(CF_HDROP))
 2 {
 3    HDROP hDrop = (HDROP)::GetClipboardData(CF_HDROP);
 4    if (hDrop != NULL)
 5    {
 6       TCHAR filename[MAX_PATH];
 7       int fileCount = ::DragQueryFile(hDrop, 0xFFFFFFFF, filename, MAX_PATH);
 8       for (int i = 0; i < fileCount; ++i)
 9       {}
10     }
11 }

 

1.OpenClipboard,

BOOL OpenClipboard(
__in HWND hWndNewOwner
);
参数
hWndNewOwne Long类型,与打开剪切板相关联的窗口句柄。如果这个参数为NULL,打开剪贴板与当前任务相关联。
返回值
如果函数执行成功,返回非零值.
如果函数执行失败,返回零,为了获得更多的错误信息,调用GetLastError.
 
2.IsClipboardFormatAvailable
常用剪贴板数据格式:
CF_TEXT 以NULL结尾的ASCII字符的文本格式
CF_BITMAP 图像句柄HBITMAP
注:若应用程序要识别多个剪贴板格式,应使用函数GetPriorityClipboardFormat 达到目的。
 
3.GetClipboardData
获得剪切板的数据。
 
4.DragQueryFile(hDrop, 0xFFFFFFFF, filename, MAX_PATH)
DragQueryFile是一个成功拖放操作后获取被拖放文件的名称等信息的函数。
 
UINT DragQueryFile(
HDROPhDrop,
UINTiFile,
LPTSTRlpszFile,
UINTcch
);
 
hDrop
Identifier of the structure containing the file names of the dropped files.
用于区分”包含被拖拽文件名称结构”的句柄。
即存放所拖放文件名称的数据结构的句柄,也就是文件名缓冲区的句柄
 
iFile
Index of the file to query. If the value of theiFileparameter is 0xFFFFFFFF,DragQueryFilereturns a count of the files dropped. If the value of theiFileparameter is between zero and the total number of files dropped,DragQueryFilecopies the file name with the corresponding value to the buffer pointed to by thelpszFileparameter.
文件索引编号(用于指明所要查询文件的序号, 如果拖进多个文件,则索引编号从零开始),如果iFile值为 0xFFFFFFFF 时,返回的是拖曳到窗体上的文件的个数。如果iFile值在0和拖拽文件总数之间时,DragQueryFile拷贝与文件名存储缓冲区大小适应的文件名称到缓冲区中。
 
lpszFile
Address of a buffer to receive the file name of a dropped file when the function returns. This file name is a null-terminated string. If this parameter is NULL,DragQueryFilereturns the required size, in characters, of the buffer.
函数返回时,用于存储拖拽文件名称的缓冲指针。文件名称是一个以空终止“\0”结尾的字符串。如果此参数是NULL,DragQueryFile函数返回拖拽的文件名的长度。函数DragQueryFile得到的文件名,是带完整路径的文件名。
 
cch
Size, in characters, of thelpszFilebuffer.
存储拖拽文件名称缓冲区的大小,即lpszFile指针所指缓冲区的字符数。
 
参考文档:http://www.cnblogs.com/wind-net/archive/2012/11/01/2749558.html
http://baike.baidu.com/link?url=8iXrSheAjTSKFIVYCjnMh2t6dmcym2LQyQ5_RjKSLDzryNRPWt4Wsulq00OF3_wk43iwPRNjwU9_g4poBmulG05_IyVJrA8fYwXCLWqZoOa
 

推荐阅读