GDI:
TextOut() 最低级的文本输出函数,速度最快,没有裁剪,不能带tab(tab键被忽略)
TabbedTextOut() 上面函数的带tab版本
ExtTextOut() TextOut升级版,可以调整字符间距和裁剪矩形
DrawText() TextOut升级版,可以是多行文本,指定一个Rect,会自动换行,可以设置对齐方向
DrawTextEx() drawText升级版,可以指定tab的显示方式
GetTextExtentPoint32() 测量某文本的显示区域
GetTabbedTextExtent() 测量带tab键的字符串的显示区域
GetTextExtentExPoint() 根据某区域测量能显示下的字符个数
GetTextMetrics() 获取字体信息
GetTextFace(hdc,100,cc); 获取设备中字体名
SetTextColor (hdc, rgbColor) 、GetTextColor | 设置文本颜色,默认黑色 |
SetBkMode (hdc, iMode) ; | 设置背景模式OPAQUE or TRANSPARENT,默认OPAQUE不透明 |
SetBkColor (hdc, rgbColor) ; | 设置背景颜色,默认白色 WHITE_BRUSH |
GetStockObject(fnObject) | 获得一些默认的font , pen ,brush,所有的索引名字可以直接使用,如WHITE_BRUSH |
GetSysColor(nIndex) | 都到windows的系统颜色 |
SetTextCharacterExtra(hdc,nCharExtra) | 设置TextOut等函数画的字符的横排间距 |
SelectObject(hdc,hgdiobj) | 将某个属性选入设备环境 |
CreatFontIndirect(lplf) | 创建字体,参数是个结构体 |
CreateFont | 创建字体,有12个参数,不常用,上面的常用 |
画路径字体(例子)
BeginPath (hdc) ; | |
BeginPath (hdc) ; | |
StrokePath (hdc) ; | |
FillPath (hdc) ; | |
FillPath (hdc) ; | |
hRgn = PathToRegion (hdc) ; | |
hRgn = PathToRegion (hdc) ; |
GDI+(GDI+的文本函数比GDI慢很多):
DrawString() 画字符串
MeasureString() 获得字符串的整体显示区域信息
行 |
函数 |
备注 | 实例代码 |
截图 |
1 |
Status MeasureString( [in] const WCHAR *string, [in] INT length, [in] const Font *font, [in, ref] const PointF &origin, [out] RectF *boundingBox ) const; |
用字体和起始点测量字符串的显示区域 | VOID Example_MeasureString4(HDC hdc) { Graphics graphics(hdc); // Set up the string. WCHAR string[] = L"Measure Text"; Font font(L"Arial", 16); PointF origin(0.0f, 0.0f); RectF boundRect; // Measure the string. graphics.DrawString(string,12,&font,origin,&SolidBrush(Color(255,0,0,0))); graphics.MeasureString(string, 12, &font, origin, &boundRect); // Draw a rectangle that represents the size of the string. graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect); } |
|
2 |
Status MeasureString( [in] const WCHAR *string, [in] INT length, [in] const Font *font, [in, ref] const RectF &layoutRect, [out] RectF *boundingBox ) const; |
用字体和限制矩形擦亮字符串的显示区域 1的基础上加了一个矩形区域比较的判断返回更小的 |
VOID Example_MeasureString(HDC hdc) { Graphics graphics(hdc); // Set up the string. WCHAR string[] = L"Measure Text"; Font font(L"Arial", 16); RectF layoutRect(0, 0, 100, 0); RectF boundRect; // Measure the string. graphics.DrawString(string,12,&font,PointF(0,0),&SolidBrush(Color(100,255,0,0))); graphics.DrawString(string,12,&font,RectF(0,0,100,500),&StringFormat(),&SolidBrush(Color(100,0,255,0))); graphics.MeasureString(string, 12, &font, layoutRect, &boundRect); // Draw a rectangle that represents the size of the string. graphics.DrawRectangle(&Pen(Color(255, 255, 0, 0)), boundRect); //高度为0可以自动测量高度 layoutRect = RectF(0, 0, 500, 0); graphics.MeasureString(string, 12, &font, layoutRect, &boundRect); // Draw a rectangle that represents the size of the string. graphics.DrawRectangle(&Pen(Color(255, 0, 255, 0)), boundRect); } |
|
3 |
Status MeasureString( [in] const WCHAR *string, [in] INT length, [in] const Font *font, [in, ref] const PointF &origin, [in] const StringFormat *stringFormat, [out] RectF *boundingBox ) const; |
1的带StringFormat的版本 | VOID Example_MeasureString5(HDC hdc) { Graphics graphics(hdc); // Set up the string. WCHAR string[] = L"Measure Text"; Font font(L"Arial", 16); PointF origin(0.0f, 0.0f); StringFormat format; format.SetAlignment(StringAlignmentCenter); RectF boundRect; // Measure the string. graphics.DrawString(string,12,&font,PointF(0,0),&format,&SolidBrush(Color(100,255,0,0))); graphics.MeasureString(string, 12, &font, origin, &format, &boundRect); // Draw a rectangle that represents the size of the string. graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect); } |
|
4 |
Status MeasureString( [in] const WCHAR *string, [in] INT length, [in] const Font *font, [in, ref] const RectF &layoutRect, [in] const StringFormat *stringFormat, [out] RectF *boundingBox, [out] INT *codepointsFitted, [out] INT *linesFilled ) const; |
2的加强版,可以测量指定区域显示下的字符的个数 | VOID Example_MeasureString2(HDC hdc) { Graphics graphics(hdc); // Set up the string. WCHAR string[] = L"Measure Text"; Font font(L"Arial", 16); RectF layoutRect(0.0f, 0.0f, 70, 50.0f); StringFormat format; format.SetAlignment(StringAlignmentFar); RectF boundRect; // Measure the string. int codepointsFitted; int linesFilled; graphics.DrawString(string,12,&font,layoutRect,&format,&SolidBrush(Color(255,0,0,0))); graphics.MeasureString(string, 12, &font, layoutRect, &format, &boundRect,&codepointsFitted,&linesFilled); // Draw a rectangle that represents the size of the string. graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect); WCHAR *count = new WCHAR[100]; swprintf(count,100 , L"codepointsFitted:%d linesFilled:%d",codepointsFitted,linesFilled); graphics.DrawString(L"字符串Measure Text,长度12",-1,&font,PointF(0,70),&SolidBrush(Color(255,0,0,0))); graphics.DrawString(count,-1,&font,PointF(0,90),&SolidBrush(Color(255,0,0,0))); } |
|
5 |
Status MeasureString( [in] const WCHAR *string, [in] INT length, [in] const Font *font, [in, ref] const SizeF &layoutRectSize, [in] const StringFormat *stringFormat, [out] SizeF *size, [out] INT *codepointsFitted, [out] INT *linesFilled ) const; |
4的RectF换成了SizeF,其他一样,不要位置只要大小,其实位置没有什么用,所以4很少用到 | 同上 | 同上 |
MeasureCharacterRanges() 获得字符串的特定字符的显示位置
Status MeasureCharacterRanges( [in] const WCHAR *string, [in] INT length, [in] const Font *font, [in, ref] const Rectf &layoutRect, [in] const StringFormat *stringFormat, [in] INT regionCount, [out] Region *regions ) const; |
测量字符的显示区域 |
VOID MeasureCharRanges(HDC hdc) { Graphics graphics(hdc); // Brushes and pens used for drawing and painting SolidBrush blueBrush(Color(255, 0, 0, 255)); SolidBrush redBrush(Color(100, 255, 0, 0)); Pen blackPen(Color(255, 0, 0, 0)); // Layout rectangles used for drawing strings RectF layoutRect_A(20.0f, 20.0f, 130.0f, 130.0f); RectF layoutRect_B(160.0f, 20.0f, 165.0f, 130.0f); RectF layoutRect_C(335.0f, 20.0f, 165.0f, 130.0f); // Three different ranges of character positions within the string CharacterRange charRanges[3] = { CharacterRange(3, 5), CharacterRange(15, 2), CharacterRange(30, 15), }; // Font and string format to apply to string when drawing Font myFont(L"Times New Roman", 16.0f); StringFormat strFormat; // Other variables Region* pCharRangeRegions; // pointer to CharacterRange regions short i; // loop counter INT count; // number of character ranges set WCHAR string[] = L"The quick, brown fox easily jumps over the lazy dog."; // Set three ranges of character positions. strFormat.SetMeasurableCharacterRanges(3, charRanges); // Get the number of ranges that have been set, and allocate memory to // store the regions that correspond to the ranges. count = strFormat.GetMeasurableCharacterRangeCount(); pCharRangeRegions = new Region[count]; // Get the regions that correspond to the ranges within the string when // layout rectangle A is used. Then draw the string, and show the regions. graphics.MeasureCharacterRanges(string, -1, &myFont, layoutRect_A, &strFormat, count, pCharRangeRegions); graphics.DrawString(string, -1, &myFont, layoutRect_A, &strFormat, &blueBrush); graphics.DrawRectangle(&blackPen, layoutRect_A); for ( i = 0; i < count; i++) { graphics.FillRegion(&redBrush, pCharRangeRegions + i); } // Get the regions that correspond to the ranges within the string when // layout rectangle B is used. Then draw the string, and show the regions. graphics.MeasureCharacterRanges(string, -1, &myFont, layoutRect_B, &strFormat, count, pCharRangeRegions); graphics.DrawString(string, -1, &myFont, layoutRect_B, &strFormat, &blueBrush); graphics.DrawRectangle(&blackPen, layoutRect_B); for ( i = 0; i < count; i++) { graphics.FillRegion(&redBrush, pCharRangeRegions + i); } // Get the regions that correspond to the ranges within the string when // layout rectangle C is used. Set trailing spaces to be included in the // regions. Then draw the string, and show the regions. strFormat.SetFormatFlags(StringFormatFlagsMeasureTrailingSpaces); graphics.MeasureCharacterRanges(string, -1, &myFont, layoutRect_C, &strFormat, count, pCharRangeRegions); graphics.DrawString(string, -1, &myFont, layoutRect_C, &strFormat, &blueBrush); graphics.DrawRectangle(&blackPen, layoutRect_C); for ( i = 0; i < count; i++) { graphics.FillRegion(&redBrush, pCharRangeRegions + i); } // Delete memory for the range regions. delete [] pCharRangeRegions; }