比如“上山打老虎”。
中間有可能換行,比如
“上山打老虎,下
地挖紅薯”。
用gdi+畫了下,效果不太好。大家有啥建議么
uj5u.com熱心網友回復:
CRichEditCtrl SetSelectionCharFormatuj5u.com熱心網友回復:
多謝回復,這是個方式。如果通過HDC處理的話,有直接能繪制的方式么
uj5u.com熱心網友回復:
每個字 都配顏色,逐字 輸出。uj5u.com熱心網友回復:
逐字 輸出可以,得計算每個字符的位置等資訊uj5u.com熱心網友回復:
不必“得計算每個字符的位置”但要 改變 Pen
uj5u.com熱心網友回復:
如果簡單的顯示,使用普通視窗,通過DC輸出,輸出前使用SetTextColor設定文本顏色(必須使用DC_PEN)如果要復雜顯示,并且可以編輯,則使用CRichCtrl,內容中包含著顯示格式的文本控制引數
uj5u.com熱心網友回復:
改變pen沒問題,如何做呢,比如這樣的
“上山打老虎,下
地挖紅薯”
uj5u.com熱心網友回復:
比如“下地”兩字的時候,跨行了,繪制的RECT等如何處理uj5u.com熱心網友回復:
如果是漢字,寬和高 是一樣的,只要算一次uj5u.com熱心網友回復:
假如字串為“上山打老虎,下地挖紅薯”
視窗寬度為7個字符
1. 根據視窗寬度分隔字串
字串變為
字串1: “上山打老虎,下“
字串2: ”地挖紅薯“
2. 根據顏色再次分割字串
字串變為:
字串1-1: “上山”
字串1-2: “打老虎,”
字串1-3: “下”
字串2-1: “地”
字串2-2: ”挖紅薯“
3. 按順序依次描畫字串
uj5u.com熱心網友回復:
恩,明白樓上的意思,多謝uj5u.com熱心網友回復:
試試 單行的:
void StepText(HDC hdc,RECT *rc,char *str)//文字逐字顯示
{
int len=strlen(str);//計算字串的長度
int posx;//當前字符位置
int posy=rc->top;
SIZE size;//存盤每個字符的寬高
SetTextAlign(hdc,TA_UPDATECP|TA_LEFT|TA_TOP);//設定自動更新文字位置
while(len>0)
{
posx=rc->left;
MoveToEx(hdc,rc->left, posy,NULL);//移動到行首
while(len>=0)
{
int t;
if((str[0])<0) t=2;//中西字符辨別
else t=1;
GetTextExtentPoint32(hdc,str,t,&size);//獲取字符寬高
if(posx+size.cx>rc->right) break;//若超出邊界
posx+=size.cx;//更新當前位置
TextOut(hdc,0,0,str,t);//輸出字符
str+=t;//更新字符
len-=t;//更新字符長度
if(len<=0) break;//若字符輸出完畢
}
posy+=size.cy; //進行移行處理
if((posy+size.cy+ size.cy)>=rc->bottom)//下一行無法書寫,進行滾屏
{
RECT rect=*rc;
rect.top+=size.cy;//滾屏位置
ScrollDC(hdc,0,-size.cy,&rect,NULL,NULL,NULL);
posy-=size.cy; //恢復移行處理
}
}
}
uj5u.com熱心網友回復:
注意 SetTextAlign(hdc,TA_UPDATECP|TA_LEFT|TA_TOP);//設定自動更新文字位置及 TextOut(hdc,0,0,str,t);//輸出字符
uj5u.com熱心網友回復:
多行 帶顏色
// StepText(hdc,&rect,"haha 你好!\r\nhaha 你也好!\r\n\r\nhaha 他好!\r\nhaha 她也好!");
void StepText(HDC hdc,RECT *rc,char *str)//文字逐字顯示
{
int len=strlen(str);//計算字串的長度
int posx;//當前字符位置
int posy=rc->top;
SIZE size;//存盤每個字符的寬高
SetTextAlign(hdc,TA_UPDATECP|TA_LEFT|TA_TOP);//設定自動更新文字位置
while(len>0)
{
posx=rc->left;
MoveToEx(hdc,rc->left, posy,NULL);//移動到行首
while(len>=0)
{
int t;
if((str[0])<0) {t=2;SetTextColor(hdc,255);}//中西字符辨別
else {t=1;SetTextColor(hdc,0);}
GetTextExtentPoint32(hdc,str,t,&size);//獲取字符寬高
if(posx+size.cx>rc->right) break;//若超出邊界
posx+=size.cx;//更新當前位置
if(*str==0x0D){ str+=1; continue;}
if(*str==0x0A){ str+=1; len=strlen(str); break;}
TextOut(hdc,0,0,str,t);//輸出字符
str+=t;//更新字符
len-=t;//更新字符長度
if(len<=0) break;//若字符輸出完畢
} // end while(len>=0)
// newline:
posy+=size.cy; //進行移行處理
if((posy+size.cy+ size.cy)>=rc->bottom)//下一行無法書寫,進行滾屏
{
RECT rect=*rc;
rect.top+=size.cy;//滾屏位置
ScrollDC(hdc,0,-size.cy,&rect,NULL,NULL,NULL);
posy-=size.cy; //恢復移行處理
}
}
}
uj5u.com熱心網友回復:
DrawTextThe DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method (expanding tabs, justifying characters, breaking lines, and so forth).
int DrawText(
HDC hDC, // handle to device context
LPCTSTR lpString, // pointer to string to draw
int nCount, // string length, in characters
LPRECT lpRect, // pointer to struct with formatting dimensions
UINT uFormat // text-drawing flags
);
Parameters
hDC
Handle to the device context.
lpString
Pointer to the string to be drawn. If the nCount parameter is –1, the string must be null-terminated.
If uFormat includes DT_MODIFYSTRING, the function could add up to four additional characters to this string. The buffer containing the string should be large enough to accommodate these extra characters.
nCount
Specifies the number of characters in the string. If nCount is –1, then the lpString parameter is assumed to be a pointer to a null-terminated string and DrawText computes the character count automatically.
lpRect
Pointer to a RECT structure that contains the rectangle (in logical coordinates) in which the text is to be formatted.
uFormat
Specifies the method of formatting the text. It can be any combination of the following values: Value Description
DT_BOTTOM Justifies the text to the bottom of the rectangle. This value must be combined with DT_SINGLELINE.
DT_CALCRECT Determines the width and height of the rectangle. If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text.
DT_CENTER Centers text horizontally in the rectangle.
DT_EDITCONTROL Duplicates the text-displaying characteristics of a multiline edit control. Specifically, the average character width is calculated in the same manner as for an edit control, and the function does not display a partially visible last line.
DT_END_ELLIPSIS or DT_PATH_ELLIPSIS Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified.
You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash.
DT_EXPANDTABS Expands tab characters. The default number of characters per tab is eight.
DT_EXTERNALLEADING Includes the font external leading in line height. Normally, external leading is not included in the height of a line of text.
DT_INTERNAL Uses the system font to calculate text metrics.
DT_LEFT Aligns text to the left.
DT_MODIFYSTRING Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified.
DT_NOCLIP Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used.
DT_NOPREFIX Turns off processing of prefix characters. Normally, DrawText interprets the mnemonic-prefix character & as a directive to underscore the character that follows, and the mnemonic-prefix characters && as a directive to print a single &. By specifying DT_NOPREFIX, this processing is turned off.
DT_RIGHT Aligns text to the right.
DT_RTLREADING Layout in right to left reading order for bi-directional text when the font selected into the hdc is a Hebrew or Arabic font. The default reading order for all text is left to right.
DT_SINGLELINE Displays text on a single line only. Carriage returns and linefeeds do not break the line.
DT_TABSTOP Sets tab stops. Bits 15–8 (high-order byte of the low-order word) of the uFormat parameter specify the number of characters for each tab. The default number of characters per tab is eight.
DT_TOP Top-justifies text (single line only).
DT_VCENTER Centers text vertically (single line only).
DT_WORDBREAK Breaks words. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by the lpRect parameter. A carriage return-linefeed sequence also breaks the line.
DT_WORD_ELLIPSIS Truncates text that does not fit in the rectangle and adds ellipses.
Note The DT_CALCRECT, DT_EXTERNALLEADING, DT_INTERNAL, DT_NOCLIP, and DT_NOPREFIX values cannot be used with the DT_TABSTOP value.
Return Values
If the function succeeds, the return value is the height of the text.
If the function fails, the return value is zero.
Windows NT: To get extended error information, callGetLastError.
Remarks
The DrawText function uses the device context's selected font, text color, and background color to draw the text. Unless the DT_NOCLIP format is used, DrawText clips the text so that it does not appear outside the specified rectangle. All formatting is assumed to have multiple lines unless the DT_SINGLELINE format is specified.
If the selected font is too large for the specified rectangle, the DrawText function does not attempt to substitute a smaller font.
Windows CE: If DT_CALCRECT is specified for the uFormat parameter, you must set the right and bottom members of the RECT structure pointed to by lpRect.
Windows CE does not support the DT_EXTERNALLEADING flag for the uFormat parameter.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.
See Also
Fonts and Text Overview, Font and Text Functions, GrayString, TabbedTextOut, TextOut, RECT
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/96789.html
標籤:基礎類
上一篇:求助:vC++2015出錯:編號的預期結尾后有多余文本
下一篇:IAccessible介面獲取到QQ聊天視窗的輸入控制元件,為什么用put_accvalue無法輸入?感激不盡!
