• 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).
      • 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 >= 0)
    11             {
    12                 TextFormatFlags formatFlags = TextFormatFlags.NoPadding | TextFormatFlags.SingleLine;
    13                 //TextFormatFlags formatFlags = TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak;
    14                 int leftAndTopPadding = 5;
    15                 string headText = entireText.Substring(0, 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)(1 * e.Font.GetHeight() / 6);
    22                 int rightTextMargin = (int)(1 * e.Font.GetHeight() / 4);
    23                 if (index > 0)
    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 - 2 * leftTextMargin - rightTextMargin, yPosition), Color.Red, formatFlags);
    27                     TextRenderer.DrawText(e.Graphics, tailText, e.Font, new Point(leftAndTopPadding + headTextPartWidth + highlightedTextWidth - 3 * leftTextMargin - 2 * 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 - 2 * 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         }
  • 相关阅读:
    「Vijos 1282」「OIBH杯NOIP2006第二次模拟赛」佳佳的魔法照片
    「Vijos 1285」「OIBH杯NOIP2006第二次模拟赛」佳佳的魔法药水
    「Vijos 1284」「OIBH杯NOIP2006第二次模拟赛」佳佳的魔法阵
    「Vijos 1283」「OIBH杯NOIP2006第二次模拟赛」佳佳的魔杖
    「2018-12-02模拟赛」T3 约束排列 解题报告
    「2018-12-02模拟赛」T2 种树 解题报告
    「2018-12-02模拟赛」T1 最短路 解题报告
    「分块系列」公主的朋友 解题报告
    「分块系列」「洛谷P4168 [Violet]」蒲公英 解题报告
    Java高级架构师(一)第03节:多模块多Web应用合并War包
  • 原文地址:https://www.cnblogs.com/wyp1988/p/9895246.html
Copyright © 2020-2023  润新知