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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值