比如内容为"abc一二三"的wxString对象,我想得到的字符数量应该为6,而不是length之类的得到的9之类的字节数。
请大家帮忙指点一下,多谢了。
请问如何获得含有中文的wxString的字符数量呢?
Re: 请问如何获得含有中文的wxString的字符数量呢?
What platform and wxWidgets version are you using?
When i run this code under Windows:It shows "6".
When i run this code under Windows:
Code: Select all
wxString s( wxT("abc一二三") );
wxLogMessage("length: %d", s.Length());
Use the source, Luke!
Re: 请问如何获得含有中文的wxString的字符数量呢?
They are WIndows 10 and wxWidgets 3.1.0, Sir.
To be frank, this issue is a link in my question chain. The origin is to implement an auto linefeed method, for wxString::Wrap doesn't work in my project (maybe others don't have this problem at all, so weird). Naturally, I have to obtain the character number of the concerned string for the benefit of finding the position at which the linefeed is inserted. The problem of finding character number is solved by analyzing and processing the UTF-8 code.
But I soon found myself in the trouble of the mismatch of the dimension between wxDC::GetTextExtent and wxStaticText::GetClientSize.GetX. When the string of increasing X dimension (obtained by GetTextExtent method) approach the end of lines (specified with GetClientSize.GetX), it is designed to do a linefeed. It works quiet well if one just examines in Watch window: the wxStaticText window has a width of 500, and the dimension of the string is approaching 480...497... auto linefeed, Great! But when I take a look at the painted window, uh, the end of the string just passes the middle of the window! I have tried lots of DC, e.g., wxDC, wxScreenDC, wxPainterDC and wxWindow::GetTextExtent, but no satisfying outcome emerged.
Any suggestion on these would be highly appreciated.
To be frank, this issue is a link in my question chain. The origin is to implement an auto linefeed method, for wxString::Wrap doesn't work in my project (maybe others don't have this problem at all, so weird). Naturally, I have to obtain the character number of the concerned string for the benefit of finding the position at which the linefeed is inserted. The problem of finding character number is solved by analyzing and processing the UTF-8 code.
But I soon found myself in the trouble of the mismatch of the dimension between wxDC::GetTextExtent and wxStaticText::GetClientSize.GetX. When the string of increasing X dimension (obtained by GetTextExtent method) approach the end of lines (specified with GetClientSize.GetX), it is designed to do a linefeed. It works quiet well if one just examines in Watch window: the wxStaticText window has a width of 500, and the dimension of the string is approaching 480...497... auto linefeed, Great! But when I take a look at the painted window, uh, the end of the string just passes the middle of the window! I have tried lots of DC, e.g., wxDC, wxScreenDC, wxPainterDC and wxWindow::GetTextExtent, but no satisfying outcome emerged.
Any suggestion on these would be highly appreciated.
Re: 请问如何获得含有中文的wxString的字符数量呢?
How exactly do you do that, for each character individually and then add them up? That doesn't work, the width of a single character can be smaller if it's inside a word (at least for western text, i don't know if Chinese characters do the same).When the string of increasing X dimension (obtained by GetTextExtent method)
https://en.wikipedia.org/wiki/Kerning
Use the source, Luke!
Re: 请问如何获得含有中文的wxString的字符数量呢?
I just make the whole current string as the required parameter, not sum them up. This procedure works pretty well for western text, but not so for Chinese characters. Here is a snapshot of a wrong linefeed.doublemax wrote: ↑Thu Aug 13, 2020 5:28 pmHow exactly do you do that, for each character individually and then add them up? That doesn't work, the width of a single character can be smaller if it's inside a word (at least for western text, i don't know if Chinese characters do the same).When the string of increasing X dimension (obtained by GetTextExtent method)
https://en.wikipedia.org/wiki/Kerning
Re: 请问如何获得含有中文的wxString的字符数量呢?
Sorry, no idea. Can you post some compilable code to make some tests with?
Use the source, Luke!
Re: 请问如何获得含有中文的wxString的字符数量呢?
I received your email. Please post some compilable code that i can use for testing this myself.
Use the source, Luke!