Dav*_*all 13
您可以通过将列表视图的OwnerDraw属性设置为true来执行此操作.
然后,这允许您为listview的绘制事件提供事件处理程序.
MSDN上有一个详细的例子
下面是一些将标题颜色设置为红色的示例代码:
private void listView1_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red, e.Bounds);
e.DrawText();
}
Run Code Online (Sandbox Code Playgroud)
我认为(但很高兴被证明是错误的)将OwnerDraw设置为true,您还需要为其他具有默认实现的绘制事件提供处理程序,如下所示:
private void listView1_DrawItem(object sender,
DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
Run Code Online (Sandbox Code Playgroud)
我当然没有设法让listview绘制没有它的项目.
小智 9
我知道派对有点晚了,但我仍然看到这篇帖子,这对我有帮助.这是一个提供的代码david的抽象应用程序
using System.Windows.Forms;
using System.Drawing;
//List view header formatters
public static void colorListViewHeader(ref ListView list, Color backColor, Color foreColor)
{
list.OwnerDraw = true;
list.DrawColumnHeader +=
new DrawListViewColumnHeaderEventHandler
(
(sender, e) => headerDraw(sender, e, backColor, foreColor)
);
list.DrawItem += new DrawListViewItemEventHandler(bodyDraw);
}
private static void headerDraw(object sender, DrawListViewColumnHeaderEventArgs e, Color backColor, Color foreColor)
{
using (SolidBrush backBrush = new SolidBrush(backColor))
{
e.Graphics.FillRectangle(backBrush, e.Bounds);
}
using (SolidBrush foreBrush = new SolidBrush(foreColor))
{
e.Graphics.DrawString(e.Header.Text, e.Font, foreBrush, e.Bounds);
}
}
private static void bodyDraw(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
Run Code Online (Sandbox Code Playgroud)
然后在表单构造函数中调用它
public Form()
{
InitializeComponent();
*CLASS NAME*.colorListViewHeader(ref myListView, *SOME COLOR*, *SOME COLOR*);
}
Run Code Online (Sandbox Code Playgroud)
只需将*CLASS NAME*替换为您放入第一位代码的类和*某些颜色*的某种颜色.
//Some examples:
Color.white
SystemColors.ActiveCaption
Color.FromArgb(0, 102, 255, 102);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26846 次 |
| 最近记录: |