• 两种方式

    • 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);
}
}

WinForm中的重绘 - 文本的重绘的更多相关文章

  1. C#WinForm中复制、粘贴文本到剪贴板

    //复制: private void button1_Click(object sender, System.EventArgs e) {   if(textBox1.SelectedText != ...

  2. C# WinForm 中Label自动换行 解决方法

    在TableLayoutPannel中放着一些Label如果把Label的AutoSize属性设成True的话,文字超过label长度时就会自动增加,直到后面的字出窗体以外设置成False时,一旦到达 ...

  3. c中的可重入和不可重入函数

    可重入和不可重入 的基本概念 ---简介--- 可重入函数主要用于多任务环境中,一个可重入的函数简单来说就是可以被中断的函数,也就是说,可以在这个函数执行的任何时刻中断它,转入OS调度下去执行另外一段 ...

  4. Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock)

    Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在学习Java的之前,你可能已经听说过读 ...

  5. simhash进行文本查重 Simhash算法原理和网页查重应用

    simhash进行文本查重http://blog.csdn.net/lgnlgn/article/details/6008498 Simhash算法原理和网页查重应用http://blog.jobbo ...

  6. (原创)IconFont(矢量图标字体)在Winform中的应用

    一.前言 很多时候,使用矢量图形可以带来非常美观的界面效果,比如SVG的使用.但是Winform原生是不支持显示SVG图像的,所以退而求其次,可以使用IconFont来实现相似的矢量效果. 先来个图解 ...

  7. C#在WinForm中重写ProgressBar控件(带%的显示)

    废话少说,直接上码: namespace csPublish { [ToolboxItem(true)] class textProgressBar : System.Windows.Forms.Pr ...

  8. C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率)

    C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率)   窗体缩放是一个困扰我多时的问题,为了解决这个问题,我从网上找了很多相关的资料,很多人说用Anchor和Dock属性,但是我试了 ...

  9. C#winform中ListView的使用

    使用ListView模仿Windows系统的资源管理器界面,实现文件(夹)的浏览.重命名.删除及查询等功能,主要功能界面展示如下: 1.MainForm.cs及MainForm.Designer.cs ...

随机推荐

  1. 常用FTP命令 1. 连接ftp服务器

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  2. HTTP之Web服务器

    一台 Web 服务器可搭建多个独立域名的 Web 网站,也可作为通信路径上的中转服务器提升传输效率. HTTP 报文首部 在报文众多的字段当中,HTTP 首部字段包含的信息最为丰富.首部字段同时存在于 ...

  3. 解析Java反射 - invoke方法

    最近工作中涉及到获取同程火车票,大概描述为:将本地获取的发出城市,目的城市及出发时间按固定格式封装,调用接口获取可乘坐座席等级最高的火车票,接口返回数据用包含三层类封装的类接受,接受的类总共为四层,倒 ...

  4. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 8_Neural Networks Representation 神经网络的表述

    神经网络是一种受大脑工作原理启发的模式. 它在许多应用中广泛使用:当您的手机解释并理解您的语音命令时,很可能是神经网络正在帮助理解您的语音; 当您兑现支票时,自动读取数字的机器也使用神经网络. 8.1 ...

  5. Oracle字符集的查看查询和Oracle字符集的设置修改(转)

    最近郁闷的字符集2014年7月31日16:32:58 本文主要讨论以下几个部分:如何查看查询oracle字符集. 修改设置字符集以及常见的oracle utf8字符集和oracle exp 字符集问题 ...

  6. Base -快捷键|通配符|特殊符号|输出(正确与错误的保存)

    curl + a   移动光标到行首. curl +e    移动光标到行尾. curl +k    剪切光标所在位置到行末的字符. curl+u    剪切光标所在位置到行首的字符. curl +y ...

  7. 453D Little Pony and Elements of Harmony

    传送门 分析 我们可以将所有的b[i^j]直接对应到b[f(i^j)]上 于是显然可以fwt 我们对b进行t次fwt之后直接将答案与e0卷起来即可 注意由于模数不确定,我们可以将模数扩大$2^m$然后 ...

  8. 对于 yii2 高级模板 生成文件入口

    安装的 advanced 模板web下是没有index.php 方法: 在advanced 目录下有个init.bat 应用程序  双击即可如下 查看advanced 目录 (刷新)如下 已有:

  9. Django JSON-RPC

    Django JSON-RPC https://github.com/samuraisam/django-json-rpc =============== A basic JSON-RPC Imple ...

  10. 跨域问题hbuilder

    1.借助jquery-jsonp插件 $.jsonp({ url: url, data: { 'name': usd, 'passwd': pass }, callbackParameter: &qu ...