From 9c21257c221e121c6e3672134033ad8a61d10491 Mon Sep 17 00:00:00 2001 From: chirsz-ever Date: Sun, 5 Jul 2020 22:57:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E7=B1=BB=E7=AE=A1=E7=90=86=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 `ege::mb2w` 函数。 --- src/ege_head.h | 7 +++++-- src/egegapi.cpp | 35 +++++++++++++++++------------------ src/image.cpp | 5 ++--- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/ege_head.h b/src/ege_head.h index 6dd4e5ce..199b1ca1 100644 --- a/src/ege_head.h +++ b/src/ege_head.h @@ -565,8 +565,11 @@ struct count_ptr { //Mutex* m_mutex; }; -// convert wide string to multibyte string -std::string w2mb(LPCWSTR wStr); +// convert wide char string to multibyte ANSI string +std::string w2mb(const wchar_t wStr[]); + +// convert multibyte ANSI string to wide char string +std::wstring mb2w(const char mbStr[]); void internal_panic(LPCWSTR errmsg); diff --git a/src/egegapi.cpp b/src/egegapi.cpp index 9e1f93a6..f41857a7 100644 --- a/src/egegapi.cpp +++ b/src/egegapi.cpp @@ -33,13 +33,20 @@ void guiupdate(_graph_setting* pg, egeControlBase* &root); float _GetFPS(int add); int getflush(); -std::string w2mb(LPCWSTR wStr) { +std::string w2mb(const wchar_t wStr[]) { int bufsize = WideCharToMultiByte(CP_ACP, 0, wStr, -1, NULL, 0, 0, 0); std::string mbStr(bufsize, '\0'); WideCharToMultiByte(CP_ACP, 0, wStr, -1, &mbStr[0], bufsize, 0, 0); return mbStr; } +std::wstring mb2w(const char mbStr[]) { + int bufsize = MultiByteToWideChar(CP_ACP, 0, mbStr, -1, NULL, 0); + std::wstring wStr(bufsize, L'\0'); + MultiByteToWideChar(CP_ACP, 0, mbStr, -1, &wStr[0], bufsize); + return wStr; +} + void internal_panic(LPCWSTR errmsg) { MessageBoxW(graph_setting.hwnd, errmsg, L"EGE INTERNAL ERROR", MB_ICONSTOP); ExitProcess((UINT)grError); @@ -107,12 +114,8 @@ is_run() { } void setcaption(LPCSTR caption) { - int bufsize = MultiByteToWideChar(CP_ACP, 0, caption, -1, NULL, 0); - if (bufsize) { - std::wstring new_caption(bufsize, L'\0'); - MultiByteToWideChar(CP_ACP, 0, caption, -1, &new_caption[0], bufsize); - setcaption(new_caption.c_str()); - } + const std::wstring& new_caption = mb2w(caption); + setcaption(new_caption.c_str()); } void setcaption(LPCWSTR caption) { @@ -2376,9 +2379,8 @@ void EGEAPI ege_drawtext(LPCSTR textstring, float x, float y, PIMAGE pimg) { MultiByteToWideChar(CP_ACP, 0, textstring, -1, wStr, 128); ege_drawtext_p(wStr, x, y, img); } else { - std::wstring wStr(bufferSize + 1, L'\0'); - MultiByteToWideChar(CP_ACP, 0, textstring, -1, &wStr[0], bufferSize + 1); - ege_drawtext_p(&wStr[0], x, y, img); + const std::wstring& wStr = mb2w(textstring); + ege_drawtext_p(wStr.c_str(), x, y, img); } } CONVERT_IMAGE_END; @@ -2444,16 +2446,13 @@ draw_frame(PIMAGE img, int l, int t, int r, int b, color_t lc, color_t dc) { int inputbox_getline(LPCSTR title, LPCSTR text, LPSTR buf, int len) { - WCHAR _title[256], _text[256], *_buf = (WCHAR*)malloc(len * 2); - int ret; - MultiByteToWideChar(CP_ACP, 0, title, -1, _title, 256); - MultiByteToWideChar(CP_ACP, 0, text, -1, _text, 256); - buf[0] = 0; - ret = inputbox_getline(_title, _text, _buf, len); + const std::wstring& title_w = mb2w(title); + const std::wstring& text_w = mb2w(text); + std::wstring buf_w(len, L'\0'); + int ret = inputbox_getline(title_w.c_str(), text_w.c_str(), &buf_w[0], len); if (ret) { - WideCharToMultiByte(CP_ACP, 0, _buf, -1, buf, len, 0, 0); + WideCharToMultiByte(CP_ACP, 0, buf_w.c_str(), -1, buf, len, 0, 0); } - free(_buf); return ret; } diff --git a/src/image.cpp b/src/image.cpp index dd09d7e5..ddf6bf48 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -311,9 +311,8 @@ IMAGE::getimage(LPCSTR filename, int zoomWidth, int zoomHeight) { if (ret == 0) return 0; } - WCHAR wszPath[MAX_PATH*2+1]; - MultiByteToWideChar(CP_ACP, 0, filename, -1, wszPath, MAX_PATH*2); - return getimage(wszPath, zoomWidth, zoomHeight); + const std::wstring& wszPath = mb2w(filename); + return getimage(wszPath.c_str(), zoomWidth, zoomHeight); } inline void getimage_from_IPicture(PIMAGE self, IPicture* pPicture) {