Advice required to insert one string into another once obtaining text from clipboard

谁都会走 提交于 2019-12-06 00:03:30

Try something like this:

case WM_PASTE:
{
    std::wstring cbtext;

    if( !OpenClipboard(hwnd) ) // open clipboard
        return 0;

    // get clipboard data
    HANDLE hClipboardData = GetClipboardData(CF_UNICODETEXT);
    if( hClipboardData )
    {
        // Call GlobalLock so that to retrieve a pointer
        // to the data associated with the handle returned
        // from GetClipboardData.

        cbtext = (LPWSTR) GlobalLock(hClipboardData);

        // Unlock the global memory.
        GlobalUnlock(hClipboardData);
    }

    // Finally, when finished I simply close the Clipboard
    // which has the effect of unlocking it so that other
    // applications can examine or modify its contents.

    CloseClipboard();

    if (cbtext.empty())
        return 0;

    // format the new text with the clipboard data inserted as needed

    int len = GetWindowTextLengthW(hwnd);
    std::wstring newtext(len, 0);
    if (len > 0)
        GetWindowTextW(hWnd, &newtext[0], len);

    DWORD start, end;
    SendMessageW(hwnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);

    if (end > start)
        newtext.replace(start, end-start, cbtext);
    else
        newtext.insert(start, cbtext);

    // parse the new text for validity

    // code for parsing text 
    if( IsTextValid )
        SetWindowTextW( hwnd, newtext.c_str() );

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