CEGUI与其他程序剪切复制粘帖的实现(支持中文)~~~

void xx()
{
using namespace CEGUI;
CEGUI::Window * window = CEGUI::System::getSingleton().getGUISheet()->getActiveChild();
const CEGUI::String& type = window->getType();
CEGUI::MultiLineEditbox * mleb = 0;
CEGUI::Editbox * eb = 0;
if (type == "TaharezLook/MultiLineEditbox" ¦¦ type == "Vanilla/MultiLineEditbox" ¦¦ type == "WindowsLook/MultiLineEditbox")
{
mleb = static_cast<CEGUI::MultiLineEditbox*>(window);
}
else if(type == "TaharezLook/Editbox" ¦¦ type == "Vanilla/Editbox" ¦¦ type == "WindowsLook/Editbox")
{
eb = static_cast<CEGUI::Editbox*>(window);
}

if(mleb)
{
if (!mPastState && (mCopyState ¦¦ mCutState && mleb->getSelectionLength() > 0))  //复制,剪切
{
HWND hwnd = GetActiveWindow();
assert(OpenClipboard(hwnd));
EmptyClipboard();
CEGUI::String string(mleb->getText().data());
string.erase(mleb->getSelectionEndIndex(),mleb->getText().size() - mleb->getSelectionEndIndex());
string.erase(0,mleb->getSelectionStartIndex());

HGLOBAL clipBuffer;
WCHAR* buffer;
const char* str = string.c_str();
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, str, strlen(str), NULL, 0);

wchar_t* wszString = new wchar_t[wcsLen];
//转换
::MultiByteToWideChar(CP_UTF8, NULL, str, strlen(str), wszString, wcsLen);
wszString[wcsLen] = '/0';
clipBuffer = GlobalAlloc(GMEM_DDESHARE,wcsLen * 2 + 2);
buffer = (WCHAR *)GlobalLock(clipBuffer);

wcscpy(buffer,wszString);
buffer[wcsLen] = L'/0';
GlobalUnlock(clipBuffer);
SetClipboardData(CF_UNICODETEXT,clipBuffer);
CloseClipboard();
if (mCutState)
{
CEGUI::String s(mleb->getText().data());
s.erase((CEGUI::String::size_type)(mleb->getSelectionStartIndex()),(CEGUI::String::size_type)mleb->getSelectionLength());
mleb->setCaratIndex(mleb->getSelectionStartIndex());
mleb->setText(s.data());
mleb->ensureCaratIsVisible();
}
}
else if ((mPastState && mCopyState) ¦¦ (mPastState && mCutState)) //粘帖
{
HWND hwnd = GetActiveWindow();
assert(OpenClipboard(hwnd));
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
WCHAR * buffer;
buffer = (WCHAR*)GlobalLock(hData);
GlobalUnlock(hData);
CloseClipboard();

int utf8Num = WideCharToMultiByte(CP_UTF8, NULL, buffer, wcslen(buffer), NULL, 0, NULL, NULL);
char * str = new char[utf8Num + 1];
WideCharToMultiByte(CP_UTF8, NULL, buffer, wcslen(buffer), str, utf8Num, NULL, NULL);
str[utf8Num] = '/0';
CEGUI::String string(mleb->getText().data());
string.erase(mleb->getSelectionStartIndex(),mleb->getSelectionLength());
CEGUI::String strNum((CEGUI::utf8*)str);
string.insert(mleb->getSelectionStartIndex(),strNum);
mleb->setText(string.data());
mleb->setCaratIndex(mleb->getSelectionStartIndex() + strNum.size());
mleb->ensureCaratIsVisible();
mPastState = false;
}
}
}

转载至:
http://lisuyong.com/2010/06/02/cegui%E4%B8%8E%E5%85%B6%E4%BB%96%E7%A8%8B%E5%BA%8F%E5%89%AA%E5%88%87%E5%A4%8D%E5%88%B6%E7%B2%98%E5%B8%96%E7%9A%84%E5%AE%9E%E7%8E%B0%EF%BC%88%E6%94%AF%E6%8C%81%E4%B8%AD%E6%96%87%EF%BC%89/

你可能感兴趣的:(GUI)