UTF-8文本到剪贴板C [英] UTF-8 text to clipboard C
问题描述
我一直在寻找如何带来字符串的方法,
I have been looking around on how to bring a string,
const char* output = "ヽ(⌐■_■)ノ♪♬";
到剪贴板.
SetClipboardData(CF_UNICODETEXT, hMem);
我尝试了MultiByteToWideChar,但是我只听到了噪音,而且还声称不能将UTF-16LE保存到剪贴板(wchar_t).老实说我只是感到困惑.方向或代码示例会很棒.
I have tried MultiByteToWideChar, but I have gotten only noise and also conflicting claims that you cannot save UTF-16LE to clipboard (wchar_t). Honestly I am just confused. A direction or code sample would be great.
推荐答案
Windows使用UTF-16LE.该字符串应使用L
前缀创建.要使用UTF8,可以声明带u8
前缀的字符串.例如:
Windows uses UTF-16LE. The string should be created with L
prefix. To use UTF8 you can declare the string with u8
prefix. For example:
const char* text = u8"ヽ(⌐■_■)ノ♪♬E";
然后,您必须使用MultiByteToWideChar
将UTF8转换为UTF16并在WinAPI中使用.请注意,要使用u8
,您需要较新的编译器,例如VS2015.
Then you have to use MultiByteToWideChar
to convert UTF8 to UTF16 and use in WinAPI. Note that to use u8
you need newer compilers like VS2015.
首先使用UTF16更容易.例如:
It's easier to use UTF16 in the first place. For example:
const wchar_t* text = L"ヽ(⌐■_■)ノ♪♬E";
int len = wcslen(text);
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * sizeof(wchar_t));
wchar_t* buffer = (wchar_t*)GlobalLock(hMem);
wcscpy_s(buffer, len + 1, text);
GlobalUnlock(hMem);
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hMem);
CloseClipboard();
这篇关于UTF-8文本到剪贴板C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!