Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

c# - Drawing outside of column area in listview column header

Is it possible to ownerdraw the entire column header section of a listview? (including the region to the right of the column headers)? ListView is in Details View.

An answer here indicates that the remaining space can be drawn along with the last column header: http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic32927.aspx

But it does not seem to work at all - nothing is drawn outside header area.

The proposed solution is based on drawing outside of the passed Bounds:

if (e.ColumnIndex == 3) //last column index
{
    Rectangle rc = new Rectangle(e.Bounds.Right, //Right instead of Left - offsets the rectangle
            e.Bounds.Top, 
            e.Bounds.Width, 
            e.Bounds.Height);

    e.Graphics.FillRectangle(Brushes.Red, rc);
}

The ClipBounds property of the available Graphics instance indicates an unbound area (from large negative numbers to large positive). But nothing is drawn outside the columnheader area of the last column.

Does anybody have a solution for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm surprised by Jeffery Tan's answer in that post. His solution cannot work, since the code tries to draw outside of the header control client area. The hDC used within custom drawing (and hence owner drawing) is for the client area of the control, and so cannot be used to paint in the non-client area. The area to the right of the right most column in a header control is in non-client area. So you need a different solution.

Possible Solutions

  1. Hi tech and partially effective

You can enable drawing outside the client area by using the GetDC() WinAPI call:

[System.Runtime.InteropServices.DllImport("user32")]
private static extern IntPtr GetDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("user32")]
private static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);

public static IntPtr GetHeaderControl(ListView list) {
    const int LVM_GETHEADER = 0x1000 + 31;
    return SendMessage(list.Handle, LVM_GETHEADER, 0, 0);
}

In your column draw event handler, you will need something like this:

if (e.ColumnIndex == 3) //last column index
{
  ListView lv = e.Header.ListView;
  IntPtr headerControl = NativeMethods.GetHeaderControl(lv);
  IntPtr hdc = GetDC(headerControl);
  Graphics g = Graphics.FromHdc(hdc);

  // Do your extra drawing here
  Rectangle rc = new Rectangle(e.Bounds.Right, //Right instead of Left - offsets the rectangle
            e.Bounds.Top, 
            e.Bounds.Width, 
            e.Bounds.Height);

    e.Graphics.FillRectangle(Brushes.Red, rc);

  g.Dispose();
  ReleaseDC(headerControl, hdc);
}

But the problem with this is that since your drawing is outside the client area, Windows doesn't always know when it should be drawn. So it will disappear sometimes, and then be redrawn when Windows thinks the header needs repainting.

  1. Low tech but ugly

Add an extra empty column to your control, owner draw it do look however you want, make it very wide, and turn off horizontal scrolling (optional).

I know this is horrible, but you're looking for suggestions :)

  1. Most effective, but still not perfect

Use ObjectListView. This wrapper around a .NET ListView allows you to add overlays to your list -- an overlay can draw anywhere within the ListView, including the header. [Declaration: I'm the author of ObjectListView, but I still think it is best solution]

public class HeaderOverlay : AbstractOverlay
{
    public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
        if (olv.View != System.Windows.Forms.View.Details)
            return;

        Point sides = NativeMethods.GetColumnSides(olv, olv.Columns.Count-1);
        if (sides.X == -1)
            return;

        RectangleF headerBounds = new RectangleF(sides.Y, 0, r.Right - sides.Y, 20);
        g.FillRectangle(Brushes.Red, headerBounds);
        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Center;
        sf.LineAlignment = StringAlignment.Center;
        g.DrawString("In non-client area!", new Font("Tahoma", 9), Brushes.Black, headerBounds, sf);
    }
}

This gives this: alt text

[Reading over this answer, I think this is an example of trying too hard :) Hope you find something here helpful.]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...