剪贴板相关操作实例

如果你的项目中不支持OLE,请加上下列代码: 

#include <afxole.h> 

并在初始化时加入:

OleInitialize(NULL); 

1、拷贝文字到剪贴板

CString str = _T("要复制的文本"); //复制到剪贴板
if (OpenClipboard())
{
    HGLOBAL clipBuffer;
    EmptyClipboard();

#ifdef _UNICODE //复制Unicode字符串到剪贴板
    wchar_t *buffer;
    clipBuffer = GlobalAlloc(GMEM_DDESHARE, 2 * lstrlen(str) + sizeof(wchar_t));
    buffer = (wchar_t *)GlobalLock(clipBuffer);
    wcscpy(buffer, strSel);
    GlobalUnlock(clipBuffer);
    SetClipboardData(CF_UNICODETEXT, clipBuffer);
#else //复制ANSI字符串到剪贴板
    char *pbuff;
    clipBuffer = GlobalAlloc(GMEM_DDESHARE, str.GetLength() + 1);
    pbuff = (char *)GlobalLock(clipBuffer);
    strcpy(pbuff, LPCSTR(strSel));
    GlobalUnlock(clipBuffer);
    SetClipboardData(CF_TEXT, clipBuffer);
#endif
    CloseClipboard();
}

2、图像拷贝到剪贴板

CWnd *pWnd = GetDlgItem(IDC_IMAGE_SELECT);
WINDOWPLACEMENT *winPlacement;
WinPlacement = new WINDOWPLACEMENT;
pWnd->GetWindowPlacement(winPlacement); //获得一个控件的位置
CDC *dcTemp;
dcTemp = new CClientDC(FromHandle(m_hWnd));
CDC memDC;
memDC.CreateCompatibleDC(dcTemp); //创建一个兼容的DC
CBitmap bm;
CSize sz(lWidth, lHeight);
bm.CreateCompatilbleBitmap(dcTemp, sz.cx, sz.cy); //创建一个兼容的位图
CBitmap *oldbm = memDC.SelectObject(&bm);
memDC.BitBlt(0, 0, sz.cx, sz.cy, dcTemp, winPlacement->rcNormalPosition.left,
             winPlacement->rcNormalPosition.top, SRCCOPY);
pWnd->OpenClipboard(); //打开剪贴板,不用pWnd->GetParent()->OpenClipard();
EmptyClipard();
SetClipardData(CF_BITMAP, bm.m_hObject); //粘贴到剪贴板
CloseClipard();
memDC.SelcetObject(oldbm);
delete dcTemp;

3、从剪贴板获取文字

int textflag;
#ifdef _UNICODE
textflag = CF_UNICODETEXT;
#else
textflag = CF_TEXT;
#endif
if (!IsClipboardFormatAvailable(textflag)) //非unicode为CF_TEXT,unicode 为CF_UNICODETEXT
{
    MessageBox(hWnd, _T("剪贴板上无文本数据,不能粘贴!"), _T("OpenFC"), MB_OK);
    return false;
}
if (!OpenClipboard(hWnd))
{
    MessageBox(hWnd, _T("剪贴板打开失败!"), L"OpenFC", MB_OK);
    return false;
}
(TCHAR *)hClipboardText = GetClipboardData(textflag); //非unicode为CF_TEXT,unicode 为CF_UNICODETEXT
if (!hClipboardText)
{
    MessageBox(hWnd, _T("无法读取剪贴板数据!"), _T("OpenFC"), MB_OK);
    CloseClipboard();
    break;
}
if (GetFocus() == hwndEditCode)
    SendMessage(hwndEditCode, EM_REPLACESEL, 1, (LPARAM)GlobalLock(hClipboardText)); //发送到窗口
if (GetFocus() == hwndEditOut)
    SendMessage(hwndEditOut, EM_REPLACESEL, 1, (LPARAM)GlobalLock(hClipboardText));
GlobalUnlock(hClipboardText);
CloseClipboard();

4、从剪贴板获取图像

if (OpenClipboard())
{
    //获得剪贴板数据
    HBITMAP handle = (HBITMAP)GetClipboardData(CF_BITMAP); //CF_DIB或者CF_BIMAP
    CBitmap *bm = CBitmap::FromHandle(handle);
    CClientDC cdc(this);
    CDC dc;
    dc.CreateCompatibleDC(&cdc);
    dc.SelectObject(bm);
    cdc.BitBlt(0, 0, 200, 200, &dc, 0, 0, SRCCOPY);
    CloseClipboard();
}

5、设置并使用自定义格式

使用RegisterClipboardFormat()函数,可以复制和粘贴任何你需要的数据类型。比如我们有以下一个数据类型:

struct MyFormatData
{
    long val1;
    int val2;
};

我们要把它复制到剪贴板,可以使用如下的代码:

UINT format = RegisterClipBoardFormat("MY_CUSTOM_FORMAT");
if (OpenClipboard())
{
    MyFormatData data;
    data.val1 = 100;
    data.val2 = 200;
    HGLOBAL clipbuffer;
    EmptyClipboard();
    clipbuffer = GlobalAlloc(GMEM_DDESHARE, sizeof(MyFormatData));
    MyFormatData *buffer = (MyFormatData *)GlobalLock(clipbuffer);
    //保存到内存
    *buffer = data;
    //保存到剪贴板
    GlobalUnlock(clipbuffer);
    SetClipboardData(format, clipbuffer);
    CloseClipboard();
}

读取数据使用以下代码:

UINT format = RegisterClipboardFormat("MY_CUSTOM_FORMAT");
MyFormatData data;
if (Openclipboard())
{
    HANDLE hData = GetClipboardData(format);
    MyFormatData *buffer = (MyFormatData *)GlobalLock(hData);
    data = *buffer;
    GlobalUnlock(hData);
    CloseClipboard();
}

0 Comments (12356 Views)

Leave a Reply

Your email address will not be published. Required fields are marked *