Requirement: When the font is displayed on the screen, if there are too many characters to be displayed by branch (divided into two lines), the first line and the second line each account for half of the characters.
Because the displayed string is const char * ptext = "...";
It contains Chinese characters, punctuation marks, English letters, numbers, and so on. Chinese characters are represented in two bytes, and others are represented in one byte. If the character is directly separated into two parts based on the length of the character, garbled characters may occur, so it is necessary to convert it into a wide byte character before segmentation.
1 Int Main ()
2 {
3 Const Char * Ptext = " Hello, the Light building listens to the spring rain all night, the Light building listens to the spring rain all night, and the light building listens to the spring rain all night! " ;
4 String Str1, str2;
5
6 Wchar_t * pwidestr = NULL;
7 Char * Pmultistr1 = NULL;
8 Char * Pmultistr2 = NULL;
9
10 Do
11 {
12 // Param @ 5 -- point to the buffer that receives the converted string
13 // Param @ 6 -- specify the number of characters in the buffer zone directed by 5th parameters.
14 // If the value is zero, the function returns the required number of wide characters in the buffer. If the value is 5th, null is not required.
15 DWORD dwnum = multibytetowidechar (cp_acp, 0 , Ptext ,- 1 , Null, 0 );
16 Pwidestr =New Wchar_t [dwnum];
17 If (! Pwidestr)
18 Break ;
19
20 Multibytetowidechar (cp_acp, 0 , Ptext ,- 1 , Pwidestr, dwnum );
21
22 Wstring wstr (pwidestr );
23 Wstring wstr1 = wstr. substr ( 0 , Wstr. Size ()/ 2 );
24 Wstring wstr2 = wstr. substr (wstr. Size ()/ 2 , Wstr. Size ()-wstr. Size ()/ 2 );
25
26 // Param @ 3 -- point to the Unicode string to be converted.
27 // Param @ 4 -- specify the number of characters in the buffer zone directed by 3rd parameters. If the value is-1,
28 // The string is set to a string with null as the Terminator and automatically calculates the length.
29 // Param @ 5 -- point to the buffer zone that receives the converted string.
30 // Param @ 6 -- specify the maximum value of the buffer to which the parameter lpmultibytestr points (measured in bytes ).
31 // If the value is zero, the function returns the number of bytes required for the target buffer to which lpmultibytestr points,
32 // In this case, the 5th parameters are usually null.
33 Dwnum = widechartomultibyte (cp_oemcp, null, wstr1.c _ STR (),- 1 , Null, 0 , Null, false );
34 Pmultistr1 = New Char [Dwnum];
35 If (! Pmultistr1)
36 Break ;
37 Widechartomultibyte (cp_oemcp, null, wstr1.c _ STR (),- 1 , Pmultistr1, dwnum, null, false );
38 Str1 = pmultistr1;
39
40 Dwnum = widechartomultibyte (cp_oemcp, null, wstr2.c _ STR (),- 1 , Null, 0 , Null, false );
41 Pmultistr2 = New Char [Dwnum];
42 If (! Pmultistr2)
43 Break ;
44 Widechartomultibyte (cp_oemcp, null, wstr2.c _ STR (),- 1 , Pmultistr2, dwnum, null, false );
45 Str2 = pmultistr2;
46
47 } While ( 0 );
48
49 If (Pwidestr)
50 Delete [] pwidestr;
51 If (Pmultistr1)
52 Delete [] pmultistr1;
53 If (Pmultistr2)
54 Delete [] pmultistr2;
55
56 Cout <ptext <Endl;
57 Cout <str1 <Endl;
58 Cout <str2 <Endl;
59
60 Return 0 ;
61 }