fastcoloredtextbox 中文不重叠

DrawLineChars方法: 

private void DrawLineChars(PaintEventArgs e, int firstChar, int lastChar, int iLine, int iWordWrapLine, int x,
                                   int y)
        {
            Line line = lines[iLine];
            LineInfo lineInfo = LineInfos[iLine];
            int from = lineInfo.GetWordWrapStringStartPosition(iWordWrapLine);
            int to = lineInfo.GetWordWrapStringFinishPosition(iWordWrapLine, line);

            int startX = x;
            if (startX < LeftIndent)
                firstChar++;

            lastChar = Math.Min(to - from, lastChar);

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            //folded block ?
            if (lineInfo.VisibleState == VisibleState.StartOfHiddenBlock)
            {
                //rendering by FoldedBlockStyle
                FoldedBlockStyle.Draw(e.Graphics, new Point(startX + firstChar * CharWidth, y),
                                      new Range(this, from + firstChar, iLine, from + lastChar + 1, iLine));
            }
            else
            {
                //render by custom styles
                StyleIndex currentStyleIndex = StyleIndex.None;
                int iLastFlushedChar = firstChar - 1;
                int isChinaese = 0;
                SizeF sizef = GetCharSize(new Font("宋体", 10), '中');
                int len = (int)sizef.Width - CharWidth;
                int nextLen = 0;
                for (int iChar = firstChar; iChar <= lastChar; iChar++)
                {
                    StyleIndex style = line[from + iChar].style;
                    if (IsChina(line[from + iChar].c))
                    {
                        isChinaese++;
                    }
                    if (currentStyleIndex != style)
                    {

                        FlushRendering(e.Graphics, currentStyleIndex,
                                       new Point(startX + (iLastFlushedChar + 1) * CharWidth+nextLen, y),
                                       new Range(this, from + iLastFlushedChar + 1, iLine, from + iChar, iLine));
                        iLastFlushedChar = iChar - 1;
                        currentStyleIndex = style;
                        if (isChinaese > 0)
                        {
                            nextLen = len * isChinaese;
                        }
                    }
                }
                FlushRendering(e.Graphics, currentStyleIndex, new Point(startX + (iLastFlushedChar + 1) * CharWidth+nextLen, y),
                               new Range(this, from + iLastFlushedChar + 1, iLine, from + lastChar + 1, iLine));
            }

            //draw selection
            if (!Selection.IsEmpty && lastChar >= firstChar)
            {
                e.Graphics.SmoothingMode = SmoothingMode.None;
                var textRange = new Range(this, from + firstChar, iLine, from + lastChar + 1, iLine);
                textRange = Selection.GetIntersectionWith(textRange);
                if (textRange != null && SelectionStyle != null)
                {
                    SelectionStyle.Draw(e.Graphics, new Point((startX + (textRange.Start.iChar - from) * CharWidth) * 2, y),
                                        textRange);
                }
            }
        }

 //判断是否是中文
        public bool IsChina(char c)
        {

            bool BoolValue = false;
            if (Convert.ToInt32(c) < Convert.ToInt32(Convert.ToChar(128)))
            {
                BoolValue = false;
            }
            else
            {
                return BoolValue = true;
            }
            return BoolValue;
        }

 //IME mode
            if (range.tb.ImeAllowed)
            for (int i = range.Start.iChar; i < range.End.iChar; i++)
            {
               
                SizeF size = FastColoredTextBox.GetCharSize(f, line[i].c);
               
                var gs = gr.Save();
                //float k = size.Width>range.tb.CharWidth + 1?range.tb.CharWidth / size.Width:1;
                //gr.TranslateTransform(x, y+(1-k)*range.tb.CharHeight/2);
                //gr.ScaleTransform(k, (float)Math.Sqrt(k));
                //gr.DrawString(line[i].c.ToString(), f, ForeBrush, 0, 0, stringFormat);
                //gr.Restore(gs);
              
                if(size.Width>range.tb.CharWidth*1.5f)
                    gr.DrawString(line[i].c.ToString(), f, ForeBrush, x, y+range.tb.CharHeight/4, stringFormat);
                else
                    gr.DrawString(line[i].c.ToString(), f, ForeBrush, x, y, stringFormat);
                if (IsChina(line[i].c))
                {
                    x += size.Width;
                }
                else
                {
                    x += dx;
                }
            }

原文地址:https://www.cnblogs.com/MyNameIsMT/p/3518328.html