Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the header height of a Listview

Can somebody tell me how to get the header height of a ListView.

like image 311
Brad Avatar asked Feb 11 '09 21:02

Brad


2 Answers

Here's how to get the listview header height using Win32 Interop calls.

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct RECT 
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

const long LVM_FIRST = 0x1000;
const long LVM_GETHEADER = (LVM_FIRST + 31);

[DllImport("user32.dll", EntryPoint="SendMessage")]
private static extern IntPtr SendMessage(IntPtr hwnd, long wMsg, long wParam, long lParam);

[DllImport("user32.dll")]
private static extern bool GetWindowRect(HandleRef hwnd, out RECT lpRect);

RECT rc = new RECT();
IntPtr hwnd = SendMessage(ListView1.Handle, LVM_GETHEADER, 0, 0);
if (hwnd != null) 
{
    if (GetWindowRect(new HandleRef(null, hwnd), out rc)) 
    {
        int headerHeight = rc.Bottom - rc.Top;
    }
}
like image 162
Phaedrus Avatar answered Oct 19 '22 03:10

Phaedrus


This might be a little bit hacky but you can do:

listView.Items[0].Bounds.Top

This will only work if there is only one item in the list. So you might want to temporarily add one when you first create the list and keep the height value.

Else, you can always use:

listView.TopItem.Bounds.Top

To make the test at any moment, but you still need at least one item in the list.

like image 40
Coincoin Avatar answered Oct 19 '22 04:10

Coincoin