Does MFC provide a quick way to throw text on the clipboard?

此生再无相见时 提交于 2019-12-08 18:09:01

问题


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?

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?

Update: Created a new question based on mwigdahl's response


回答1:


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();
}


来源:https://stackoverflow.com/questions/2253476/does-mfc-provide-a-quick-way-to-throw-text-on-the-clipboard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!