MFC是否提供了一种在剪贴板上引发文本的快速方法? [英] Does MFC provide a quick way to throw text on the clipboard?

查看:73
本文介绍了MFC是否提供了一种在剪贴板上引发文本的快速方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在代码库中添加的剪贴板代码是很底层的-分配全局内存,依此类推.对于简单的情况,我只想在剪贴板上放置一些纯文本,是否有任何例程可以包装所有内容?

The add-to-clip-board code we have in our code base is quite low-level - allocating global memory and so on. For the simple case I just want to put some plain text on the clipboard, are there any routines which can wrap all that stuff?

一个示例是CRichEditCtrl具有Copy()& Cut()方法可自动将当前选择内容放置在剪贴板上. MFC是否可以单独使用这种功能?

An example is that CRichEditCtrl has Copy() & Cut() methods which automatically put the current selection on the clipboard. Does MFC make this kind of functionality available in isolation?

更新:创建推荐答案

否,但将它包装起来并不难.改编自弗罗斯特代码(未经测试):

No, but it's not that hard to wrap it yourself. Adapted from Frost Code (and untested):

void SetClipboardText(CString & szData)
{
    HGLOBAL h;
    LPTSTR arr;

    h=GlobalAlloc(GMEM_MOVEABLE, szData.GetLength()+1);
    arr=(LPTSTR)GlobalLock(h);
    strcpy_s((char*)arr, szData.GetLength()+1, szData.GetBuffer());
    szData.ReleaseBuffer();
    GlobalUnlock(h);

    ::OpenClipboard (NULL);
    EmptyClipboard();
    SetClipboardData(CF_TEXT, h);
    CloseClipboard();
}

这篇关于MFC是否提供了一种在剪贴板上引发文本的快速方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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