Ownerdraw listview in 2.0 - DrawColumnHeader question

B

Bob Dankert

In 2.0, with an ownerdrawn listview, is there any way to ownerdraw the
entire column header section? Currently, I can draw the column headers for
each column but I can not draw the region to the right of the column
headers. For example, a 500px wide listview with one 100px wide column will
have a 100px columnheader and then 400px of extra space. I want to change
the background color of the 400px of extra space.

Thanks for any help,

Bob Dankert
 
J

Jeffrey Tan[MSFT]

Hi Bob,

Thanks for your post.

Yes, I see your concern. This because the right column of the header
columns is not a real column header. It is just a place holder for the
remain space. And the ListView
owner-draw does not provide notification for this place holder drawing.

If you really want to draw something on this extra place holder header. We
can just draw this together with the last column header draw. Code like
this:
private void listView1_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
using (StringFormat sf = new StringFormat())
{
// Store the column text alignment, letting it default
// to Left if it has not been set to Center or Right.
switch (e.Header.TextAlign)
{
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}

// Draw the standard header background.
e.DrawBackground();

// Draw the header text.
using (Font headerFont =
new Font("Helvetica", 10, FontStyle.Bold))
{
e.Graphics.DrawString(e.Header.Text, headerFont,
Brushes.Black, e.Bounds, sf);

if (e.ColumnIndex == 3)
{
Rectangle rc = new Rectangle(e.Bounds.Right, e.Bounds.Top,
e.Bounds.Width, e.Bounds.Height);
e.Graphics.DrawString("Test", headerFont,
Brushes.Black, rc, sf);
}
}

}
return;
}
Note: in the code snippet, I assume the column number is 4, then I draw to
the right rectangle of current header when ColumnIndex is 3.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Bob Dankert

Thanks Jeffrey, it seems obvious now that you show me the solution.

Take care,

Bob Dankert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top