C# 如何在listBox的特定案例中为特定项目的文本着色?
我有这个方法:C# 如何在listBox的特定案例中为特定项目的文本着色?,c#,.net,winforms,C#,.net,Winforms,我有这个方法: private void batch_Resize(Image sourceImage,string oldfName, string sourceDirectory,string oldFileName) { Bitmap newImage = new Bitmap(512, 512); using (Graphics gr = Graphics.FromImage(newImage)) {
private void batch_Resize(Image sourceImage,string oldfName, string sourceDirectory,string oldFileName)
{
Bitmap newImage = new Bitmap(512, 512);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(sourceImage, new Rectangle(0, 0, newImage.Width, newImage.Height));
i = i + 1;
newImage.Save(@"d:\NewImages1\" + i.ToString("D6") + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
newImage.Save(@"d:\NewImages1\" + oldfName + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
string filesExit = sourceDirectory + "\\"+ oldfName + ".gif";
if (!File.Exists(filesExit))
{
newImage.Save(sourceDirectory + "\\" + oldfName + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
}
else
{
itemToColor = "File already exist and was not overwritten:";
listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add("File already exist and was not overwritten: " + oldfName); }));
}
}
if (newImage != null)
newImage.Dispose();
}
我想把调用的项涂成红色:
listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add("File already exist and was not overwritten: " + oldfName); }));
我想用红色显示文本:文件已存在且未被覆盖:
我在设计器中将listbox1绘图模式更改为OwnderDrawFixed
并添加了draw item事件
/// <summary>
/// Handles the DrawItem event of the listBox1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.DrawItemEventArgs"/> instance containing the event data.</param>
private void listBox1_DrawItem( object sender, DrawItemEventArgs e )
{
e.DrawBackground();
Graphics g = e.Graphics;
// draw the background color you want
// mine is set to olive, change it to whatever you want
g.FillRectangle( new SolidBrush( Color.Olive), e.Bounds );
// draw the text of the list item, not doing this will only show
// the background color
// you will need to get the text of item to display
g.DrawString( THE_LIST_ITEM_TEXT , e.Font, new SolidBrush( e.ForeColor ), new PointF( e.Bounds.X, e.Bounds.Y) );
e.DrawFocusRectangle();
}
在draw item事件中,我执行了以下操作:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.Olive), e.Bounds);
g.DrawString(itemToColor, e.Font, new SolidBrush(e.ForeColor), new PointF(e.Bounds.X, e.Bounds.Y));
}
itemToColor是格式1中的全局字符串,我想为文本着色:
itemToColor = "File already exist and was not overwritten:";
listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add("File already exist and was not overwritten: " + oldfName); }));
但它不起作用
如何仅将调用中的文本涂成红色?文件已存在且未被覆盖:
因此,只有当它出现在方法if中时,它才会对其进行着色。下面是一个工作示例代码 记住将DrawMode更改为OwnerDrawFixed并处理DrawItem事件
/// <summary>
/// Handles the DrawItem event of the listBox1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.DrawItemEventArgs"/> instance containing the event data.</param>
private void listBox1_DrawItem( object sender, DrawItemEventArgs e )
{
e.DrawBackground();
Graphics g = e.Graphics;
// draw the background color you want
// mine is set to olive, change it to whatever you want
g.FillRectangle( new SolidBrush( Color.Olive), e.Bounds );
// draw the text of the list item, not doing this will only show
// the background color
// you will need to get the text of item to display
g.DrawString( THE_LIST_ITEM_TEXT , e.Font, new SolidBrush( e.ForeColor ), new PointF( e.Bounds.X, e.Bounds.Y) );
e.DrawFocusRectangle();
}
//
///处理listBox1控件的DrawItem事件。
///
///事件的来源。
///包含事件数据的实例。
私有void listBox1_DrawItem(对象发送方,DrawItemEventArgs e)
{
e、 牵引杆接地();
图形g=e.图形;
//绘制所需的背景色
//我的设置为olive,可以随意更改
g、 FillRectangle(新的SolidBrush(颜色为橄榄色),e.Bounds);
//绘制列表项的文本,不这样做只会显示
//背景色
//您需要获取要显示的项目的文本
g、 DrawString(列表项文本、字体、新SolidBrush(e.ForeColor)、新PointF(e.Bounds.X、e.Bounds.Y));
e、 DrawFocusRectangle();
}
可能是缺少e.DrawFocusRectangle()导致了问题。如果您没有任何其他指示器,哪些项目应该是红色的,以下是低技术方法:
string itemToColor = "File already exist and was not overwritten.";
使用ListView(正如我误读的那样),代码如下:
private void listView1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Item.Text != itemToColor ) e.DrawDefault = true;
else
{
e.DrawBackground();
// e.Graphics.FillRectangle(new SolidBrush(Color.Olive), e.Bounds); // optional
e.Graphics.DrawString(itemToColor, listView1.Font,
Brushes.Red, new PointF(e.Bounds.X, e.Bounds.Y));
}
}
注意:根据您的需要,您可能还需要对DrawHeader
和DrawSubItem
事件进行编码
实际上,您使用的是一个很好的旧列表框,这里有与ListBox.DrawItem
事件相同的代码:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
SolidBrush brush = null;
if (listBox1.Items[e.Index].ToString() != itemToColor )
brush = new SolidBrush(e.ForeColor);
else brush = new SolidBrush(Color.Red);
e.DrawBackground();
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),
e.Font, brush, new PointF(e.Bounds.X, e.Bounds.Y));
}
不确定是否真的要用橄榄色绘制背景。。如果是这样,您可以轻松地稍微更改cvode
但是项目列表、项目索引或设置标签可能更符合您的喜好。它怎么不起作用?如果只有一个项目应该是不同的颜色,你需要知道哪一个;您可以保留一份inices列表或设置标签。。如果文本中只有一部分是红色的,那么您需要解析这些部分,并对每个部分使用DrawString调用您可以使用e中的参数来了解您正在绘制的项目,但代码必须适用于所有项目!对于普通项目,您只需调用
e.DrawDefault()代码>TaW e没有Item属性也没有DrawDefault没有e.Item也没有e.DrawDefaultWhoops,你是对的;我为一个列表视图
编码,顺便说一句,它几乎总是更好的选择。很抱歉,请参阅UZUpdate的答案!