How to display multi-line text in the specified rectangle box (revision)

zhaozj2021-02-08  265

//// Description (Mapping mode mm_lometric): /// The horizontal or vertical display in the rectangular box ////////// Xu Xi Zhou (jingzhou_xu@163.net) /// / parameter: // PDC: Draw DC //// szString: Draw string ///////////lmode: Arrangement, 0: Horizontal mode; 1: Vertical //// LHORI: Horizontal alignment, 0: Left alignment; 1: Case; 2: Right alignment; 3: Customized //// Lvert: Vertical alignment, 0: Top alignment; 1: Central; 2: Right; 3: Customize /// CRECT DRAWTILEINRECT (CDC * PDC, CSTRING SZSTRING, LPRECT LPRECT, Long LMODE, Long Lhori, long LVERT) {CRECT RCINNER (LPRECT)

IF (rcinner.width () == 0) Return RCINNER;

TextMetricTM; PDC-> GetTextMetrics (& TM); int TmpWidth = Tm.tmavecharWidth, Tmpheight = Tm.TmHeight;

/ / -------------------------------------------------------------------------------------------- ------------------------------------------- // Function: New, old rectangle, recalculate the number of lines, make text multi-line display, Jingzhou XU // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ------------- // Maximum number of characters in a row int nmaxlinechar = abs (lprect-> right - lprect-> left) / tmpWidth; if (nmaxlinechar <2) // should at least display A Chinese character return rcinner;

// Record the width of the current row short thelinength = 0; // Record the number of Chinese characters in the current row to prevent half of the Chinese characters from divided into two lines Unsigned short halfchinese = 0;

For (int i = 0; i <= szstring.getLength () - 1; i ) {IF ((unsigned char) szstring.getat (i) == 0x0d) && ((unsigned char) szstring.getat (i 1) == 0x0a)) ThelineLength = 0;

/ / In this case, the "||" character is a commutline flag character, and the input can be turned off according to this string ((unsigned char) szstring.getat (i) == '|') && (UNSIGNED Char) Szstring.getat (i 1) == '|')) {szstring.set (i, (unsigned char) 0x0d); szstring.sett (i 1, (unsigned char) 0x0a);}

/ / Greater than 0xA1 byte is Chinese character byte IF ((unsigned char) szstring.getat (i)> = 0xa1) HalfChinese ; thelineLength ; // If the line width is greater than the maximum width of each row, special processing IF (ThelineLength> NmaxlineChar ) {/ / Prevent a Chinese character from dividing a Chinese word into two lines, backtracking IF (! ("") && (unsigned char) szstring.getat (i)> = 0xa1) {szstring.insert (i-1, (unsigned char) 0x0a); szstring.insert (i-1, (unsigned char) 0x0d); // Note: This does not add a skip, because it is added at I-1, just jump to At the point, it is only necessary to add once at the loop. } Else {szstring.insert (i, (unsigned char) 0x0a); szstring.insert (i, (unsigned char) 0x0d);

i ; // Skip new newline characters, you should jump to (add once in the loop, so just once this)} thelinength = 0;}}

IF (lmode == 0) // horizontally arranged {RCINNER.LEFT = TmpWidth; rcinner.right- = tmpWidth; RCINNER.TOP- = TmpWidth; RCINNER.BOTTOM = TmpWidth;} if (lmode == 1) // vertical arrangement {RCINNER.LEFT = TmpWidth; rcinner.right = RCINNER.LEFT TMPWIDTH; RCINNER.TOP- = TmpWidth; RCINNER.BOTTOM = TmpWidth;}

// Recalculate the rectangular boundary range PDC-> DrawText (Szstring, RCINNER, DT_WORDBREAK | DT_LEFT | DT_CALCRECT);

Switch (lhori) {casse 0: Break; case 1: {long xoutcent = (lprect-> right lprect-> left) / 2; long xinnenet = (RCINNER.right RCINNER.LEFT) / 2; RCINNER.LEFT = (xoutcent-xinnce); RCINNER.right = (xoutcent-xinnce);} Break; case 2: {long linwidth = rcinner.right-rcinner.LPRECT; RCINNER.right = lpRect-> Right-TmpWidth; Rcinner.LEFT = RCINNER .right-linwidth;} Break; default: Break;} switch (LVERT) {CASE 0: BREAK; Case 1: {long youtcent = (lprect-> bottom lprect-> TOP) / 2; long yinncent = (RCINNER. Bottom RCINNER.TOP) / 2; RCINNER.TOP - = (YINNCENT-YOUTCENT); RCINNER.BOTTOM - = (YINNCENT-YOUTCENT);} Break; case 2: {long linheigh = rcinner.top-rcinner.bottom; RCinner .bottom = lprect-> Bottom TmpWidth; RCINNER.TOP = RCINNER.BOTTOM Linheigh;} Break; default: Break;}} (RCINNER.BOTTOM bottom) RCINNER.BOTTOM = lpRect-> bottom; ing RCINNER.TOP> LPRECT-> TOP) RCINNER.TOP = LPRECT-> TOP;

/ / -------------------------------------------------------------------------------------------- ---------------------------------------------

IF (lhori == 0) PDC-> DRAWText (Szstring, Rcinner, DT_WordBreak | DT_LEFT); Else if (Lhori == 1) PDC-> DrawText (Szstring, RCINNER, DT_WORDBREAK | DT_CENTER); Else IF (lhori == 2 PDC-> DrawText (Szstring, RCINNER, DT_WORDBREAK | DT_RIGHT);

Return Rcinner;

转载请注明原文地址:https://www.9cbs.com/read-2166.html

New Post(0)