首页 > 代码库 > BCB中利用剪贴板复制粘贴
BCB中利用剪贴板复制粘贴
将一个字符串拷贝到剪贴版中的不同方法:
#include <vcl/Clipbrd.hpp>
// 方法一:
TClipboard *pClipboard = new TClipboard();
pClipboard->AsText = "拷贝到剪贴版的文字数据";
delete pClipboard;
// 方法二:
TClipboard *pClipboard = new TClipboard();
pClipboard->SetTextBuf("拷贝到剪贴版的文字数据");
delete pClipboard;
// 方法三:
Clipboard()->SetTextBuf("拷贝到剪贴版的文字数据");
// 方法四:
Clipboard()->AsText = "拷贝到剪贴版的文字数据";
// 将文件Copy到剪贴板
bool CopyFileToClip(LPCSTR pcFile)
{
// sFile为拷贝文件名,多个文件以/0分隔,以/0/0结束
int nLen;
BYTE *pData;
HGLOBAL hGbl;
DROPFILES df;
if(!pcFile || !pcFile[0])
return false;
if(!OpenClipboard(0))
return false;
EmptyClipboard();
df.fNC = false;
df.fWide = false;
df.pFiles = sizeof(df);
df.pt.x = df.pt.y = 0;
for(nLen=0; pcFile[nLen]!=0; )
nLen += strlen(pcFile + nLen) + 1;
nLen += 1; //最后结尾的第二个/0字符
hGbl = GlobalAlloc(GHND, sizeof(df) + nLen);
pData = http://www.mamicode.com/(BYTE*)GlobalLock(hGbl);
if(pData != NULL)
{
memcpy(pData, &df, sizeof(df));
memcpy(pData + sizeof(df), pcFile, nLen);
GlobalUnlock(hGbl);
if(!SetClipboardData(CF_HDROP, hGbl))
{
GlobalFree(hGbl);
hGbl = NULL;
}
}
else if(hGbl != NULL)
{
GlobalFree(hGbl);
hGbl = NULL;
}
CloseClipboard();
return (hGbl != NULL);
}
//---------------------------------------------------------------------------
// 从剪贴板Paste出文件
bool PasteFileFromClip(LPCSTR pcDesDir)
{
int nLen;
bool bReturn;
LPCSTR lpcData;
HGLOBAL hGbl;
AnsiString strTemp;
DROPFILES *pdf;
SHFILEOPSTRUCT fos;
if(!pcDesDir || !pcDesDir[0])
return false;
if(!OpenClipboard(0))
return false;
hGbl = GetClipboardData(CF_HDROP);
pdf = hGbl? (DROPFILES*)GlobalLock(hGbl): NULL;
if(pdf == NULL)
{
CloseClipboard();
return false;
}
lpcData = http://www.mamicode.com/(LPCSTR)pdf + pdf->pFiles;
memset(&fos, 0, sizeof(fos));
if(pdf->fWide)
{
strTemp = WideCharLenToString((wchar_t*)lpcData, GlobalSize(hGbl)-pdf->pFiles);
fos.pFrom = strTemp.c_str();
}
else
fos.pFrom = lpcData;
fos.wFunc = FO_COPY;
fos.pTo = pcDesDir;
bReturn = SHFileOperation(&fos)==0;
GlobalUnlock(hGbl);
CloseClipboard();
return bReturn;
}
// if(CopyFileToClip("C://1.txt/0C://Test.exe/0"))
// PasteFileFromClip("D://");
BCB中利用剪贴板复制粘贴