请问如何获得含有中文的wxString的字符数量呢?

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
liebealt
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Jun 20, 2020 3:18 pm

请问如何获得含有中文的wxString的字符数量呢?

Post by liebealt » Wed Aug 12, 2020 5:44 am

比如内容为"abc一二三"的wxString对象,我想得到的字符数量应该为6,而不是length之类的得到的9之类的字节数。

请大家帮忙指点一下,多谢了。

User avatar
doublemax
Moderator
Moderator
Posts: 16398
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: 请问如何获得含有中文的wxString的字符数量呢?

Post by doublemax » Thu Aug 13, 2020 3:08 pm

What platform and wxWidgets version are you using?

When i run this code under Windows:

Code: Select all

wxString s( wxT("abc一二三") );
wxLogMessage("length: %d", s.Length());
It shows "6".
Use the source, Luke!

liebealt
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Jun 20, 2020 3:18 pm

Re: 请问如何获得含有中文的wxString的字符数量呢?

Post by liebealt » Thu Aug 13, 2020 4:57 pm

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.

User avatar
doublemax
Moderator
Moderator
Posts: 16398
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: 请问如何获得含有中文的wxString的字符数量呢?

Post by doublemax » Thu Aug 13, 2020 5:28 pm

When the string of increasing X dimension (obtained by GetTextExtent method)
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).
https://en.wikipedia.org/wiki/Kerning
Use the source, Luke!

liebealt
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Jun 20, 2020 3:18 pm

Re: 请问如何获得含有中文的wxString的字符数量呢?

Post by liebealt » Thu Aug 13, 2020 6:14 pm

doublemax wrote:
Thu Aug 13, 2020 5:28 pm
When the string of increasing X dimension (obtained by GetTextExtent method)
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).
https://en.wikipedia.org/wiki/Kerning
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.
1.png
1.png (9.62 KiB) Viewed 4647 times

User avatar
doublemax
Moderator
Moderator
Posts: 16398
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: 请问如何获得含有中文的wxString的字符数量呢?

Post by doublemax » Thu Aug 13, 2020 7:12 pm

Sorry, no idea. Can you post some compilable code to make some tests with?
Use the source, Luke!

User avatar
doublemax
Moderator
Moderator
Posts: 16398
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: 请问如何获得含有中文的wxString的字符数量呢?

Post by doublemax » Thu Aug 13, 2020 10:36 pm

I received your email. Please post some compilable code that i can use for testing this myself.
Use the source, Luke!

Post Reply