#include <stdio.h>
#include <Windows.h>
#include <ShlObj.h>
BOOL SlxCopySingleFileToClipboard(LPCSTR lpFileToCopy)
{
DWORD dwLength = lstrlen(lpFileToCopy) + 1
DWORD dwBufferSize = dwLength + 1 + sizeof(DROPFILES)
char *lpBufferAll = new char[dwBufferSize]
if(lpBufferAll != NULL)
{
DROPFILES *pSlxDropFiles = (DROPFILES *)lpBufferAll
pSlxDropFiles->fNC = FALSE
pSlxDropFiles->pt.x = 0
pSlxDropFiles->pt.y = 0
pSlxDropFiles->fWide = FALSE
pSlxDropFiles->pFiles = sizeof(DROPFILES)
LPSTR lpFile = lpBufferAll + sizeof(DROPFILES)
lstrcpy(lpFile, lpFileToCopy)
lpFile[dwLength] = '\0'
HGLOBAL hGblFiles = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, dwBufferSize)
if(hGblFiles != NULL)
{
LPSTR lpGblData = (char *)GlobalLock(hGblFiles)
if(lpGblData != NULL)
{
memcpy(lpGblData, lpBufferAll, dwBufferSize)
GlobalUnlock(hGblFiles)
}
}
if(OpenClipboard(NULL))
{
EmptyClipboard()
SetClipboardData(CF_HDROP, hGblFiles)
CloseClipboard()
}
delete []lpBufferAll
return TRUE
}
return FALSE
}
int main()
{
SlxCopySingleFileToClipboard("C:\\Windows\\NOTEPAD.exe")
return 0
}
把文本放置到剪贴板上:CString source
//put your text in source
if(OpenClipboard())
{
HGLOBAL clipbuffer
char * buffer
EmptyClipboard()
clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1)
buffer = (char*)GlobalLock(clipbuffer)
strcpy(buffer, LPCSTR(source))
GlobalUnlock(clipbuffer)
SetClipboardData(CF_TEXT,clipbuffer)
CloseClipboard()
}
从剪接板上获取文本:
char * buffer
if(OpenClipboard())
{
buffer = (char*)GetClipboardData(CF_TEXT)
//do something with buffer here
//before it goes out of scope
}
CloseClipboard()
GetClipboardData(CF_TEXT)参考MSDN GetClipboardData的用法及例子.
HGLOBAL hglb
LPTSTRlptstr
HWND hEdit = (HWND)GetDlgItem(hDlg, IDC_EDIT1)
if (!IsClipboardFormatAvailable(CF_TEXT))
return
if (!OpenClipboard(hwndMain))
//hwndMain换成你的主窗口句柄
return
hglb = GetClipboardData(CF_TEXT)
if (hglb != NULL)
{
lptstr = GlobalLock(hglb)
if (lptstr != NULL)
{
SetWindowText(hEdit, lptstr)
GlobalUnlock(hglb)
}
}
CloseClipboard()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)