wxWigets学习笔记1

本文详细介绍了在wxWidgets项目中如何解决源代码文件的字符编码问题,并提供了多种字符串类型之间的转换方法,包括wxString与其他类型如char*、wchar_t*等之间的相互转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.源代码汉字问题

如果源代码文件编码是utf-8的,无需其他特别处理,可以正常编译wxWidgets程序。如果wxWidgets程序(非控制台程序)不能正常显示或编译,在编译选项上加上(非控制台程序)-finput-charset=UTF-8即可。是否能正常显示与wxWidgets版本有关。

如果源代码格式设置为windows-936,那么含有汉字字符串的文件编译时会出现:error: converting to execution character set: Illegal byte sequence 的错误提示。这时,需要在编译选项中加入-finput-charset=GBK参数。然后可以正常编译。(windows下codeblocks设置文件使用默认编码default时,实际上文件是ANSI格式,代码页为windows-936)

MinGW编译的控制台程序,如果采用源文件默认配置的utf8编码,cout << "Hello world!中文你好" << endl; 的中文部分会出现乱码。这是因为windows控制台默认代码页是936,不能正常显示utf8输出。解决这个问题,可以把源文件encoding修改为system default(其实windows下就是ANSI格式),就可以正常显示了。

MinGW编译的控制台程序,如果采用源文件默认配置的utf8编码,可以通过wcout输出宽字符来使控制台程序正确显示宽字符汉字:

                        setlocale(LC_ALL, "chs");

                        wcout << L"中文" << endl;

       但是需要注意的是,不能像在vc编译器中一样使用wcout.imbue(locale("chs"));因为在MinGW中,Locale只支持"C"和""为合法的参数。使用其他的任何Locale都会抛出异常。

但源文件如果是ANSI格式,汉字前加了L的宽字符,譬如:cout << L"Hello world!中文你好" << endl;,那么又会出现error: converting to execution character set: Illegal byte sequence 的错误提示你无法编译。这时编译选项加入-finput-charset=GBK,编译通过了,但控制台程序还是无法正常显示汉字,因为控制台默认不支持宽字符

2.wxString与其他类型的转换(转自wxWiki)

char* to wxString

const char* chars = "Hello world";

// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed

wxString mystring(chars, wxConvUTF8);

或者

wxString mystring;

mystring =  wxString::FromUTF8(chars);

wxString to char*

void my_function(const char* foo)

{

}

...

wxString mystring(wxT("HelloWorld"));

// you could give the encoding you want as a parameter to mb_str(), e.g. mb_str(wxConvUTF8)

my_function( mystring.mb_str() );

mb_str() returns a temporary pointer; if you need the output for more than one function call (as is the case above), you can store the char buffer for a little while :

wxString s(wxT("some string") );

wxCharBuffer buffer=s.ToUTF8();

foo( buffer.data() );  // data() returns const char *

bar( buffer.data(), strlen(buffer.data()) );  // in case you need the lenght of the data

And if you really need to copy it in to char* (but why would you? ;) :

wxString mystring(wxT("HelloWorld"));

char cstring[1024];

// assuming you want UTF-8, change the wxConv* parameter as needed

strncpy(cstring, (const char*)mystring.mb_str(wxConvUTF8), 1023);

You can also use ToUTF8(), since which encoding you get is clearer than with mb_str()

From const char* to char*:

wxString mystring(wxT("HelloWorld"));

(const_cast<char*>((const char*)mystring.mb_str()))

wchar_t* to wxString

const wchar_t* chars = L"Hello world";

wxString mystring(chars);

wxString to wchar_t*

See the following methods in the docs :

wxString::wc_str()

wxString::wchar_str()

wxString to TCHAR

TCHAR tCharString[255];

wxString myString(_T("Hello World"));

const wxChar* myStringChars = myString.c_str();  

for (int i = 0; i < myString.Len(); i++) {

   tCharString[i] = myStringChars [i];

}

tCharString[myString.Len()] = _T('\0');

int to wxString

wxString mystring =wxString::Format(wxT("%i"),myint);

or

wxString mystring;

mystring << myint;

float to wxString

wxString mystring =wxString::Format(wxT("%f"), myfloat);

or

wxString mystring;

mystring << myfloat;

wxString to integer number

wxString number(wxT("145"));

long value;

if(!number.ToLong(&value)) { /* error! */ }

or

wxString str =_T("123");

int num;

num = wxAtoi(str);

wxString to floating-point number

wxString number(wxT("3.14159"));

double value;

if(!number.ToDouble(&value)){ /* error! */ }

std::string to wxString

std::string stlstring = "Hello world";

// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed

wxString mystring(stlstring.c_str(), wxConvUTF8);

Starting from wxWidgets 2.9, you may also use the appropriate constructor

std::string stlstring = "Hello world";

// assuming your string is encoded as the current locale encoding (wxConvLibc)

wxString mystring(stlstring);

wxString to std::string

wxWidgets 2.8 :

wxString mystring(wxT("HelloWorld"));

std::string stlstring = std::string(mystring.mb_str());

Under wxWidgets 2.9, you may use

wxString::ToStdString()

std::wstring to wxString

Starting from wxWidgets 2.9, you may use the appropriate constructor

std::sstring stlstring = L"Hello world";

// assuming your string is encoded as the current locale encoding (wxConvLibc)

wxString mystring(stlstring);

wxString to std::wstring

Under wxWidgets 2.9, you may use

wxString::ToStdWstring()

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值