本文转自:http://blog.csdn.net/hgy413/article/details/6758834
1.犯了一个错误:
Brush(RGB(255,0,0));这样写程序不会报错,然后很悲剧的怎么也显示不出文字,记录下,以后不要再犯了。
自己写的示例代码:
- CPaintDC dc(this);
- Graphics g(dc.GetSafeHdc());
- //---------------------------------------
- // fontFamily第一个参数是用来定义
- // 宋体、楷体、仿宋、黑体、隶书以及幼圆等多种字样
- // 特别说明:Gdi+不支持OpenType字体,吃了一次亏了
- //---------------------------------------
- FontFamily fontFamily(L"幼圆");
- //---------------------------------------
- // font四个参数,第二个:字体高度,单位由第四个决定
- // 第三个:字体风格:
- // FontStyleRegular(正常)、
- // FontStyleBold(加粗)、
- // FontStyleItalic(斜体)、
- // FontStyleBoldItalic (粗斜体)、
- // FontStyleUnderline(下划线)
- // FontStyleStrikeout(删除线)
- // 第四个:UnitDisplay(1/75英寸)、
- // UnitPixel(像素)、
- // UnitPoint(点)、一点约折合为1/72英寸
- // UnitInch(英寸)、
- // UnitDocument(1/300英寸)、
- // UnitMillimeter(毫米)
- //---------------------------------------
- Gdiplus::Font font(&fontFamily, 12, FontStyleRegular, UnitPixel);
- //---------------------------------------
- // 这是为了和以前的GDI字体
- // font两个参数:
- // HDC, LOGFONT
- //---------------------------------------
- LOGFONT lf;
- ::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT), sizeof(lf), &lf);
- memcpy(lf.lfFaceName, "Arial", 5) ;
- lf.lfHeight = 13 ;
- Gdiplus::Font Gdi_font(dc.GetSafeHdc(), &lf);
- //-----------------------------------------
- //stringformat主要用以下的函数来控制文本的显示方式
- // SetAlignment
- //-----------------------------------------
- StringFormat stringformat;
- //-----------------------------------------
- // StringAlignmentNear(左对齐或右对齐,取决于书写方向是从左到右还是从右到左)、
- // StringAlignmentCenter(水平居中)
- // StringAlignmentFar(两端对齐)。
- // 默认都不影响垂直
- //-----------------------------------------
- stringformat.SetAlignment(StringAlignmentCenter);
- //-----------------------------------------
- // 和SetAlignment对应,为竖直的设置
- //-----------------------------------------
- stringformat.SetLineAlignment(StringAlignmentCenter);
- //------------------------------------------
- // SolidBrush
- // HatchBrush(阴影画刷)
- // TextureBrush(纹理画刷):使用图像来填充形状的内部。无法继承此类
- // LinearGradientBrush(渐变画刷)
- // 注意,用的是Color初始化,不要用RGB初始化,不会报错,但绘不出来!
- //------------------------------------------
- SolidBrush brush(Color(255, 255, 0, 0));
- CRect rcClient ;
- GetClientRect(rcClient);
- //------------------------------------------
- // SetTextRenderingHint控制文本输出的质量
- //------------------------------------------
- g.SetTextRenderingHint(TextRenderingHintAntiAlias);
- //------------------------------------------
- // 一种是point ,一种是rect控制输出
- //------------------------------------------
- g.DrawString(TEXT("1234"), -1, &font,
- RectF(0, 0, rcClient.Width(), rcClient.Height()), &stringformat, &brush);