当前位置: 首页 > article >正文

GDI+ 文本测量方法

背景

图形类 Graphics 是 GDI+的核心,它提供绘制图形、图像和文本的各种方法。Graphics 中使用 DrawString 方法在指定位置绘制文本或者在一个指定矩形内绘制文本。

通过调用 Graphics 对象的 MeasureString 方法,可以对文本的绘制区域进行计算。

计算文本所需矩形范围

void DemoGDI::DrawUser(HDC hdc)
{Graphics graphics(hdc);//构造 Graphics 对象SolidBrush solidBrush(Color::Blue);Font font(L"宋体", 24, FontStyleRegular, UnitPixel);QString m_txt = "在Qt中使用GD+示例";graphics.DrawString(m_txt.toStdWString().c_str(), -1, &font, PointF(20, 20), nullptr, &solidBrush);Pen m_pen(Color::Black);RectF boundRect;graphics.MeasureString(m_txt.toStdWString().c_str(), -1, &font, PointF(20, 20), &boundRect);graphics.DrawRectangle(&m_pen, boundRect);}

在这里插入图片描述

指定区域宽度计算所需高度

void DemoGDI::DrawUser(HDC hdc)
{Graphics graphics(hdc);//构造 Graphics 对象SolidBrush solidBrush(Color::Blue);Font font(L"宋体", 24, FontStyleRegular, UnitPixel);QString m_txt = "在Qt中使用GD+示例";RectF layoutRect(20, 20, 90, 0);//高度设置为0Pen m_pen(Color::Black);RectF boundRect;graphics.MeasureString(m_txt.toStdWString().c_str(), -1, &font, layoutRect, &boundRect);graphics.DrawRectangle(&m_pen, boundRect);graphics.DrawString(m_txt.toStdWString().c_str(), -1, &font, boundRect, nullptr, &solidBrush);}

在这里插入图片描述

通过路径计算所需矩形范围

void DemoGDI::DrawUser(HDC hdc)
{Graphics graphics(hdc);//构造 Graphics 对象SolidBrush solidBrush(Color::Blue);Font font(L"宋体", 24, FontStyleRegular, UnitPixel);QString m_txt = "在Qt中使用GD+示例";graphics.DrawString(m_txt.toStdWString().c_str(), -1, &font, PointF(20, 20), nullptr, &solidBrush);Pen m_pen(Color::Black);RectF boundRect;Gdiplus::GraphicsPath graphicsPath;FontFamily fontFamily(L"宋体");graphicsPath.AddString(m_txt.toStdWString().c_str(), -1, &fontFamily, font.GetStyle(), font.GetSize(), PointF(20, 20), nullptr);  graphicsPath.GetBounds(&boundRect);graphics.DrawRectangle(&m_pen, boundRect);
}

在这里插入图片描述

在这里插入图片描述

http://www.lryc.cn/news/2419183.html

相关文章:

  • DLNA介绍(包括UPnP)
  • 对38位互联网大佬奇葩癖好的奇葩解读
  • 心脏滴血漏洞利用(CVE-2014-0160)
  • 【千峰】网络安全学习笔记(一)
  • HTML5学习(一)
  • 用Java实现贪吃蛇
  • 一、echarts地图全国地图进省地图
  • Access入门简单教程
  • 高分一号(GF-1)卫星影像数据全色和多光谱信息介绍
  • Linux 服务器建站新手教程 (宝塔建站全流程)- 不需要敲一行命令 (记录啊、好东西)(1)
  • AspNetPager 几种常见样式
  • 海外idc有哪些?海外IDC服务商推荐
  • LSF作业管理系统简易使用指南
  • 上证指数的计算公式
  • Virtools
  • 男生诡异世界观,是不是跟你想的不一样~
  • MS-DOS系统的操作命令
  • Windows下usb接口驱动技术
  • filtic函数详解(附3个实例)
  • mfc中CImageList的使用
  • 禁用window安全_如何在Windows中禁用锁定工作站功能(Window + L)
  • 帝国CMS7.5基于迅搜(Xunsearch)的全文搜索插件
  • 网页设计必备!亲测推荐的15款工具,让你事半功倍!
  • 软件 易用性理解
  • VMware安装centos8全步骤
  • golang time strings常用方法
  • 技术网站网址
  • 《智能家居设计与未来的深入探讨》
  • c++(八) string
  • Java下载安装及环境配置