我找到了一篇完全符合我需要的文章。它在文本框的同一行上绘制多种颜色。但问题是它是用 VB.NET 编写的,而我正在用 C# 编写程序。如果可能的话,任何优秀的灵魂都可以将其转换为 C#,如果不是,你能给我其他选择吗?谢谢。
这是文章:http ://www.vbrad.com/article.aspx?id=34 。
这是您发布的内容的转换 Nicolas
如果您需要更改/让其他任何工作正常工作,您将需要最终对其进行测试..
快乐编码
private void MeasureItemHandler(object sender, MeasureItemEventArgs e)
{
Graphics g = Graphics.FromHwnd(lstColor.Handle);
StringFormat sf = new StringFormat(StringFormat.GenericTypographic);
SizeF size = default(SizeF);
float height = 0;
Font oFont = new Font("Arial", 10);
//measure the height of what you are about to draw
//and let the listbox know this
size = g.MeasureString(data(e.Index), oFont, 500, sf);
height = size.Height + 5;
e.ItemHeight = height;
}
private void DrawItemHandler(object sender, DrawItemEventArgs e)
{
Graphics g = Graphics.FromHwnd(lstColor.Handle);
StringFormat sf = new StringFormat(StringFormat.GenericTypographic);
SizeF size = default(SizeF);
float width = 0;
Font oFont = new Font("Arial", 10);
//get the width of the string you are about to write
//this info is needed so that we can offset the next
//string that will be drawn in a different color.
size = g.MeasureString(data(e.Index), oFont, 500, sf);
width = size.Width + 16;
//prepare the list for drawing
e.DrawBackground();
e.DrawFocusRectangle();
//draw the first string in a certain color
e.Graphics.DrawString(data(e.Index), oFont, new SolidBrush(color(e.Index)), e.Bounds.X, e.Bounds.Y);
//draw the second string in a different color
e.Graphics.DrawString(data(data.Length - 1 - e.Index), oFont, new SolidBrush(color(color.Length - 1 - e.Index)), width, e.Bounds.Y);
}
首先,他们在本文中没有使用文本框,他们使用的是列表框,但接下来是您所问的从 VB.Net 到 C# 的代码转换。它需要整理一下,但它可以完成工作。
只需创建一个新的 Windows 窗体,在此窗体上放置一个名为 lstColor 的列表框,在属性窗口中将属性更改DrawMode
为OwnerDrawFixed,然后为DrawItem和MeasureItem添加事件处理程序(您可以通过单击属性窗口中的闪电来添加事件处理程序,然后双击列表中这两个单词旁边的空格)。
在DrawItem事件处理程序中,添加以下内容:
private void lstColor_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
var size = g.MeasureString(data[e.Index], oFont, 500, sf);
var width = size.Width + 16;
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(data[e.Index], oFont, new SolidBrush(color[e.Index]), e.Bounds.X, e.Bounds.Y);
e.Graphics.DrawString(data[data.Length - 1 - e.Index], oFont, new SolidBrush(color[color.Length - 1 - e.Index]), width, e.Bounds.Y);
}
在MeasureItem事件处理程序中,添加以下内容:
private void lstColor_MeasureItem(object sender, MeasureItemEventArgs e)
{
var size = g.MeasureString(data[e.Index], oFont, 500, sf);
var height = size.Height;
e.ItemHeight = Convert.ToInt32(height);
}
在任何方法的范围之外添加五个私有字段,但在您的Form1(或您称之为表单的任何内容)类中,如下所示:
private string[] data;
private Color[] color;
private Font oFont;
private Graphics g;
private StringFormat sf;
将以下三行放入Form1_Load事件中:
private void Form1_Load(object sender, EventArgs e)
{
oFont = new Font("Arial", 10);
data = new string[] { "This is Red", "This is Blue", "This is Green", "This is Yellow", "This is Black", "This is Aqua", "This is Brown", "This is Cyan", "This is Gray", "This is Pink" };
color = new Color[] {Color.Red, Color.Blue, Color.Green, Color.Yellow, Color.Black, Color.Aqua, Color.Brown, Color.Cyan, Color.Gray,Color.Pink};
lstColor.DataSource = data;
g = Graphics.FromHwnd(lstColor.Handle);
sf = new StringFormat(StringFormat.GenericTypographic);
}
你都准备好了。
希望这可以帮助
查看http://converter.telerik.com/ 它将代码从 VB.NET 转换为 C#,将 C# 转换为 VB.NET。它不适用于复杂的代码,但可能对您有用。