WinForm中的重绘 - 文本的重绘
- 两种方式
- 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).
- 注意:默认在每次绘制的文本左右有padding,即使参数中设置了TextFormatFlags.NoPadding也是一样,因此在分段绘制文本时(比如绘制搜索结果文本中高亮一部分时),每次绘制前在定位传递Point参数时,需要进行修正,减去相应个数的padding的宽度(由于需要转成int,所以会有误差)
- e.Graphics.DrawString
- TextRenderer.DrawText
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中的重绘 - 文本的重绘的更多相关文章
- C#WinForm中复制、粘贴文本到剪贴板
//复制: private void button1_Click(object sender, System.EventArgs e) { if(textBox1.SelectedText != ...
- C# WinForm 中Label自动换行 解决方法
在TableLayoutPannel中放着一些Label如果把Label的AutoSize属性设成True的话,文字超过label长度时就会自动增加,直到后面的字出窗体以外设置成False时,一旦到达 ...
- c中的可重入和不可重入函数
可重入和不可重入 的基本概念 ---简介--- 可重入函数主要用于多任务环境中,一个可重入的函数简单来说就是可以被中断的函数,也就是说,可以在这个函数执行的任何时刻中断它,转入OS调度下去执行另外一段 ...
- Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock)
Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在学习Java的之前,你可能已经听说过读 ...
- simhash进行文本查重 Simhash算法原理和网页查重应用
simhash进行文本查重http://blog.csdn.net/lgnlgn/article/details/6008498 Simhash算法原理和网页查重应用http://blog.jobbo ...
- (原创)IconFont(矢量图标字体)在Winform中的应用
一.前言 很多时候,使用矢量图形可以带来非常美观的界面效果,比如SVG的使用.但是Winform原生是不支持显示SVG图像的,所以退而求其次,可以使用IconFont来实现相似的矢量效果. 先来个图解 ...
- C#在WinForm中重写ProgressBar控件(带%的显示)
废话少说,直接上码: namespace csPublish { [ToolboxItem(true)] class textProgressBar : System.Windows.Forms.Pr ...
- C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率)
C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率) 窗体缩放是一个困扰我多时的问题,为了解决这个问题,我从网上找了很多相关的资料,很多人说用Anchor和Dock属性,但是我试了 ...
- C#winform中ListView的使用
使用ListView模仿Windows系统的资源管理器界面,实现文件(夹)的浏览.重命名.删除及查询等功能,主要功能界面展示如下: 1.MainForm.cs及MainForm.Designer.cs ...
随机推荐
- 分布式缓存系统 Memcached 半同步/半异步模式
在前面工作线程初始化的分析中讲到Memcached采用典型的Master_Worker模式,也即半同步/半异步的高效网络并发模式.其中主线程(异步线程)负责接收客户端连接,然后分发给工作线程,具体由工 ...
- 1112 Stucked Keyboard
题意:坏掉的键若被按下,总是重复打出k次.比如,k为3,打出的序列如下—— thiiis iiisss a teeeeeest 坏掉的键是i和e,虽然iiisss中s也出现了3次,但它不是坏掉的键,因 ...
- 记一次oralce 11g r2 rac安装问题
环境:虚拟机[root@rac1 ~]# lsb_release -aLSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd6 ...
- MySQL优化方法论
MySQL优化方法 主机 操作系统 数据库 应用 MySQL优化理论 吞吐率(Throughput) VS 延时(Latency) 吞吐率: 我们一般使用单位时间内服务器处理的请求数来描述其并发处理能 ...
- mybatis 学习一 建立maven项目
一.直接建立Maven项目方法 1.建立Maven项目 接下来使用Eclipse的maven构建一个web项目,以构建SpringMVC项目为例: 1.1 选择建立Maven Project 选择Fi ...
- 用css实现图片在div内的垂直居中
已知一个div内有一个img,两者的高度均不知道,但图片的高度一定小于div的高度 代码如下: .box { /*非IE的主流浏览器识别的垂直居中的方法*/ display: table-cell; ...
- Python Twisted系列教程7:小插曲,Deferred
作者:dave@http://krondo.com/an-interlude-deferred/ 译者:杨晓伟(采用意译) 你可以从这里从头开始阅读这个系列 回调函数的后序发展 在第六部分我们认识这 ...
- Flask之RESTful
5.4 Restful 2000年,Roy Thomas Fielding博士在他的博士论文<Architectural Styles and the Design of Network-bas ...
- Oracle使用split和splitstr函数批量分隔字符串
/* * Oracle 创建 split 和 splitstr 函数 */ /* 创建一个表类型 */ ) / /* 创建 split 函数 */ CREATE OR REPLACE FUNCTION ...
- 虚拟机之 LAMP
LAMP 就是Linux apache mysql php 一.下载: 安装下载工具 yum install wget -y mysql:5.5.47 wget http://mirrors.sohu ...