T
Terry
What I would like to do is override the default ListViewItem selection
(solid rect) drawing and draw my own. It seems like it should be a
relatively simple thing to do, but I'm running into problems.
I set the appropriate Control styles:
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
Then, I over ride OnPaint and do what seems like appropriate drawing
behavior. But, what results is basically a mess. Only partial rectangles
are drawn and the very first item in the list refuses to paint when being
clicked on. If the entire form loses focus or the first item is covered and
uncovered it will paint properly. So, obviously, the default behavior of
the control is interfering with my painting somewhere.
Can anyone see what I'm missing?
Here's my OnPaint:
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (this.View == View.Details)
{
Console.WriteLine(e.ToString());
Graphics g = e.Graphics;
g.Clear(this.BackColor);
// Draw the text for all items
foreach (ListViewItem lvi in Items)
{
if (lvi.Selected || lvi.Focused)
{
g.DrawRectangle(new Pen(Color.Orange), lvi.Bounds);
}
g.DrawString(lvi.Text, lvi.Font, new SolidBrush(lvi.ForeColor), lvi.Bounds,
StringFormat.GenericDefault);
}
}
}
Thanks,
Terry
(solid rect) drawing and draw my own. It seems like it should be a
relatively simple thing to do, but I'm running into problems.
I set the appropriate Control styles:
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
Then, I over ride OnPaint and do what seems like appropriate drawing
behavior. But, what results is basically a mess. Only partial rectangles
are drawn and the very first item in the list refuses to paint when being
clicked on. If the entire form loses focus or the first item is covered and
uncovered it will paint properly. So, obviously, the default behavior of
the control is interfering with my painting somewhere.
Can anyone see what I'm missing?
Here's my OnPaint:
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (this.View == View.Details)
{
Console.WriteLine(e.ToString());
Graphics g = e.Graphics;
g.Clear(this.BackColor);
// Draw the text for all items
foreach (ListViewItem lvi in Items)
{
if (lvi.Selected || lvi.Focused)
{
g.DrawRectangle(new Pen(Color.Orange), lvi.Bounds);
}
g.DrawString(lvi.Text, lvi.Font, new SolidBrush(lvi.ForeColor), lvi.Bounds,
StringFormat.GenericDefault);
}
}
}
Thanks,
Terry