ListView drawsubitem - 未选择列
ListView drawsubitem - column is not selected
为什么没有选择第 4 列?我使用 listView3_DrawSubItem 和 e.DrawDefault = true;对于该列,现在不可选择。
编辑:
listView1_DrawSubItem代码:
// Only interested in 2nd column.
if (e.Header != this.action)
{
e.DrawDefault = true;
return;
}
drawItem(e);
以及绘图项目代码:
string drawString = e.SubItem.Text;
float size = 8.25F;
e.DrawBackground();
Bitmap image = new Bitmap(DesktopCleaner.Properties.Resources.folder_icon_512x512);
if (drawString == "Leave on Desktop")
{
image = new Bitmap(DesktopCleaner.Properties.Resources.desk);
}
else if (drawString == "Recycle")
{
image = new Bitmap(DesktopCleaner.Properties.Resources.recyclebin_preview_1);
}
else if (drawString == "Delete")
{
image = new Bitmap(DesktopCleaner.Properties.Resources.free_vector_delete_icon_101805_Delete_icon);
}
var imageRect = new Rectangle(e.Bounds.X + 3, e.Bounds.Y, image.Width - 2, image.Height - 2);
e.Graphics.DrawImage(image, imageRect);
System.Drawing.Font drawFont = new System.Drawing.Font(listView1.Font.FontFamily, size, FontStyle.Bold);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
var strrect = new Rectangle(e.Bounds.X + 18, e.Bounds.Y + 3, 150, e.Bounds.Height);
e.Graphics.DrawString(drawString, drawFont, drawBrush, strrect, drawFormat);
您负责绘制选区。
所以即使你使用好的方法:
e.DrawBackground();
e.DrawText();
未绘制任何选择。
所以你需要使用 FillRectangle
和 DrawString
以及适当的 Colors
,可能像这样:
bool selected = e.Item.Selected;
using ( SolidBrush backBrush = new SolidBrush(
selected? SystemColors.MenuHighlight :SystemColors.Window ) )
e.Graphics.FillRectangle(backBrush, e.Bounds);
using (SolidBrush textBrush = new SolidBrush(
selected ? Color.White : Color.Black ))
e.Graphics.DrawString(e.Item.Text, yourFont, textBrush, e.Bounds.X, e.Bounds.Y);
代码精简;您将使用坐标为图标等腾出空间。
为什么没有选择第 4 列?我使用 listView3_DrawSubItem 和 e.DrawDefault = true;对于该列,现在不可选择。
编辑: listView1_DrawSubItem代码:
// Only interested in 2nd column.
if (e.Header != this.action)
{
e.DrawDefault = true;
return;
}
drawItem(e);
以及绘图项目代码:
string drawString = e.SubItem.Text;
float size = 8.25F;
e.DrawBackground();
Bitmap image = new Bitmap(DesktopCleaner.Properties.Resources.folder_icon_512x512);
if (drawString == "Leave on Desktop")
{
image = new Bitmap(DesktopCleaner.Properties.Resources.desk);
}
else if (drawString == "Recycle")
{
image = new Bitmap(DesktopCleaner.Properties.Resources.recyclebin_preview_1);
}
else if (drawString == "Delete")
{
image = new Bitmap(DesktopCleaner.Properties.Resources.free_vector_delete_icon_101805_Delete_icon);
}
var imageRect = new Rectangle(e.Bounds.X + 3, e.Bounds.Y, image.Width - 2, image.Height - 2);
e.Graphics.DrawImage(image, imageRect);
System.Drawing.Font drawFont = new System.Drawing.Font(listView1.Font.FontFamily, size, FontStyle.Bold);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
var strrect = new Rectangle(e.Bounds.X + 18, e.Bounds.Y + 3, 150, e.Bounds.Height);
e.Graphics.DrawString(drawString, drawFont, drawBrush, strrect, drawFormat);
您负责绘制选区。
所以即使你使用好的方法:
e.DrawBackground();
e.DrawText();
未绘制任何选择。
所以你需要使用 FillRectangle
和 DrawString
以及适当的 Colors
,可能像这样:
bool selected = e.Item.Selected;
using ( SolidBrush backBrush = new SolidBrush(
selected? SystemColors.MenuHighlight :SystemColors.Window ) )
e.Graphics.FillRectangle(backBrush, e.Bounds);
using (SolidBrush textBrush = new SolidBrush(
selected ? Color.White : Color.Black ))
e.Graphics.DrawString(e.Item.Text, yourFont, textBrush, e.Bounds.X, e.Bounds.Y);
代码精简;您将使用坐标为图标等腾出空间。