• 两种方式

    • 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
  1. private void smartTipListBox_DrawItem(object sender, DrawItemEventArgs e)
  2. {
  3. ListBox eObj = sender as ListBox;
  4.  
  5. e.DrawBackground();
  6.  
  7. string entireText = ((EntireInfo)eObj.Items[e.Index]).VarName;
  8.  
  9. int index = entireText.IndexOf(searchingStr, StringComparison.InvariantCultureIgnoreCase);
  10. if (index >= )
  11. {
  12. TextFormatFlags formatFlags = TextFormatFlags.NoPadding | TextFormatFlags.SingleLine;
  13. //TextFormatFlags formatFlags = TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak;
  14. int leftAndTopPadding = ;
  15. string headText = entireText.Substring(, index);
  16. string matchedText = entireText.Substring(index, searchingStr.Length);
  17. string tailText = entireText.Substring(index + matchedText.Length, entireText.Length - index - matchedText.Length);
  18. int headTextPartWidth = TextRenderer.MeasureText(headText, e.Font, new Size(int.MaxValue, int.MaxValue), formatFlags).Width;
  19. int highlightedTextWidth = TextRenderer.MeasureText(matchedText, e.Font, new Size(int.MaxValue, int.MaxValue), formatFlags).Width;
  20. int yPosition = e.Index * smartTipListBox.ItemHeight + leftAndTopPadding;
  21. int leftTextMargin = (int)( * e.Font.GetHeight() / );
  22. int rightTextMargin = (int)( * e.Font.GetHeight() / );
  23. if (index > )
  24. {
  25. TextRenderer.DrawText(e.Graphics, headText, e.Font, new Point(leftAndTopPadding, yPosition), Color.Black, formatFlags);
  26. TextRenderer.DrawText(e.Graphics, matchedText, e.Font, new Point(leftAndTopPadding + headTextPartWidth - * leftTextMargin - rightTextMargin, yPosition), Color.Red, formatFlags);
  27. TextRenderer.DrawText(e.Graphics, tailText, e.Font, new Point(leftAndTopPadding + headTextPartWidth + highlightedTextWidth - * leftTextMargin - * rightTextMargin, yPosition), Color.Black, formatFlags);
  28. }
  29. else
  30. {
  31. TextRenderer.DrawText(e.Graphics, matchedText, e.Font, new Point(leftAndTopPadding + headTextPartWidth, yPosition), Color.Red, formatFlags);
  32. TextRenderer.DrawText(e.Graphics, tailText, e.Font, new Point(leftAndTopPadding + headTextPartWidth + highlightedTextWidth - * leftTextMargin - rightTextMargin, yPosition), Color.Black, formatFlags);
  33. }
  34.  
  35. //Brush blackBrush = Brushes.Black;
  36. //Brush redBrush = Brushes.Red;
  37. //Rectangle initRectangle = e.Bounds;
  38. //e.Graphics.DrawString(entireText.Substring(0, index), e.Font, blackBrush, initRectangle, null);
  39. //initRectangle.Offset(Convert.ToInt32(e.Graphics.MeasureString(entireText.Substring(0, index), e.Font).Width), 0);
  40. //e.Graphics.DrawString(entireText.Substring(index, searchingStr.Length), e.Font, redBrush, initRectangle, null);
  41. //initRectangle.Offset(Convert.ToInt32(e.Graphics.MeasureString(entireText.Substring(index, searchingStr.Length), e.Font).Width), 0);
  42. //e.Graphics.DrawString(entireText.Substring(index + searchingStr.Length, entireText.Length - index - searchingStr.Length), e.Font, blackBrush, initRectangle, null);
  43. }
  44. }

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. AngularJS:教程

    ylbtech-AngularJS:教程 1.返回顶部 1. AngularJS 教程 AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 可以构建一个单一页面应用程序(S ...

  2. sublime text3 自动编译php 适合用于简单的php文件执行

    1.将php路径放入环境变量中 2. 点击 sublime_text的“工具”->"编译系统"->"编译新系统" { "cmd" ...

  3. 决策树 ID3 C4.5 CART(未完)

    1.决策树 :监督学习 决策树是一种依托决策而建立起来的一种树. 在机器学习中,决策树是一种预测模型,代表的是一种对象属性与对象值之间的一种映射关系,每一个节点代表某个对象,树中的每一个分叉路径代表某 ...

  4. Halcon学习之文本操作

    1.          新建文本文件open_file ( : : FileName, FileType : FileHandle ) 创建 ( 'output' or 'append' )或者打开  ...

  5. MYSQL中str_to_date函数的用法

    str_to_date(str,format) 函数的用法 str_to_date函数将str转化为日期型的数据,format表示转化后的格式. format参数格式: 常用: %Y  年 %m  月 ...

  6. Reading RxJava Marble Diagrams

    ------>表示一个Observable(承时间推移,由左入右,左边item先发射) ------>上面的图形,表示这个Observable发射的item ------>上的的|( ...

  7. 关闭SublimeText自动更新

    [关闭SublimeText自动更新] 1.找到Preferences -> Settings-User(设置用户) 2.在最后一个花括号结尾(“}”)前添加一句:”update_check&q ...

  8. android:gravity设置居中的问题

    如果设置一个Button的android:gravity="center" android:text="按钮",则是设置了“按钮”两个字在Button中居中显示 ...

  9. java代码优化29个点

    通过java代码规范来优化程序,优化内存使用情况,防止内存泄露 可供程序利用的资源(内存.CPU时间.网络带宽等)是有限的,优化的目的就是让程序用尽可能少的资源完成预定的任务.优化通常包含两方面的内容 ...

  10. linq 条件查询与分页

    <div>姓名:<asp:TextBox ID="T1" runat="server"></asp:TextBox>< ...