以颜色打印 C# 保留关键字

发布于 2025-01-07 10:24:14 字数 983 浏览 1 评论 0原文

好的,在我的记事本之类的程序中,我想打印像代码一样的文本,所以我希望保留字以蓝色打印,文本在页边空白内,有什么想法如何做到这一点吗? 这就是我到目前为止所拥有的。

    int charPag = 0;
        int linPag = 0;
        Font rodFont = new Font("Courier New", (float)10.0);

        e.Graphics.MeasureString(stringToPrint, txtMain.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, out charPag, out linPag);
        e.Graphics.DrawString(stringToPrint, txtMain.Font, new SolidBrush(Color.Black), e.MarginBounds, StringFormat.GenericTypographic);
        stringToPrint = stringToPrint.Substring(charPag);

        e.Graphics.DrawLine(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Bottom, e.MarginBounds.Right, e.MarginBounds.Bottom);
        e.Graphics.DrawString(numPag.ToString(), rodFont, Brushes.Black, e.MarginBounds.Right - (numPag.ToString().Length * rodFont.SizeInPoints), e.MarginBounds.Bottom + 5);
        if (stringToPrint.Length > 0)
        {
            e.HasMorePages = true;
            numPag++;
        }

OK in my notepad like program, I want to print the text like code so I want the reserved words to be printed in blue and the text to be within the margins any ideas how to do this??
This is what i have so far.

    int charPag = 0;
        int linPag = 0;
        Font rodFont = new Font("Courier New", (float)10.0);

        e.Graphics.MeasureString(stringToPrint, txtMain.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, out charPag, out linPag);
        e.Graphics.DrawString(stringToPrint, txtMain.Font, new SolidBrush(Color.Black), e.MarginBounds, StringFormat.GenericTypographic);
        stringToPrint = stringToPrint.Substring(charPag);

        e.Graphics.DrawLine(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Bottom, e.MarginBounds.Right, e.MarginBounds.Bottom);
        e.Graphics.DrawString(numPag.ToString(), rodFont, Brushes.Black, e.MarginBounds.Right - (numPag.ToString().Length * rodFont.SizeInPoints), e.MarginBounds.Bottom + 5);
        if (stringToPrint.Length > 0)
        {
            e.HasMorePages = true;
            numPag++;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天的雪花 2025-01-14 10:24:14

创建一个包含保留字的 HashSet

public HashSet<string> _reservedWords = 
    new HashSet { "if", "else", "class", "..." };

然后您必须找到文本行中包含的标识符。标识符是以字母或下划线开头,由字母、下划线和数字组成的字符序列。

找到标识符后,检查它是否是保留关键字

bool reserved = _reservedWords.Contains(word);

Create a HashSet holding the reserved words

public HashSet<string> _reservedWords = 
    new HashSet { "if", "else", "class", "..." };

Then you will have to find the identifiers contained in a text line. Identifiers are character sequences beginning with a letter or an underscore and consisting of letters, underscores and digits.

Once you have found an identifier, check whether it is a reserved keyword or not

bool reserved = _reservedWords.Contains(word);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文