以颜色打印 C# 保留关键字
好的,在我的记事本之类的程序中,我想打印像代码一样的文本,所以我希望保留字以蓝色打印,文本在页边空白内,有什么想法如何做到这一点吗? 这就是我到目前为止所拥有的。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个包含保留字的 HashSet
然后您必须找到文本行中包含的标识符。标识符是以字母或下划线开头,由字母、下划线和数字组成的字符序列。
找到标识符后,检查它是否是保留关键字
Create a HashSet holding the reserved words
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