• 两种方式

    • TextRenderer.DrawText

      • 注意:默认在每次绘制的文本左右有padding,即使参数中设置了TextFormatFlags.NoPadding也是一样,因此在分段绘制文本时(比如绘制搜索结果文本中高亮一部分时),每次绘制前在定位传递Point参数时,需要进行修正,减去相应个数的padding的宽度(由于需要转成int,所以会有误差)

        • https://theartofdev.com/2013/08/12/the-wonders-of-text-rendering-and-gdi/
        • Introduced in .NET 2.0 TextRenderer is the recommended text rendering method, using GDI library under the hood it provided the best looking text, accurate measurement and proper international support.
          Using TextRenderer.DrawText and TextRenderer.MeasureText is straightforward and well documented so I won't go into details. The only annoying issue is the padding added to the left (1/6 em) and right (1/4 em) of the drawn/measured text as described here. To have exact draw/measure of text you need the subtract (1/6font.GetHeight()) from left corner during draw and (2.5/6font.GetHeight()) from the measured width (Figure 2).
    • e.Graphics.DrawString
         private void smartTipListBox_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox eObj = sender as ListBox; e.DrawBackground(); string entireText = ((EntireInfo)eObj.Items[e.Index]).VarName; int index = entireText.IndexOf(searchingStr, StringComparison.InvariantCultureIgnoreCase);
if (index >= )
{
TextFormatFlags formatFlags = TextFormatFlags.NoPadding | TextFormatFlags.SingleLine;
//TextFormatFlags formatFlags = TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak;
int leftAndTopPadding = ;
string headText = entireText.Substring(, index);
string matchedText = entireText.Substring(index, searchingStr.Length);
string tailText = entireText.Substring(index + matchedText.Length, entireText.Length - index - matchedText.Length);
int headTextPartWidth = TextRenderer.MeasureText(headText, e.Font, new Size(int.MaxValue, int.MaxValue), formatFlags).Width;
int highlightedTextWidth = TextRenderer.MeasureText(matchedText, e.Font, new Size(int.MaxValue, int.MaxValue), formatFlags).Width;
int yPosition = e.Index * smartTipListBox.ItemHeight + leftAndTopPadding;
int leftTextMargin = (int)( * e.Font.GetHeight() / );
int rightTextMargin = (int)( * e.Font.GetHeight() / );
if (index > )
{
TextRenderer.DrawText(e.Graphics, headText, e.Font, new Point(leftAndTopPadding, yPosition), Color.Black, formatFlags);
TextRenderer.DrawText(e.Graphics, matchedText, e.Font, new Point(leftAndTopPadding + headTextPartWidth - * leftTextMargin - rightTextMargin, yPosition), Color.Red, formatFlags);
TextRenderer.DrawText(e.Graphics, tailText, e.Font, new Point(leftAndTopPadding + headTextPartWidth + highlightedTextWidth - * leftTextMargin - * rightTextMargin, yPosition), Color.Black, formatFlags);
}
else
{
TextRenderer.DrawText(e.Graphics, matchedText, e.Font, new Point(leftAndTopPadding + headTextPartWidth, yPosition), Color.Red, formatFlags);
TextRenderer.DrawText(e.Graphics, tailText, e.Font, new Point(leftAndTopPadding + headTextPartWidth + highlightedTextWidth - * leftTextMargin - rightTextMargin, yPosition), Color.Black, formatFlags);
} //Brush blackBrush = Brushes.Black;
//Brush redBrush = Brushes.Red;
//Rectangle initRectangle = e.Bounds;
//e.Graphics.DrawString(entireText.Substring(0, index), e.Font, blackBrush, initRectangle, null);
//initRectangle.Offset(Convert.ToInt32(e.Graphics.MeasureString(entireText.Substring(0, index), e.Font).Width), 0);
//e.Graphics.DrawString(entireText.Substring(index, searchingStr.Length), e.Font, redBrush, initRectangle, null);
//initRectangle.Offset(Convert.ToInt32(e.Graphics.MeasureString(entireText.Substring(index, searchingStr.Length), e.Font).Width), 0);
//e.Graphics.DrawString(entireText.Substring(index + searchingStr.Length, entireText.Length - index - searchingStr.Length), e.Font, blackBrush, initRectangle, null);
}
}

最新文章

  1. css实现页面元素居中
  2. 解决svn pritine text not exist问题
  3. Linux进程间通信(五):信号量 semget()、semop()、semctl()
  4. Winform MDI窗体容器 权限 简单通讯
  5. Bootstrap学习笔记系列6-----Bootstrap文本显示
  6. Android配置文件,所有权限
  7. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
  8. linux下搭建SVN服务器完全手册【摘抄】
  9. 初步探讨WPF的ListView控件(涉及模板、查找子控件)
  10. HTML-day-1-HTML基础知识
  11. .c和.h文件的区别(转载)
  12. linux下查看本机socket端口详细信息
  13. Web---文件上传-用apache的工具处理、打散目录、简单文件上传进度
  14. 查看linux的公网地址
  15. (7) MySQL数据库备份详解
  16. NOIP2018滚粗记
  17. python 全排列combinations和permutations函数
  18. js五星好评2
  19. 基于Ubuntu搭建GMS测试环境
  20. 【Android】Android实现自定义带文字和图片的Button

热门文章

  1. oracle里的统计信息
  2. 如何准确计算Java对象的大小
  3. vc6.0出现“cannot add new member”解决办法
  4. PHP 乘法口诀表
  5. dubbo参数调优
  6. 单机配置tomcat 8 集群
  7. CSS--抽屉(dig.chouti.com)页面
  8. ofbiz
  9. 第十八课 Gazebo仿真器
  10. 简单的Cooki案例——记录用户上次访问该网页的时间